spring的aop解读
2016-04-25 00:35
393 查看
之前讲过动态代理,其实我们spring比较重要的是IOC和AOP 而IOC的底层原理是反射,AOP的底层实现原理就是动态代理
spring实现aop的方式有两种
第一种:
配置文件
配置文件
结果:
其实,如果动了动态代理,那么spring的aop是很好理解的!
spring实现aop的方式有两种
第一种:
package com.asiainfo.spring; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 切面类(给主要的功能添加额外的功能) */ @Aspect public class AspectDemo { /* *前置通知,只在方法之前执行 *第一个*表示所有的返回对象 *第二个*表示包中所有的类 *第三个*表示以add开头的所有方 *第四个..表示方法中的所有参数 * / @Before("execution(* com.asiainfo.spring.*.add*(..))")public void before(){System.out.println("before");}}
配置文件
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="hello" class="com.asiainfo.spring.HelloBean"></bean> <bean id="aspect" class="com.asiainfo.spring.AspectDemo"></bean> <aop:aspectj-autoproxy /><!--打开aop的开关,自动代理--> </beans>测试:
package com.asiainfo.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml"); HelloBean HelloBean = (HelloBean)ac.getBean("hello"); HelloBean.add(); } }结果:
四月 25, 2016 12:37:46 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27dd2519: startup date [Mon Apr 25 00:37:46 GMT+08:00 2016]; root of context hierarchy 四月 25, 2016 12:37:46 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [aopconfig.xml] before spring add第二种:配置xml方式
配置文件
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="hello" class="com.asiainfo.spring.HelloBean"></bean> <bean id="aspect" class="com.asiainfo.spring.AspectXml"></bean> <aop:config> <aop:aspect id="aspectj" ref="aspect"> <aop:pointcut expression="execution(* com.asiainfo.spring.*.add*(..))" id="pointcut"/> <aop:before method="before" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>切面类:
package com.asiainfo.spring; public class AspectXml { public void before(){ System.out.println("before"); } }测试
package com.asiainfo.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("aopconfig.xml"); HelloBean HelloBean = (HelloBean)ac.getBean("hello"); HelloBean.add(); } }
结果:
四月 25, 2016 12:43:26 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27aea0c1: startup date [Mon Apr 25 00:43:26 GMT+08:00 2016]; root of context hierarchy 四月 25, 2016 12:43:26 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [aopconfig.xml] before spring add
其实,如果动了动态代理,那么spring的aop是很好理解的!
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- JavaScript AOP编程实例
- 使用AOP改善javascript代码
- 初识SmartJS - AOP三剑客
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- java使用动态代理来实现AOP(日志记录)的实例代码