Spring aop 编程
2017-03-30 15:12
253 查看
一.手动aop编程
接口:
目标类(实现类)
applicationContext.xml配置
测试:
AOP全自动配置:
先添加Aspectj.weaver 包
applicationContext1.xml配置
测试:
接口:
/** * 接口 * */ public interface UserService { public void addUser(); public void saveUser(); }
目标类(实现类)
public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("添加用户"); } @Override public void saveUser() { // TODO Auto-generated method stub System.out.println("保存用户"); } }通知:
/** * 继承aop联盟 * */ public class MyAuAspect implements MethodInterceptor{ @Override public Object invoke(MethodInvocation invocation) throws Throwable { // TODO Auto-generated method stub System.out.println("开始执行"); Object obj=invocation.proceed(); System.out.println("执行结束"); return obj; } }
applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--切面类 --> <bean id="myAuAspectId" class="com.study.aop.MyAuAspect"></bean> <!--目标类 --> <bean id="userServiceId" class="com.study.poxy.UserServiceImpl"></bean> <!--创建代理对象 --> <bean id="poxyServiceId" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--在之前我们学习jdk的动态代理和CGLIB的动态代理的时候,要传输接口,目标对象,方法的回调等参数,所以我们现在也要设置这些参数 --> <property name="interfaces" value="com.study.poxy.UserService"></property> <property name="target" ref="userServiceId"></property> <property name="interceptorNames" value="myAuAspectId"></property> </bean> </beans>
测试:
@Test public void Test(){ String xmlPath="com/study/test/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserService userService = (UserService) applicationContext.getBean("poxyServiceId"); userService.addUser(); }
AOP全自动配置:
先添加Aspectj.weaver 包
<!--aspectj.weaver配置 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency>
applicationContext1.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--切面类 --> <bean id="myAuAspectId" class="com.study.aop.MyAuAspect"></bean> <!--目标类 --> <bean id="userServiceId" class="com.study.poxy.UserServiceImpl"></bean> <!-- aop配置: 使用<aop:config>进行配置 proxy-target-class表示是否使用CGLIB动态代理 <aop:pointcut>设置切点,就是表明那些方法会被拦截,使用其代理 expression表达式说明: execution(* com.itheima.c_spring_aop.*.*(..)) * com.itheima.c_spring_aop.*.*(..) 分别表示如下: 返回值, 包 类,方法,参数 *表示任意 <aop:advisor> 设置切面,包括通知和切点 --> <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* com.study.poxy.*.*(..))" id="pointCutId"/> <aop:advisor advice-ref="myAuAspectId" pointcut-ref="pointCutId"/> </aop:config> </beans>
测试:
@Test public void Test(){ String xmlPath="com/study/test/applicationContext1.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserService userService = (UserService) applicationContext.getBean("userServiceId"); userService.addUser(); }
相关文章推荐
- spring aop 2.0 编程
- spring 2.0 aop编程(一)
- 注释方式切面编程(Spring AOP技术)
- Spring AOP:使用NameMatchMethodPointcutAdvisor实现切面编程
- 【我看Spring】从一个简单的AOP示例看切面编程
- Spring AOP: Spring之面向方面编程
- 使用Spring进行切面(AOP)编程
- spring aop 2.0 编程
- Spring AOP面向方面编程原理:AOP概念
- Spring之面向方面编程(AOP)
- spring aop编程(二)
- spring aop编程与ssh集成的奇怪异常问题解决办法
- Spring 2.0中 AOP的编程
- spring aop 面向切面编程 如何来做一个强大的日志记录功能
- Spring AOP面向方面编程原理:AOP概念
- spring aop 2.0 编程(二)一个实例
- spring aop 2.0 编程(二)一个实例
- Spring的面向方面编程(Aop)
- Spring面向切面编程AOP的个人理解
- 使用Spring进行切面(AOP)编程