您的位置:首页 > 编程语言 > Java开发

spring的aop解读

2016-04-25 00:35 393 查看
之前讲过动态代理,其实我们spring比较重要的是IOC和AOP 而IOC的底层原理是反射,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是很好理解的!

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring aop