------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
默认自动代理DefaultAdvisorAutoProxyCreator
本处没有什么要讲的,放原代码
ISomeService接口:
package cn.dawn.day17atuo01; /**
* Created by Dawn on 2018/3/8.
*/
public interface ISomeService {
public void insert();
public void delete();
public void select();
public void update();
}
SomeServiceImpl类继承上面的那个接口:
package cn.dawn.day17atuo01; /**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl implements ISomeService{ public void insert() {
System.out.println("insert ok");
} public void delete() {
System.out.println("delete ok"); } public void select() {
System.out.println("select ok"); } public void update() {
System.out.println("update ok"); }
}
LoggerBefore类,做了前置增强
package cn.dawn.day17atuo01; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /**
* Created by Dawn on 2018/3/5.
*/
/*前置增强*/
public class LoggerBefore implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("日志记录");
}
}
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">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day17atuo01.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="myadvice" class="cn.dawn.day17atuo01.LoggerBefore"></bean>
<!--顾问-->
<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=".*select.*"></property>
</bean>
<!--默认自动代理-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean> </beans>
必须要有顾问,没有不可以,默认自动代理里面不用实现参数,他自动匹配
单测方法:
package cn.dawn.day17auto01; import cn.dawn.day17atuo01.ISomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180312 {
@Test
/*默认自动代理*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day17auto01.xml");
ISomeService service = (ISomeService) context.getBean("service"); service.select(); }
}