spring中AOP实现的4中方式

[quote]配置可以通过xml文件来进行,大概有四种方式:

1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等

2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象

3. 通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点

4. 通过<aop:config>来配置[/quote]
[b][size=large]一、基于代理的AOP[/size][/b]
1、创建通知
package com.demo.aop.bean;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 通知
*
*/
public class ActionAdvice implements MethodBeforeAdvice,AfterReturningAdvice{

public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行方法之后调用"+System.currentTimeMillis());
}

public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行方法之前调用"+System.currentTimeMillis());

}

}

2、Service层


package com.demo.aop.service;

public interface IPersonService {
public void doUpdate();
}


3、Service实现层
package com.demo.aop.service.impl;

import org.springframework.stereotype.Component;

import com.demo.aop.service.IPersonService;

public class PersonService implements IPersonService {

public void doUpdate() {
// TODO Auto-generated method stub
System.out.println("执行更新方法"+System.currentTimeMillis());
}

}

4、文件配置
第一种:不实用自动代理
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 基于代理的AOP -->
<!-- 创建通知 -->
<bean id="actionAdvice" class="com.demo.aop.bean.ActionAdvice" />
<!-- 使用正则表达式切点 -->
<bean id="actionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*doUpdate" />
</bean>
<!-- 通知 与切点结合-->
<bean id="actionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="actionAdvice" />
<property name="pointcut" ref="actionPointcut" />
</bean>
<!-- 代理对象 -->
<bean id="actionProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="personService" />
<property name="interceptorNames" value="actionAdvisor" />
<property name="proxyInterfaces" value="com.demo.aop.service.IPersonService" />
</bean><!--此种代理,创建:IPersonService actionProxy声明,使用代理进行调用 -->
<bean id="personService" class="com.demo.aop.service.impl.PersonService"/>

</beans>

第二种:使用自动代理
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 基于代理的AOP -->
<!-- 创建通知 -->
<bean id="actionAdvice" class="com.demo.aop.bean.ActionAdvice" />
<!-- 使用正则表达式切点 -->
<bean id="actionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*doUpdate" />
</bean>
<!-- 通知 与切点结合-->
<bean id="actionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="actionAdvice" />
<property name="pointcut" ref="actionPointcut" />
</bean>
<!-- 代理对象 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <!--自动代理,创建对象正常使用,会在调用指定方法时进行执行-->
<bean id="personService" class="com.demo.aop.service.impl.PersonService"/>

</beans> 

5、测试
第一种:不使用自动代理情况下,注意创建对象时要将代理注入
package com.demo.aop.AopTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.aop.service.IPersonService;

/**
* 测试基于代理的AOP
* @author wwl
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" })
public class AopTest {
/**
* 用接口声明,注入代理对象
*/
@Autowired
private IPersonService actionProxy;
@Test
public void testAop(){
actionProxy.doUpdate();
}
}

第二种:使用自动代理
package com.demo.aop.AopTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.aop.service.IPersonService;

/**
* 测试基于代理的AOP
* @author wwl
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" })
public class AopTest {
/**
* 自动创建代理
*/
@Autowired
private IPersonService personService;
@Test
public void testAop(){
personService.doUpdate();
}
}

[b][size=large]二、使用AspectJ提供的注解[/size][/b]
1、创建通知
package com.demo.aop.bean;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 通知
*
*/
@Component
@Aspect
public class Action1Advice{
@Pointcut("execution(* *.doUpdate())")
public void actionPoint(){}

@AfterReturning("actionPoint()")
public void afterAction(){
System.out.println("执行方法之后调用"+System.currentTimeMillis());
}
@Before("actionPoint()")
public void beforeAction(){
System.out.println("执行方法之前调用"+System.currentTimeMillis());

}

}

2、Service层


package com.demo.aop.service;

public interface IPersonService {
public void doUpdate();
}


3、Service实现层
package com.demo.aop.service.impl;

import org.springframework.stereotype.Component;

import com.demo.aop.service.IPersonService;
@Component
public class PersonService implements IPersonService {

public void doUpdate() {
// TODO Auto-generated method stub
System.out.println("执行更新方法"+System.currentTimeMillis());
}

}

4、文件配置
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
<!-- @AspectJ注解 -->
<!-- 激活组件扫描功能-->
<context:component-scan base-package="com.demo"/>
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

5、测试
package com.demo.aop.AopTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.aop.service.IPersonService;

/**
* 测试使用AspectJ提供的注解切面
* @author wwl
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/context-aop1.xml" })
public class Aop1Test {
@Autowired
private IPersonService personService;
@Test
public void testAop(){
personService.doUpdate();
}
}

[size=large][b] 三、纯POJO切面(不使用注解,只在配置中注入)[/b][/size]
1、创建通知
package com.demo.aop.bean;
import org.springframework.stereotype.Component;
/**
* 通知
*
*/
@Component
public class Action1Advice{

public void afterAction(){
System.out.println("执行方法之后调用"+System.currentTimeMillis());
}
public void beforeAction(){
System.out.println("执行方法之前调用"+System.currentTimeMillis());

}

}

2、Service层
...........

3、Service实现层
...........

4、文件配置
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
<!-- @AspectJ注解 -->
<!-- 激活组件扫描功能 -->
<context:component-scan base-package="com.demo" />

<aop:config>
<aop:aspect ref="action1Advice">
<aop:before method="beforeAction" pointcut="execution(* *.doUpdate(..))" />
<aop:after method="afterAction" pointcut="execution(* *.doUpdate(..))" />
</aop:aspect>
</aop:config>
 <!--或者-->

<!--

  <aop:config>
    <aop:pointcut id="myPointcut" expression="execution(* *.doUpdate(..))"/>
    <aop:aspect id="action1Advice" ref="action1Advice" >
        <aop:before method="beforeAction" pointcut-ref="myPointcut"/>
        <aop:after method="afterAction" pointcut-ref="myPointcut"/>
    </aop:aspect>
  </aop:config>

-->


</beans>

5、测试
[quote]

...

总结:总体看其实两种方式,一种是使用代理,一种是使用AOP(是否使用注解)。

流程:先创建通知,创建切点,再将通知与切点绑定使用代理进行处理调用[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值