------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------
Name Auto Proxy generator: Beannameautoproxycreator
For better testing, I put two interfaces, two implementation classes:
Isomeservice Interface:
Package cn.dawn.day18auto02; /* */Publicinterface isomeservice {publicvoid Insert (); Public void Delete (); Public void Select (); Public void update ();}
Its implementation class: Someserviceimpl
Package cn.dawn.day18auto02;/** * Created by Dawn on 2018/3/8.*/ Public classSomeserviceimpl implements Isomeservice { Public voidInsert () {System. out. println ("Insert OK"); } Public voidDelete () {System. out. println ("Delete OK"); } Public void Select() {System. out. println ("Select OK"); } Public voidUpdate () {System. out. println ("Update OK"); }}
Ibookservice Interface:
Package cn.dawn.day18auto02; /* */Publicinterface ibookservice {publicvoid selectallbooks ();}
Its implementation class: Bookserviceimpl
Package cn.dawn.day18auto02; /* */Publicclass Bookserviceimpl implements Ibookservice { publicvoid selectallbooks () { System. out. println ("selectbooks OK");} }
An enhanced
package cn.dawn.day18auto02;import org.springframework.aop.MethodBeforeAdvice; Import Java.lang.reflect.Method; /* * * Created by Dawn on 2018/3/5. */ /* pre-enhancement */ public class Loggerbefore implements Methodbeforeadvice { Public void before (method, object[] objects, Object o) throws Throwable {System. out . println ( " logging "
In the XML 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/context http://www.springframework.org/schema/context/spring-context.xsd"><!--objects to enhance--<bean id="Service" class="Cn.dawn.day18auto02.SomeServiceImpl"></bean> <bean id="Bookservice" class="Cn.dawn.day18auto02.BookServiceImpl"></bean> <!--enhanced Content-<bean id="Myadvice" class="Cn.dawn.day18auto02.LoggerBefore"></bean> <!--Advisor--<bean id="Myadvisor" class="Org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="Advice" ref="Myadvice"></property> <!--<property name="Mappednames"Value="do*"></property>--> <!--<property name="pattern"Value=". *do.*"></property>--> <property name="Patterns"Value=". *e.*"></property> </bean> <!--name Auto Agent generator-<beanclass="Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="Beannames"Value="Service,bookservice"></property> <property name="Interceptornames"Value="Myadvisor"></property> </bean></beans>
There is no consultant here, the value of Beannames in the name Auto Agent generator Beannameautoproxycreator is an array of type string, so you can put multiple target objects, separated by commas.
Single test method:
Package Cn.dawn.day18auto02;import Org.junit.test;import org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Dawn on 2018/3/3.*/ Public classtest20180312 {@Test/*AOP Agent Factory bean Exception Enhancement*/ Public voidt01 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext-day18auto02.xml"); Isomeservice Service= (Isomeservice) Context.getbean ("Service"); Ibookservice Bookservice= (Ibookservice) Context.getbean ("Bookservice"); Service.Select(); Bookservice.selectallbooks (); }}
ssm-spring-15:spring name Auto Agent generator Beannameautoproxycreator