18.0面向切面AOP
当我们程序写完后,用户又提出想要在完成某项功能的操作前后加一个日志内容显示,这时我们在不改动原代码的情况下,只能使用AOP面向切面技术来添加log日志,和动态代理,理念相似。
UserService.java 接口文件
public interface UserService {
public void add ();
public void delete ();
public void select ();
public void modify ();
}
UserServiceImpl.java 接口实现类文件
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("添加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void select() {
System.out.println("查询了一个用户");
}
@Override
public void modify() {
System.out.println("修改了一个用户");
}
}
Log.java文件
public class Log implements MethodBeforeAdvice {
//Method:要执行的目标对象的方法
//args:参数
//target:目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了"+method.getName()+"方法");
}
}
AfterLog.java文件
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了方法"+method.getName()+"返回值是"+returnValue);
}
}
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
https://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="UserService" class="spring.Springaop.serviceImpl.UserServiceImpl" />
<bean id="Log" class="spring.Springaop.Log" />
<bean id="AfterLog" class="spring.Springaop.AfterLog" />
<!--方式一:采用原生SpringAIP接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:execution需要执行的位置(* * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* hkl.com.ipml.serviceImpl.*(..))" />
<!--执行环绕增加-->
<aop:advisor advice-ref="Log" pointcut-ref="pointcut" />
<aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut" />
</aop:config>
</beans>
Test.java测试文件
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationConfig.xml");
//动态代理的是接口,不是接口的实现类
UserService userService = (UserService) context.getBean("UserService");
userService.add();
}
}
执行结果如下图所示:

18.1、面向切面AOP 2
第二种实现AOP的方式:
Log2.java文件
public class Log2 {
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
https://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="UserService" class="spring.Springaop.serviceImpl.UserServiceImpl" />
<bean id="Log" class="spring.Springaop.Log" />
<bean id="AfterLog" class="spring.Springaop.AfterLog" />
<!--方式二:采用自定义类-->
<bean id="Log2" class="spring.Springaop.Log2" />
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref=”Log2”>
<!--切入点-->
<aop:pointcut id="point" expression="execution(* hkl.com.ipml.serviceImpl.*(..))" />
<!--通知,执行环绕增加-->
<aop:before advice-ref="before" pointcut-ref="point" />
<aop:after advice-ref="after" pointcut-ref="point" />
<aop:aspect>
</aop:config>
</beans>
19.0、用注解实现面向切面AOP
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="UserService" class="spring.Springaop.serviceImpl.UserServiceImpl" />
<bean id="Log" class="spring.Springaop.Log" />
<bean id="AfterLog" class="spring.Springaop.AfterLog" />
<bean id="UserImpl" class="spring.aop2.UserImpl" />
<bean id="Pointcutproxy" class="spring.aop2.Pointcutproxy" />
<aop:aspectj-autoproxy />
</beans>
Pointcutproxy.java文件
@Aspect
public class Pointcutproxy {
@Before("execution(* spring.aop2.UserImpl.*(..))")
public void before() {
System.out.println("======方法执行前======");
}
@After("execution(* spring.aop2.UserImpl.*(..))")
public void after() {
System.out.println("======方法执行后======");
}
@Around("execution(* spring.aop2.UserImpl.*(..))")
public void around() {
System.out.println("======执行环绕增强======");
}
}
测试文件test.java
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationConfig.xml");
User u = (User) context.getBean("UserImpl");
u.add();
}
}

1万+

被折叠的 条评论
为什么被折叠?



