spring的aop解读

之前讲过动态代理,其实我们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是很好理解的!

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值