AOP
面向切面编程(不改变源代码的方式对功能进行增强) 有接口的情况,使用JDK动态代理 没有接口的情况,私用CGLIB动态代理
JDK动态代理
public interface Human {
void speak ( ) ;
int add ( int a, int b) ;
}
public class User implements Human {
@Override
public void speak ( ) {
System. out. println ( "人说话" ) ;
}
@Override
public int add ( int a, int b) {
return a+ b;
}
}
public class factoryProxy {
public static Object getProxyInstance ( Object o) {
MyinvocationHander myinvocationHander = new MyinvocationHander ( ) ;
myinvocationHander. setO ( o) ;
return Proxy. newProxyInstance ( o. getClass ( ) . getClassLoader ( ) , o. getClass ( ) . getInterfaces ( ) , myinvocationHander) ;
}
}
public class MyinvocationHander implements InvocationHandler {
private Object o;
public void setO ( Object o) {
this . o = o;
}
@Override
public Object invoke ( Object proxy, Method method, Object[ ] args) throws Throwable {
System. out. println ( "执行" + method. getName ( ) + "方法之前" ) ;
Object reslut = method. invoke ( o, args) ;
System. out. println ( "执行" + method. getName ( ) + "方法之后" ) ;
return reslut;
}
}
public class Test {
public static void main ( String[ ] args) {
Human human = ( Human) factoryProxy. getProxyInstance ( new User ( ) ) ;
human. speak ( ) ;
System. out. println ( human. add ( 1 , 2 ) ) ;
}
}
AOP术语
连接点(类中可以被增强的方法都成为连接点) 切入点(实际真正被增强的方法,成为切入点) 通知或增强(实际增强的逻辑部分称为通知) (前置,后置,环绕,异常,最终) 切面(是一个动作,把通知应用到切入点)
AOP (AspectJ注解)
public class User {
public void add ( ) {
System. out. println ( "add user" ) ;
}
}
public class UserPro {
public void before ( ) {
System. out. println ( "before" ) ;
}
}
3.进行通知的配置 3.1在Spring配置中开启注解扫描
<?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"
xmlns: aop= " http://www.springframework.org/schema/aop"
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.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: component-scan base-package = " annAspectJ" > </ context: component-scan>
</ beans>
@Component
public class User {
public void add ( ) {
System. out. println ( "add user" ) ;
}
}
@Component
public class UserPro {
public void before ( ) {
System. out. println ( "before" ) ;
}
}
@Aspect
@Component
public class UserPro {
public void before ( ) {
System. out. println ( "before" ) ;
}
}
<?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"
xmlns: aop= " http://www.springframework.org/schema/aop"
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.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: component-scan base-package = " annAspectJ" > </ context: component-scan>
< aop: aspectj-autoproxy> </ aop: aspectj-autoproxy>
</ beans>
@Order ( 1 )
@Aspect
@Component
public class UserPro {
@Pointcut ( value = "execution(* annAspectJ.User.add())" )
public void pointcut ( ) {
before ( ) ;
}
@Before ( value = "pointcut()" )
public void before ( ) {
System. out. println ( "before" ) ;
}
@After ( value = "execution(* annAspectJ.User.add())" )
public void after ( ) {
System. out. println ( "before" ) ;
}
@Around ( value = "execution(* annAspectJ.User.add())" )
public void around ( ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System. out. println ( "around before" ) ;
proceedingJoinPoint. proceed ( ) ;
System. out. println ( "around before" ) ;
}
@AfterThrowing ( value = "execution(* annAspectJ.User.add())" )
public void throwss ( ) {
System. out. println ( "error" ) ;
}
}
AOP (AspectJ配置)
<?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"
xmlns: aop= " http://www.springframework.org/schema/aop"
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.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
< bean id = " book" class = " xmlAspectJ.Book" > </ bean>
< bean id = " bookpro" class = " xmlAspectJ.BookPro" > </ bean>
< aop: config>
< aop: pointcut id = " p" expression = " execution(* xmlAspectJ.Book.speak(..))" />
< aop: aspect ref = " bookpro" >
< aop: before method = " before" pointcut-ref = " p" />
</ aop: aspect>
</ aop: config>
</ beans>
public class Book {
public void speak ( ) {
System. out. println ( "book speak" ) ;
}
}
public class BookPro {
public void before ( ) {
System. out. println ( "before" ) ;
}
}
public class Test2 {
public static void main ( String[ ] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "XMl/bean2.xml" ) ;
Book user = applicationContext. getBean ( "book" , Book. class ) ;
user. speak ( ) ;
}
}
完全注解AOP
@Configuration
@ComponentScan ( basePackages = { "xxx" } )
@EnableAspectJAutoProxy ( proxyTargetClass = true )
public class SpringConfig ( ) { }