Spring笔记(10)—— Spring AOP全自动编程
作者:互联网
1 AOP 全自动编程
- 导入 jar 包
- AOP 配置
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置 UserService-->
<bean id="userService" class="com.tzb.service.UserServiceImpl"></bean>
<bean id="stuService" class="com.tzb.service.StuService"></bean>
<!--配置切面类对象-->
<bean id="myAspect" class="com.tzb.aspect.MyAspect"></bean>
<!--全自动 AOP 配置
1. 在 bean 中配置 aop 约束
2. 配置 aop:config 内容,把切入点和通知结合
proxy-target-class: 使用 cglib 实现代理
expression 表达式:* 任意值
execution(* com.tzb.service. *. * (..) )
返回值 包名 类名 方法名 参数
AOP: 用于事务的配置和日志的记录,
-->
<aop:config proxy-target-class="true">
<!--切入点-->
<aop:pointcut id="myPointcut" expression="execution(* com.tzb.service.*.*(..))"/>
<!--通知:关联切入点-->
<aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"/>
</aop:config>
</beans>
- 测试
@Test
public void test1() {
// 获取 Spring 容器中代理对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans8.xml");
IUserService userService = (IUserService) context.getBean("userService");
userService.delete();
System.out.println();
StuService ss = (StuService) context.getBean("stuService");
ss.add();
}
标签:10,Spring,getBean,IUserService,context,AOP,userService 来源: https://blog.csdn.net/u012292754/article/details/99846892