创建maven项目:
在pom中添加依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- spring aop支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- aspectj支持 注解的依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
实体类:
package com.test.test2;
import org.springframework.stereotype.Component;
@Component
public class Client {
private String name;
private int money;
public void maidongxiClient() {
// TODO Auto-generated method stub
System.out.println("2222客户买东西");
}
public void maiClient() {
System.out.println("卖客户2");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
通知类:
package com.test.test2;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;
//通知对象,实现环绕通知接口
@Component
public class TongZhi implements MethodInterceptor, MethodBeforeAdvice, AfterReturningAdvice {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("执行前2");
invocation.proceed();// 执行原有逻辑
System.out.println("执行后2");
return null;
}
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行后1");
}
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行前1");
}
}
spring bean 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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.test.test2" />
<!-- 代理bean工厂中完成织入 -->
<bean id="proxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="client" /><!-- 需要代理的对象,也是spring容器的对象 -->
<property name="interceptorNames">
<list>
<value>tongZhi</value><!-- 通知对象,Spring容器中的对象,对象需要实现通知类型。 -->
<!-- 这种方法的缺点:需要代理的对象中,所有的方法都会被代理(无论是否需要代理),都会被代理 -->
</list>
</property>
</bean>
</beans>
调用类:
package com.test.test2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test4 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("app2.xml");
Client client = (Client) context.getBean("proxy");
client.maidongxiClient();
client.maiClient();
}
}
自动代理配置bean xml config:
<?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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.test.test2" />
<!-- 代理bean工厂中完成织入 -->
<bean id="proxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="client" /><!-- 需要代理的对象,也是spring容器的对象 -->
<property name="interceptorNames">
<list>
<value>Regexp</value><!-- 通知对象,Spring容器中的对象,对象需要实现通知类型。 -->
<!-- 这种方法的缺点:需要代理的对象中,所有的方法都会被代理(无论是否需要代理),都会被代理 -->
</list>
</property>
</bean>
<bean id="Regexp" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern">
<value>com.test.test2.Client.maidongxiClient</value><!-- 指定包名,类型,方法名 -->
</property>
<property name="advice" ref="tongZhi" />
</bean>
</beans>