Spring AOP配置源码

时间:2015-05-28 08:51:54
【文件属性】:

文件名称:Spring AOP配置源码

文件大小:364KB

文件格式:ZIP

更新时间:2015-05-28 08:51:54

aop spring

package com.spring.dao; import org.springframework.stereotype.Component; @Component("userDAO") public class UserDao { public void say() { System.out.println("say method is called"); } public void smile() { System.out.println("smile method is called"); } public void cry() { System.out.println("cry method is called"); } public void jump() { System.out.println("jump method is called"); } } 注意观察包名。@Component("userDAO")等价于在spring配置文件中定义一个 编写Service package com.spring.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.spring.dao.UserDao; @Component("userService") public class UserService { @Resource(name="userDAO") private UserDao dao; public UserDao getDao() { return dao; } public void setDao(UserDao dao) { this.dao = dao; } public void say() { dao.say(); } public void smile() { dao.smile(); } public void cry() { dao.cry(); } public void jump() { dao.jump(); } } 注意观察包名。@Component("userService")等价于在spring配置文件中定义一个 @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework.stereotype.Component; @Component("logIntercepter") public class LogIntercepter { public void before(){ System.out.println("----------before-------------"); } public void after(){ System.out.println("----------after-------------"); } public void exception(){ System.out.println("----------exception-------------"); } public void around(){ System.out.println("----------exception-------------"); } } 注意观察包名。@Component("logIntercepter")等价于在spring配置文件中定义一个 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <!-- --> 两行为开启spring的注解配置 引入具体的AOP操作类 声明一个切入点,注意execution表达式的写法 aop前置通知 aop后置通知, aop异常通知 以上结合起来意思就是在调用com.spring.service包或子包下的所有方法之前或之后或抛出异常时依次调用id为logIntercepter的类中的before after exception方法 测试用例 package com.spring.test; import javax.annotation.Resource; import org.junit.Test; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import com.spring.service.UserService; @ContextConfiguration(locations="classpath:applicationContext.xml") public class SpringTest extends AbstractJUnit4SpringContextTests { @Resource(name="userService") private UserService userService; @Test public void test1(){ userService.say(); System.out.println(); userService.smile(); System.out.println(); userService.cry(); } } 此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载applicationContext.xml @Resource(name="userService")意思是把userService注入进来 最终输出结果为: ----------before------------- say method is called ----------after------------- ----------before------------- smile method is called ----------after------------- ----------before------------- cry method is called ----------after-------------


【文件预览】:
AOP
----.springBeans(462B)
----src()
--------org.springframework.test-3.0.3.RELEASE.jar(200KB)
--------com()
--------applicationContext.xml(1KB)
----bin()
--------org.springframework.test-3.0.3.RELEASE.jar(200KB)
--------com()
--------applicationContext.xml(1KB)
----.classpath(2KB)
----.settings()
--------org.eclipse.jdt.core.prefs(629B)
----.project(587B)
----lib()

网友评论

  • 非常不错的资源。
  • 挺详细的,谢啦
  • 不错,有参考价值
  • 不错的资源 挺详细的 谢谢分享