一、什么是AOP
1、将复杂的需求分解出不同的方面,将散布在系统汇中的公共功能集中解决;
2、采用代理机制组装起来运行,再不改变原程序的基础上对代码段进行增强处理,增加新的功能;
3、默认采取的是JDK动态代理,面向接口编程的动态代理。
二、案例
接下来我们通过具体案例对AOP进一步了解
1、创建UserInfo实体类
package com.spring.entity; public class UserInfo { private Integer u_id; private String u_name; public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_name() { return u_name; } public void setU_name(String u_name) { this.u_name = u_name; } }
2、创建IUserMapper接口,定义一个添加方法
package com.spring.mapper; import com.spring.entity.UserInfo; /** * Dao层接口 */ public interface IUserMapper { //添加用户 public int addUser(UserInfo info); }
创建IUserMapperImpl实现层
package com.spring.mapper.impl; import com.spring.entity.UserInfo; import com.spring.mapper.IUserMapper; public class IUserMapperImpl implements IUserMapper { @Override public int addUser(UserInfo info) { System.out.println("add success"); return 0; } }
3、创建相对应的IUserMapperService接口,创建相同的方法
package com.spring.service; import com.spring.entity.UserInfo; public interface IUserMapperService { //添加用户 public int addUser(UserInfo info); }
4、创建Service的实现类IUserMapperServiceImpl类
package com.spring.service.impl; import com.spring.entity.UserInfo; import com.spring.mapper.IUserMapper; import com.spring.service.IUserMapperService; public class IUserMapperServiceImpl implements IUserMapperService { //注入Dao层对象 private IUserMapper iUserMapper; public IUserMapper getiUserMapper() { return iUserMapper; } public void setiUserMapper(IUserMapper iUserMapper) { this.iUserMapper = iUserMapper; } @Override public int addUser(UserInfo info) { int count = iUserMapper.addUser(info); return count; } }
5、在pom.xml中注入实现AOP的依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency>
6、创建advice增强类MyBeforeAdvice
//MethodBeforeAdvice表示前置增强,在执行逻辑代码之前执行的操作 //AfterReturningAdvice表示后置增强,在逻辑代码执行完毕之后进行的操作
package com.spring.advice; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class MyBeforeAdvice implements MethodBeforeAdvice, AfterReturningAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==============前置增强,日志处理等操作==============="); } @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("==============后置操作,事务处理等操作================"); } }
7、配置applicationContextUser.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" 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"> <!--声明DaoBean bean的注入的都是实现类--> <bean id="iUserMapper" class="com.spring.mapper.impl.IUserMapperImpl"></bean> <!--声明Service class:对应的是你的Service实现类--> <bean id="iUserMapperService" class="com.spring.service.impl.IUserMapperServiceImpl"> <!--setter方法怎么注入:找到Name属性值,将属性值的开头改为大写,然后前缀加上setIUserMapper--> <property name="iUserMapper" ref="iUserMapper"></property> </bean> <!--切面:对应增强类--> <bean id="myBeforeAdvice" class="com.spring.advice.MyBeforeAdvice"></bean> <!--增强处理--> <aop:config> <!--切点:你要对哪一个方法进行增强 --> <aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"/> <!--织入:将增强处理和切点表达式符合的方法关联到一起--> <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pointcut"></aop:advisor> </aop:config> </beans>
8、Test测试类
package com.spring.Aop; import com.spring.entity.Student; import com.spring.entity.UserInfo; import com.spring.service.IUserMapperService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { public static void main(String[] args) { //加载applicationContext.xml文件 ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContextUser.xml"); IUserMapperService user = (IUserMapperService)cxt.getBean("iUserMapperService"); user.addUser(new UserInfo()); } }
9、执行结果如下
原文地址:https://www.cnblogs.com/tinghao/p/11747108.html
时间: 2024-11-09 20:06:51