Spring AOP Demo
2016-03-10 14:26
253 查看
/** * Created by ZhangZhengyi on 2016/3/10. * 目标类 */ public class HelloWorldService { public void sayHello() { System.out.println("这是目标类"); } }
/** * Created by ZhangZhengyi on 2016/3/10. *切面 */ public class HelloWorldAspect { //前置通知 public void beforeAdvice() { System.out.println("===========这是前置通知"); } //后置最终通知 public void afterFinallyAdvice() { System.out.println("===========这是后置通知"); } }/** * Created by ZhangZhengyi on 2016/3/10.
*/ public class AopTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorldService helloworldService = ctx.getBean("helloWorldService", HelloWorldService.class); helloworldService.sayHello(); } }
spring配置:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="helloWorldService" class="foo.bar.Spring.AOP.HelloWorldService"/> <bean id="aspect" class="foo.bar.Spring.AOP.HelloWorldAspect"/> <!--<aop:aspectj-autoproxy proxy-target-class="true"/>--> <aop:config> <aop:pointcut id="pointcut" expression="execution(* foo.bar.Spring.AOP.*.*(..))"/> <aop:aspect ref="aspect"> <aop:before method="beforeAdvice" pointcut="execution(* foo..*.*(..))"/> <aop:after method="afterFinallyAdvice" pointcut="execution(* foo..*.*(..))"/> </aop:aspect> </aop:config>
文件结构:
相关文章推荐
- 【java-之路】泛型与元组
- struts学习
- struts2的零配置
- eclipse使用mybatis generator自动生成代码报错Generation Warnings Occured Project E: does
- Java中使用JNI
- spring学习
- Java中文编码小结
- C#用DES加密JAVA用DES解密,JAVA用DES加密C#用DES解密的实现
- Spring 注解学习手札(一) 构建简单Web应用
- 深入入门正则表达式(java) - 命名捕获
- 编写java程序推荐一个好的定义log的方式——lombok
- 联系人等拼音搜索算法与框架的心得
- java多线程下载
- leetcode 34. Search for a Range | Java最短代码实现
- [BAT][JAVA]定时任务之-Quartz使用篇
- Java抽象类与接口
- 使用maven命令创建java项目
- 2016蓝桥杯假期任务之《数字游戏》
- Java基础知识之集合框架(一)
- 做一个合格的程序猿之浅析Spring IoC源码(四)分析BeanPostProcessor(1)