AOP——配置文件方式实现

来源:互联网 发布:电脑版淘宝的微淘 编辑:程序博客网 时间:2024/06/07 02:11
接着上篇文章,分享一下在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
原创粉丝点击