spring学习之aop(FactoryBean代理)

实验结构:

spring学习之aop(FactoryBean代理)

MyAspect.java

 1 package com.itheima.factorybean;
 2 
 3 import org.aopalliance.intercept.MethodInterceptor;
 4 import org.aopalliance.intercept.MethodInvocation;
 5 
 6 //切面类
 7 public class MyAspect implements MethodInterceptor {
 8 
 9     @Override
10     public Object invoke(MethodInvocation arg0) throws Throwable {
11         // TODO Auto-generated method stub
12         
13         //前增强
14         check_permission();
15         //执行目标方法
16         Object object=arg0.proceed();
17         //后增强
18         log();
19     
20         return object;
21     }
22 
23     public void check_permission() {
24         // TODO Auto-generated method stub
25         
26         System.out.println("模拟检查权限");
27     }
28     public void log() {
29         // TODO Auto-generated method stub
30         
31         System.out.println("模拟日志");
32     }
33 }

applicationContext..xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
 6      <!-- 目标类 -->
 7     <bean id="userDao" class="com.itheima.jdk.UserDaoImpl"></bean>
 8     <!-- 切面类 -->
 9     <bean id="myAspect" class="com.itheima.factorybean.MyAspect"></bean>
10     <!-- 使用spring代理工厂定义一个名为userDaoProxy的代理对象 -->
11     <bean id="userDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
12         <property name="proxyInterfaces" value="com.itheima.jdk.UserDao"></property>
13         <property name="target" ref="userDao"></property>
14         <property name="interceptorNames" value="myAspect"></property>
15         <property name="proxyTargetClass" value="true"></property>
16     </bean>
17 <!--     指定代理实现的接口
18     指定目标对象
19     指定切面,织入环绕通知
20     指定代理方式,true:使用cglib,false(默认):使用jdk动态代理 -->
21     
22 </beans>

ProxyFactoryBeanTest.java

 1 package com.itheima.factorybean;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.itheima.jdk.UserDao;
 7 
 8 public class ProxyFactoryBeanTest {
 9 
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         String xmlpathString="com/itheima/factorybean/applicationContext.xml";
13         ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpathString);
14         //从spring容器中获得内容
15         UserDao userDao=(UserDao)applicationContext.getBean("userDaoProxy");
16         userDao.addUser();
17         userDao.deleteUser();
18     }
19 
20 }

截图:

spring学习之aop(FactoryBean代理)

 

上一篇:linux内核hook技术之指令覆盖与注入


下一篇:Caffe-DeepBinaryCode的安装和使用