开发者社区 问答 正文

Spring Aspect作用于action,action中的Autowired无效:配置报错 

spring-mvc-servlet.xml

<!-- --> <context:annotation-config annotation-config="true"></context:annotation-config> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

<!-- -->

Aspect 实现文件

@Aspect @Component public class FormRepeat {

@Pointcut("@annotation(com.job.bs.web.repeat.CreateFormToken)")
public void afterCreateToken(){
	System.out.println("22222222222");
}
 
//后置通知,创建token
@After("afterCreateToken()")
public Object afterCreateToken(JoinPoint joinPoint){
	System.out.println("调用了后置通知");
}

}

Controller 
@Autowired	 BaseDao baseDao;
@RequestMapping("/addcompany.html") @CreateFormToken public ModelAndView addcompany(HttpServletRequest request, HttpServletResponse response) { ModelAndView view = new ModelAndView("company/addcompany"); System.out.println("进入"); view.getModelMap().addAttribute("industrys", baseDao.findIndustry()); //      view.getModelMap().addAllAttributes(baseDao.addModelToken(request));//增加token校验部分 return view; }

controller 请求进入和后置通知都可以进入.但是就是上面的 @Autowired  BaseDao baseDao;为空!
我去掉@CreateFormToken标签baseDao对象就不为空了.但是同样也进不去通知了.

求指教.

展开
收起
kun坤 2020-06-02 14:37:34 640 分享 版权
1 条回答
写回答
取消 提交回答
  • 求高手指教.######围观一下

    @Resource 是根据name注入 

    @Autowired  是根据接口注入 要不你换  @Resource 试试

    ######早试过了的。没用.

    2020-06-02 14:37:40
    赞同 展开评论