spring瀛︿範涔媋op(FactoryBean浠g悊)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring瀛︿範涔媋op(FactoryBean浠g悊)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/static' title='static'>static user 缁撴瀯 idt info names spring瀹瑰櫒 aspect 鎸囧畾
瀹為獙缁撴瀯锛?/p>
MyAspect.java
1 package com.itheima.factorybean; 2 3 import org.aopalliance.intercept.MethodInterceptor; 4 import org.aopalliance.intercept.MethodInvocation; 5 6 //鍒囬潰绫?/span> 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 //鍓嶅寮?/span> 14 check_permission(); 15 //鎵ц鐩爣鏂规硶 16 Object object=arg0.proceed(); 17 //鍚庡寮?/span> 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浠g悊宸ュ巶瀹氫箟涓€涓悕涓簎serDaoProxy鐨勪唬鐞嗗璞?--> 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 <!-- 鎸囧畾浠g悊瀹炵幇鐨勬帴鍙? 18 鎸囧畾鐩爣瀵硅薄 19 鎸囧畾鍒囬潰锛岀粐鍏ョ幆缁曢€氱煡 20 鎸囧畾浠g悊鏂瑰紡锛宼rue锛氫娇鐢╟glib锛宖alse(榛樿)锛氫娇鐢╦dk鍔ㄦ€佷唬鐞?--> 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 //浠巗pring瀹瑰櫒涓幏寰楀唴瀹?/span> 15 UserDao userDao=(UserDao)applicationContext.getBean("userDaoProxy"); 16 userDao.addUser(); 17 userDao.deleteUser(); 18 19 20
鎴浘锛?/p>
以上是关于spring瀛︿範涔媋op(FactoryBean浠g悊)的主要内容,如果未能解决你的问题,请参考以下文章