标签:
我用spring aop给controller层添加前置通知:
controller类:
@Controller @RequestMapping("/projectController/") public class ProjectController { private static Log log = LogFactory.getLog(ProjectController.class); private ProjectService projectService = new ProjectService(); @RequestMapping("test") @ResponseBody public void test() { System.out.println(projectService); } }
spring 的核心配置文件DispatcherServlet.xml
<!--扫描文件--> <context:component-scan base-package="com.binhu.uem" /> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
aop相关类
@Aspect @Component public class ControllerOpreaAop { @Before("execution(* com.binhu.uem..*Controller.*(..))") public void permissionCheck(JoinPoint point) throws Throwable { System.out.println( "@Before " + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName()); System.out.println("@Before " + Arrays.toString(point.getArgs())); } }
由于涉及公司的隐私,所以只贴出相关代码段。
问题:
我的ProjectService类没有被spring管理,启动tomcat后通过浏览器访问ProjectController ,我的projectService却是null。就好像没有被初始化一样,所以调用ProjectService类中方法就报空指针异常。当我出去aop相关代码就没问题了。所以我想问问大家这可能是是什么原因?
标签:
原文地址:http://my.oschina.net/huhaoren/blog/527745