项目依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ssw</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.6</version> </dependency> </dependencies> </project>
创建OrderDao类:
package org.example.dao; public class OrderDao { public void add() { System.out.println("正在执行 UserDao 的 add() 方法……"); } public void adds() { System.out.println("正在执行 UserDao 的 adds() 方法……"); } public void delete() { System.out.println("正在执行 UserDao 的 delete() 方法……"); } public void modify() { System.out.println("正在执行 UserDao 的 modify() 方法……"); } public void get() { System.out.println("正在执行 UserDao 的 get() 方法……"); } }
创建环绕增强类,OrderDaoAroundAdvice :
package org.example.advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 增强代码 * 环绕增强 * * @author c语言中文网 c.biancheng.net */ public class OrderDaoAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("环绕增强前********"); //执行被代理对象中的逻辑 Object result = methodInvocation.proceed(); System.out.println("环绕增强后********"); return result; } }
修改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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--带切点的切面--> <!-- 定义目标(target)对象 --> <bean id="orderDao" class="org.example.dao.OrderDao"></bean> <!-- 定义增强 --> <bean id="aroundAdvice" class="org.example.advice.OrderDaoAroundAdvice"></bean> <!--定义切面--> <bean id="myPointCutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!--定义表达式,规定哪些方法进行拦截 .* 表示所有方法--> <!--<property name="pattern" value=".*"></property>--> <property name="patterns" value="org.example.dao.OrderDao.add.*,org.example.dao.OrderDao.delete.*"></property> <property name="advice" ref="aroundAdvice"></property> </bean> <!--Spring 通过配置生成代理--> <bean id="orderDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 配置目标 --> <property name="target" ref="orderDao"></property> <!-- 针对类的代理,该属性默认取值为 false(可省略), 表示使用 JDK 动态代理;取值为 true,表示使用 CGlib 动态代理--> <property name="proxyTargetClass" value="true"></property> <!-- 在目标上应用增强 --> <property name="interceptorNames" value="myPointCutAdvisor"></property> </bean> </beans>
执行:
package org.example; import org.example.dao.OrderDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //获取 ApplicationContext 容器 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); //获取代理对象 OrderDao orderDao = context.getBean("orderDaoProxy", OrderDao.class); //调用 OrderDao 中的各个方法 orderDao.add(); orderDao.adds(); orderDao.delete(); orderDao.get(); orderDao.modify(); } }
==================================================================================================
标签:orderDao,spring,System,PointcutAdvisor,AOP,println,org,public,out From: https://www.cnblogs.com/xiaobaibailongma/p/16973653.html