AOP——配置文件方式实现

接着上篇文章,分享一下在spring中用配置文件来实现AOP的安全检查。
  (一)环境搭建不变
    1.创建java项目
    2.引入spring相关jar包
    3.引入aspectJ相关jar包
(二)代码实现
    1.切入类
<span style="font-size:24px;">public class SecurityHandler {
	
	
	private void checkSecurity() {
		System.out.println("------此处添加安全性检查等操作-------");
	}		
}</span>
2.切入点类
<span style="font-size:24px;">	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">
       
       <!--  proxy-target-class="true"表示强制使用CGLIB -->
      <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <!-- 启用AspectJ对Annotation的支持 -->       
	<!-- <aop:aspectj-autoproxy/>        -->    
	
	<bean id="userManager" class="com.lcy.spring.UserManagerImpl"/>
	
	<bean id="securityHandler" class="com.lcy.spring.SecurityHandler"/> 
	
	<bean id="loginHanlder" class="com.lcy.spring.LoginHandler"/>
	
	<!-- 配置Aspect -->
	<aop:config>
		<aop:aspect id="securityAspect" ref="securityHandler">
			<aop:pointcut id="addAddMethod" expression="execution(* add*(..)) || execution(* del*(..)) "/>
			
			<aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>
		</aop:aspect>
		
		<aop:aspect id="loginAspect" ref="loginHanlder">
			<aop:pointcut id="loginMethod" expression="execution(* add*(..)) ||execution(* del*(..))||execution(* modify*(..)) "/>
			
			<aop:before method="CheckLogin" pointcut-ref="loginMethod"/>
		</aop:aspect>
	</aop:config>
</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");
		UserManager userManager = (UserManager)factory.getBean("userManager");
		userManager.modifyUser(1, "asd", "123321");
		userManager.delUser(1);
	}
}</span>
    总结:AOP的思想就如他的名字那样,一切都是面向切面的,他就像我们生活中的那一个个小小的插曲一样,虽然不会对生活造成什么影响,但是在那段时间里,也会让我们有所收获。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值