一、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p">
<!--4.3 自动创建代理-->
<bean id="helloService" class="test.spring.proxyServices.Impl.GreetServiceImpl"/>
<bean id="helloTarget" class="test.spring.proxyServices.Impl.HelloImpl"/>
<!--定义通知-->
<bean id="helloAdvice" class="test.spring.proxy.GreetServiceBeforeAdvice"/>
<!--1. BeanNameAutoProxyCreator 根据Bean名称创建代理-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Service"/>
<property name="interceptorNames" value="helloAdvice"/>
</bean>
<!--2. DefaultAdvisorAutoProxyCreator 根据Advisor本身包含信息创建代理,即匹配名中有say的方法-->
<bean id="regeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="helloAdvice"/>
<property name="patterns" value=".*say.*"/>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
</beans>
二、代码
1.HelloImpl&GreetServiceImpl
package test.spring.proxyServices.Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import test.spring.proxyServices.IUserDao;
import javax.annotation.Resource;
public class HelloImpl {
@Autowired
@Qualifier(value = "userDao1")
private test.spring.proxyServices.IUserDao userDao;
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
public void sayHello(String name) {
System.out.println("hello:" + name);
int sum = userDao.getNum(); //sum = name.length();
// return sum;
}
}
package test.spring.proxyServices.Impl;
import test.spring.proxyServices.GreetService;
/*实现类*/
public class GreetServiceImpl implements GreetService {
public String greet(String name) {
System.out.println("Helle,"+name);
return name;
}
public String greet1() {
System.out.println("hello,Spring");
return "spring";
}
}
2.GreetServiceBeforeAdvice---spring AOP
package test.spring.proxy;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
/*通知类*/
public class GreetServiceBeforeAdvice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor,ThrowsAdvice {
/*前置通知*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(method);
System.out.println(args);
System.out.println(target);
System.out.println("方法执行之前执行的内容。。。");
}
/*后置通知*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method);
System.out.println(args);
System.out.println(target);
System.out.println("方法执行之后执行的内容。。。");
}
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println(invocation);
System.out.println("方法执行之前");
invocation.proceed();
System.out.println("方法执行之后");
return null;
}
public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
System.out.println("系统发生异常。。");
System.out.println(ex.getMessage());
}
}
3.test
package test.spring.Controller.proxy;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.proxyServices.GreetService;
import test.spring.proxyServices.IUserDao;
import test.spring.proxyServices.Impl.HelloImpl;
import test.spring.proxyServices.Impl.UserDao1;
public class AutoCreateProxyAction {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
GreetService greetService = (GreetService) applicationContext.getBean("helloService");
greetService.greet("world!");
HelloImpl hello = (HelloImpl)applicationContext.getBean("helloHI");
IUserDao userDao = new UserDao1();
hello.setUserDao(userDao);
// System.out.println(hello.sayHello("jing"));
hello.sayHello("小红");
}
}
【补充】
sayHello(String name)这个方法有返回值会报异常:org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public int test.spring.proxyServices.Impl.HelloImpl.sayHello(java.lang.String)。没有返回值的情况下代码可运行。原因待查。