AOP——配置文件方式实现
2016-05-30 21:36
363 查看
接着上篇文章,分享一下在spring中用配置文件来实现AOP的安全检查。
(一)环境搭建不变
1.创建java项目
2.引入spring相关jar包
3.引入aspectJ相关jar包
(二)代码实现
1.切入类
(一)环境搭建不变
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的思想就如他的名字那样,一切都是面向切面的,他就像我们生活中的那一个个小小的插曲一样,虽然不会对生活造成什么影响,但是在那段时间里,也会让我们有所收获。
相关文章推荐
- 如何将TOMCAT注册成系统服务
- Shell脚本通用命令符号解析
- linux常用命令
- centos 下安装vsftpd
- ubuntu 下 opencv 3. 的安装和运行
- linux网卡驱动移植
- shell(十二)选取部分内容
- CentOS7.1 Xshell 经常掉线 Connection closed by foreign host
- linux(centos)搭建SVN服务器
- F(k)<(维护+枚举)\(找规律+递推+枚举)>
- db.properties文件配置备忘
- 监控应用状态
- Nginx-请求处理与响应
- Linux下find命令使用
- CenOS7.1安装VNC——让win7远程桌面linux
- apache安装
- Linux下出现No module named ..的原因
- Hadoop学习笔记(7)-简述MapReduce计算框架原理
- 简单介绍,基于ldirectord的高可用lvs-dr调度器
- Linux 忘记root密码并重置root密码