使用Spring实现AOP
- 项目结构
使用AOP,需要导入一个依赖包!
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
- AfterLog
package com.cloud.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//返回值为:returnValue
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName()+"方法,返回结果为:"+returnValue);
}
}
- Log
package com.cloud.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
- UserService
package com.cloud.service;
public interface UserService {
void add();
void delete();
void update();
void select();
}
- UserServicelmpl
package com.cloud.service;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加一个数据");
}
public void delete() {
System.out.println("删除一个数据");
}
public void update() {
System.out.println("修改一个数据");
}
public void select() {
System.out.println("查询一个数据");
}
}
- applicationContext.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"
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">
<!--注册bean-->
<bean id="afterlog" class="com.cloud.log.AfterLog"/>
<bean id="log" class="com.cloud.log.Log"/>
<bean id="service" class="com.cloud.service.UserServiceImpl"/>
<!--配置:aop:需要导入aop的约束-->
<!--方式一:使用原生Spring API接口-->
<aop:config>
<!--切入点:expression:表达式expression(要执行的位置!******),-->
<aop:pointcut id="pointcut" expression="execution(* com.cloud.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
*测试类
import com.cloud.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service =(UserService) context.getBean("service");
service.add();
}
}
自定义实现Spring AOP
- 项目结构
- Diy
package com.cloud.diy;
public class Diy {
public void before(){
System.out.println("=======");
}
public void after(){
System.out.println("=======");
}
}
- applicationContext.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"
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">
<!--注册bean-->
<bean id="afterlog" class="com.cloud.log.AfterLog"/>
<bean id="log" class="com.cloud.log.Log"/>
<bean id="service" class="com.cloud.service.UserServiceImpl"/>
<bean id="diy" class="com.cloud.diy.Diy"/>
<!--配置:aop:需要导入aop的约束-->
<!--方式一:使用原生Spring API接口-->
<!--<aop:config>-->
<!--<!–切入点:expression:表达式expression(要执行的位置!******),–>-->
<!--<aop:pointcut id="pointcut" expression="execution(* com.cloud.service.UserServiceImpl.*(..))"/>-->
<!--<!–执行环绕增加–>-->
<!--<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!--<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>-->
<!--</aop:config>-->
<!--方法二:自定义类-->
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.cloud.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:after method="after" pointcut-ref="point"/>
<aop:before method="before" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
我个人觉的这个相对好玩点可以自定义写个类然后去切入
注解实现SpringAOP
- applicationContext.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"
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">
<!--注册bean-->
<bean id="afterlog" class="com.cloud.log.AfterLog"/>
<bean id="log" class="com.cloud.log.Log"/>
<bean id="service" class="com.cloud.service.UserServiceImpl"/>
<bean id="diy" class="com.cloud.diy.Diy"/>
<bean id="annotationPointCut" class="com.cloud.diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
<!--配置:aop:需要导入aop的约束-->
<!--方式一:使用原生Spring API接口-->
<!--<aop:config>-->
<!--<!–切入点:expression:表达式expression(要执行的位置!******),–>-->
<!--<aop:pointcut id="pointcut" expression="execution(* com.cloud.service.UserServiceImpl.*(..))"/>-->
<!--<!–执行环绕增加–>-->
<!--<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!--<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>-->
<!--</aop:config>-->
<!--方法二:自定义类-->
<!--<aop:config>-->
<!--<!–自定义切面,ref要引用的类–>-->
<!--<aop:aspect ref="diy">-->
<!--<!–切入点–>-->
<!--<aop:pointcut id="point" expression="execution(* com.cloud.service.UserServiceImpl.*(..))"/>-->
<!--<!–通知–>-->
<!--<aop:after method="after" pointcut-ref="point"/>-->
<!--<aop:before method="before" pointcut-ref="point"/>-->
<!--</aop:aspect>-->
<!--</aop:config>-->
</beans>
- AnnotationPointCut
package com.cloud.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect//标注这个类是个切面
public class AnnotationPointCut {
@Before("execution(* com.cloud.service.UserServiceImpl.*(..))")
public void T1(){
System.out.println("王狗蛋");
}
@After("execution(* com.cloud.service.UserServiceImpl.*(..))")
public void T2(){
System.out.println("陈狗蛋");
}
}
总结:Spring里面的AOP呢就是用xml文件代替你做了这个代理不管是静态代理也好还是动态代理也好接口和真实对象要做的事情呢都是去做一个业务的镶嵌,Spring去做呢把代理的过程拿到了xml文件中去做去拿到上面的类在写好要增加的业务然后在找个切入点切入进类里面。
到这里呢Spring5也就基本学完了,接下来呢给大家整合回顾一下Mybatis的整合