The automatic agent said here is the automatic Agent Bean object, that is, in XML no longer configure the Agent factory, you can automatically proxy
The first of these
First automatic proxy: Default auto Proxy generator (defaultadvisorautoproxycreator)
First step: Define the interface and declare several methods in the interface
Package demo17; /* */Publicinterface isomeservice {publicvoid Select (); Public void Insert (); Public void Delete (); Public void update ();}
Step Two: Write the implementation class, overriding the methods in the interface
Package demo17;/** * Created by mycom on 2018/3/8.*/ Public classSomeserviceimpl implements Isomeservice { Public void Select() {System. out. println ("Select ok!"); } Public voidInsert () {} Public voidDelete () {} Public voidupdate () {}}
Step three: Write a front-facing enhancement
Package demo17;import Org.springframework.aop.methodbeforeadvice;import Java.lang.reflect.Method; /* */Publicclass Beforeadvice implements Methodbeforeadvice { publicvoid before (method method, object[] args, Object target) throws Throwable { System. out. println ("before=======");} }
Fourth Step: Configure the XML file
<?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/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd"><!--1. Target objects--<bean id="Service" class="demo17. Someserviceimpl"></bean> <!--notifications-<bean id="Beforeadvice" class="demo17. Beforeadvice"></bean> <!--Advisor--<bean id="Advisor" class="Org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="Advice" ref="Beforeadvice"></property> <property name="Patterns"Value=". *e.*"></property> </bean> <!--default Auto-proxy-<beanclass="Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean></beans>
The final step: Write a test method, do a single measurement
// Default Auto Proxy @Test publicvoid T1 () { new Classpathxmlapplicationcontext ("applicationcontextauto01.xml"); = (Isomeservice) context.getbean ("service"); Service. Select (); }
The results of the operation are as follows:
The second type: Name Auto proxy generator (beannameautoproxycreator)
Step is the same as above, here I directly use the interface and implementation class, XML configuration is different
Here I have created an interface and implementation class, if you want to configure multiple objects, how should you match?
Newly created interfaces and entity classes
Package demo17; /* */Publicinterface ibookservice {publicvoid Select ();}
Package demo17; /* */Publicclass Bookserviceimpl implements Ibookservice { publicvoidSelect() { System. out. println ("bookSelect");} }
In the configuration file
<?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/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd"><!--1. Target objects--<bean id="Service" class="demo17. Someserviceimpl"></bean> <bean id="Bookservice" class="demo17. Bookserviceimpl"></bean> <!--notifications-<bean id="Beforeadvice" class="demo17. Beforeadvice"></bean> <!--Advisor--<bean id="Advisor" class="Org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="Advice" ref="Beforeadvice"></property> <property name="Patterns"Value=". *e.*"></property> </bean> <!--name Auto-proxy-<beanclass="Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="Beannames"Value="Service,bookservice"></property> <property name="Interceptornames"Value="Advisor"></property> </bean></beans>
Writing test Classes
//Name Auto Proxy@Test Public voidT2 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontextauto02.xml"); Isomeservice Service= (Isomeservice) Context.getbean ("Service"); Service.Select(); Ibookservice Bookservice= (Ibookservice) Context.getbean ("Bookservice"); Bookservice.Select(); }
Run results
Spring (ix) Two kinds of automatic proxies in spring