使用注解实现切点编程
看一下使用XML
配置实现切面编程 ,dao
层
// dao 层
public class StudentDao {
public StudentDao study(){
System.out.print("--我现在想要学习--");
return new StudentDao();
}
}
aspect
切面类
// 切面类
public class StudentAspect {
public void before(){
System.out.print("<--学习之前我还想喝点水--");
}
public void after(){
System.out.print("我正在学习-->");
}
}
测试类Test
// 测试类
public class Test {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = new ClassPathXmlApplicationContext("spring.xml");
StudentDao bean = (StudentDao) applicationContext.getBean("studentDao");
bean.study();
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 向 IOC 容器注入业务类 studentService -->
<bean id="studentDao" class="cn.shaojie.dao.StudentDao"></bean>
<!-- 向 IOC 容器中注入切面类 studentAspect -->
<bean id="studentAspect" class="cn.shaojie.aspect.StudentAspect"></bean>
<!-- 编写 AOP 实现 -->
<aop:config>
<!-- 定义切面 -->
<aop:aspect id="studentAspect" ref="studentAspect">
<!-- 定义切点 -->
<aop:pointcut id="study" expression="execution(* cn.shaojie.dao.StudentDao.study(..))" />
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="study"/>
<!-- 后置通知 -->
<aop:after method="after" pointcut-ref="study"/>
</aop:aspect>
</aop:config>
</beans>
以上这个是正常的XML
配置切面编程的过程,接下来我们来看一下使用注解实现切面编程
dao
层
@Component
@EnableAspectJAutoProxy
@ComponentScan("cn.shaojie")
public class StudentDao {
@Bean
public StudentDao study(){
System.out.println("我现在想要学习!");
return new StudentDao();
}
}
@Component
注解将这个类加载到IOC
容器中,@EnableAspectJAutoProxy
开启切面的自动代理,@ComponentScan("cn.shaojie")
扫描包下的所有类使用当前的类作为IOC
的容器。
aspect
切面类
@Component
@Aspect
public class StudentAspect {
@Pointcut("execution(* cn.shaojie.dao.StudentDao.study(..))")
public void studentAspect(){
}
@After("studentAspect()")
public void after(){
System.out.println("我正在学习!");
}
@Before("studentAspect()")
public void before(){
System.out.println("学习之前我还想喝点水");
}
}
@Aspect
将这个类作为切面类,@Pointcut
将这个方法作为切点方法,@After
将方法作为后置通知,@Before
将方法作为前置通知。
测试类Test
public class Test {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(StudentDao.class);
applicationContext.getBean("study",StudentDao.class);
}
}
XML
文件内容
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 扫描器扫描注解 -->
<context:component-scan base-package="cn.shaojie" ></context:component-scan>
<!--
开启切面的注解扫描
@EnableAspectJAutoProxy 注解的作用和
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> 一样
-->
</beans>
到此使用注解创建AOP
切面编程到此结束。
更为详细说明,请关注个人博客:https://www.lzmvlog.top/