注解创建Aop切面编程

使用注解实现切点编程

看一下使用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/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值