AOP:Aspect-Oriented-Programming(面向切面编程),一种编程思想。
是为了解决编码中的代码冗余问题,解决项目维护存在极大隐患。
动态代理优点:
- 通过动态字节码技术,在运行时动态生成代理( 反射 )。
- 则既不用维护代理类,有可以有代码打理额外功能。
编码流程:
首先在pom.xml导入依赖:
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!--springAop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
编写applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!--与以往不同的是额外导入了aop的包↑ xmlns: 可以随意起名但是为了方便识别取aop-->
<!--要追加功能的 目标-->
<bean id="userService" class="com.hs.service.UserserviceImpl"></bean>
<!--想添加的 额外功能:Advice-->
<bean id="before04" class="com.hs.advice.BeforeAdvice"></bean>
<bean id="after04" class="com.hs.advice.MaAfterAdvice"></bean>
<bean id="throws" class="com.hs.advice.Throws"></bean>
<bean id="inter" class="com.hs.advice.Interceptor"></bean>
<!--编织
内容:
通过目标信息,额外功能的信息,组件一个新的类 :Proxy
-->
<!--aop是上面的别名-->
<aop:config>
<!-- 切入点:目标中的方法
execution表达式
execution(修饰符,返回值,包,类,方法名(参数表))
execution( public integer com.hs.service.userserviceimpl.queryuser(integer))
execution(* com.hs.service.userserviceimpl.*(..))
-->
<!--引入切入点-->
<aop:pointcut id="pc04" expression="execution(* com.hs.service.UserserviceImpl.*(..))"/>
<!-- 将某个额外功能,编织到某些切入点中-->
<aop:advisor advice-ref="before04" pointcut-ref="pc04"/>
<aop:advisor advice-ref="after04" pointcut-ref="pc04"/>
<aop:advisor advice-ref="inter" pointcut-ref="pc04"/>
<aop:advisor advice-ref="throws" pointcut-ref="pc04"></aop:advisor>
</aop:config>
</beans>
下面是随手写的测试用Serviceimpl:
public class UserserviceImpl implements UserService {
@Override
public Integer insertUser(User user) {
System.out.println("核心insertuser");
return 4;
}
@Override
public User queryOne(Integer id) {
System.out.println("核心quertone");
return new User();
}
}
最后则是在advice包中的想要追加的功能
这里有多种Adivce:
- 前置额外功能
- 后置额外功能
- 环绕额外功能
- 异常额外功能
//前置
public class MyBeforeAdvice implements MethodBeforeAdvice{
/**
* @param method 当前执行的方法
* @param args 当前执行的方法中的参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("before------ method:"+method.getName()+" args:"+args.length+" target:"+target);
}
}
//后置
public class MyAfterAdvice implements AfterReturningAdvice{
/**
*
* @param returnValue 目标业务方法返回值
* @param method 当前执行的业务方法对象
* @param args 方法的参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("After-------"+returnValue+method.getName());
}
}
//环绕
public class MyMethodInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("begin tx----");
Object proceed = invocation.proceed();//执行目标核心功能
System.out.println("end tx------");
return proceed;//返回目标业务方法返回值
}
}
//异常
public class MyThrows implements ThrowsAdvice{
//目标业务方法中抛出异常时,执行此方法。ex=抛出的异常对象
public void afterThrowing(Exception ex){
System.out.println(ex.getMessage()+"出现异常~~~");
}
}
最后是测试类:
@Test
public void testporxy(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理对象
UserService userServiceProxy = (UserService) context.getBean("userService");
userServiceProxy.queryOne(1);
userServiceProxy.insertUser(new User());
}
最终运行结果为: