Spring(九)Spring中的兩種自動代理
阿新 • • 發佈:2018-03-13
創建 for 工廠 update lns 實體 dem create path
這裏說的自動代理說的是自動代理bean對象,就是說在xml中不用再配置代理工廠,就可以自動代理
下面來說第一種
第一種自動代理:默認自動代理生成器(DefaultAdvisorAutoProxyCreator)
第一步:定義接口,在接口中聲明幾個方法
package demo17; /** * Created by mycom on 2018/3/8. */ public interface ISomeService { public void select(); public void insert(); public void delete(); publicvoid update(); }
第二步:編寫實現類,重寫接口中的方法
package demo17; /** * Created by mycom on 2018/3/8. */ public class SomeServiceImpl implements ISomeService { public void select() { System.out.println("select ok!"); } public void insert() { } public void delete() { }public void update() { } }
第三步:寫一個前置增強
package demo17; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /** * Created by mycom on 2018/3/12. */ public class BeforeAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("before======="); } }
第四步:配置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" xmlns:p="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--1.目標對象--> <bean id="service" class="demo17.SomeServiceImpl"></bean> <!--通知--> <bean id="beforeAdvice" class="demo17.BeforeAdvice"></bean> <!--顧問--> <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="patterns" value=".*e.*"></property> </bean> <!--默認自動代理--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean> </beans>
最後一步:編寫測試方法,進行單測
//默認自動代理 @Test public void t1() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextauto01.xml"); ISomeService service =(ISomeService) context.getBean("service"); service.select(); }
運行結果如下:
第二種:名稱自動代理生成器(BeanNameAutoProxyCreator)
步驟和上述的一樣,在這裏我就直接用上一種的接口和實現類了,xml中的配置有所不同
這裏我有創建了一個接口和實現類,如果要配置多個對象,應該怎麽配?
新創建的接口和實體類
package demo17; /** * Created by mycom on 2018/3/12. */ public interface IBookService { public void select(); }
package demo17; /** * Created by mycom on 2018/3/12. */ public class BookServiceImpl implements IBookService { public void select() { System.out.println("Book select"); } }
配置文件中
<?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" xmlns:p="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--1.目標對象--> <bean id="service" class="demo17.SomeServiceImpl"></bean> <bean id="bookService" class="demo17.BookServiceImpl"></bean> <!--通知--> <bean id="beforeAdvice" class="demo17.BeforeAdvice"></bean> <!--顧問--> <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="patterns" value=".*e.*"></property> </bean> <!--名稱自動代理--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="service,bookService"></property> <property name="interceptorNames" value="advisor"></property> </bean> </beans>
編寫測試類
//名稱自動代理 @Test public void t2() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextauto02.xml"); ISomeService service =(ISomeService) context.getBean("service"); service.select(); IBookService bookService =(IBookService) context.getBean("bookService"); bookService.select(); }
運行結果
Spring(九)Spring中的兩種自動代理