spring中AOP——注解实现

之前在刚接触AOP的时候总是感觉AOP是一种技术,就像SSH一样,有具体的实现步骤。然而,最近这几天再次细致的学习AO之后,才发现,它就是一种编程的思想。就像“面向对象”一样,在编程的过程中很多地方都可以体现,但是如果说AOP是怎么实现的,那就不确定了。因为在不同的框架里,它的实现方式也是不一样的。
    我先说说我自己对AOP的理解。在回顾了Spring中的AOP之后,我认为,AOP就像我们走路,当我们在路上行走的时候,因为饥饿,去路边的饭店吃饭,然后当我们吃饱喝足的时候,就又回到那条路上我们之前离开的那个地方。而对于我们的路途来说,中间的那次吃饭根本没有什么影响,我们唯一的收获就是不饿了。
    就像上面的例子一样,我们在编程过程中的AOP也是如此,当我们在完成某一个功能的时候,我们需要在原本的功能逻辑的基础上加上一个记录日志的功能。因为这个日志的功能不仅仅是某一个模块中使用,而是大部分的功能中都会添加,所以假如我们在每个模块中都注入该功能那么将会十分的繁琐,而且不易于维护。然而,加入我们利用AOP的思想,在所有需要添加日志的功能中找到一个切入点,在该切入点内我们加入日志,然后在日志处理完成后再回到这个切入点,这样原本的功能没有任何影响,只是消耗了一些时间来处理其他的事情。以后如果将日志的方式改变了,我们只需要改变一处就可以实现所有需要记录日志的地方都改变记录日志的方式了。
springAOP的实现方式(一)
    (一)环境搭建
    1.创建java项目
    2.引入spring相关jar包
    3.引入aspectJ相关jar包
  (二)代码实现

    1.切入类

@Aspect
public class SecurityHandler {
	
	
	/**
	 * 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数
	 * 该方法就是一个标识,不进行调用
	 */
	@Pointcut("execution(* add*(..))")
	private void addAddMethod(){};
	
	
	/**
	 * 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
	 */
	@Before("addAddMethod()")
	//@After("addAddMethod()")
	private void checkSecurity() {
		System.out.println("-------记录日志,或者检查安全性-------");
	}		
}
2.切入点类
<span style="font-size:24px;">public class UserManagerImpl implements UserManager {
	public void addUser(String username, String password) {
		//checkSecurity();
		System.out.println("---------UserManagerImpl.add()--------");
	}
	public void delUser(int userId) {
		//checkSecurity();
		System.out.println("---------UserManagerImpl.delUser()--------");
	}
	public String findUserById(int userId) {
		//checkSecurity();
		System.out.println("---------UserManagerImpl.findUserById()--------");
		return "张三";
	}
	public void modifyUser(int userId, String username, String password) {
		//checkSecurity();
		System.out.println("---------UserManagerImpl.modifyUser()--------");
	}

}</span>


3.配置文件
<span style="font-size:24px;"><?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:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
       
    <!-- 启用AspectJ对Annotation的支持 -->       
	<aop:aspectj-autoproxy/>           
	
	<bean id="userManager" class="com.lcy.spring.UserManagerImpl"/>
	
	<bean id="securityHandler" class="com.lcy.spring.SecurityHandler"/> 
</beans></span>
4.测试方法:
<span style="font-size:24px;">import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
	public static void main(String[] args) {
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager");
		userManager.addUser("张三", "123");
	}
}</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值