Spring的AOP使用xml配置

来源:互联网 发布:苹果bt下载软件 编辑:程序博客网 时间:2024/05/21 06:26

 

需要使用spring的包,大家自己全部导入进去即可。省4........

 

用户管理接口

  1. package com.rx.spring;

  2. public interface UserManager {

  3.     public void addUser(String username, String password);
  4.     
  5.     public void deleteUser(int id);
  6.     

  7. }

用户管理实现

  1. package com.rx.spring;

  2. public class UserManagerImpl implements UserManager {

  3.     public void addUser(String username, String password) {
  4.         System.out.println("-------UserManagerImpl.addUser()----------");
  5.     }

  6.     public void deleteUser(int id) {
  7.         System.out.println("-------UserManagerImpl.deleteUser()----------");
  8.     }

  9. }

 

切面

  1. package com.rx.spring;

  2. public class SecurityHandler {
  3.     
  4.     private void checkSecurity() {
  5.         System.out.println("----------checkSecurity()---------------");
  6.     }
  7.     
  8. }

 

applicationContext.xml内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xmlns:aop="http://www.springframework.org/schema/aop"
  5.          xmlns:tx="http://www.springframework.org/schema/tx"
  6.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9.     
  10.     <bean id="securityHandler" class="com.rx.spring.SecurityHandler"/>           
  11.     
  12.     <bean id="userManager" class="com.rx.spring.UserManagerImpl"/>
  13.     
  14.     <aop:config>
  15.         <aop:aspect id="security" ref="securityHandler">
  16.             <aop:pointcut id="allAddMethod" expression="execution(* com.rx.spring.UserManagerImpl.add*(..))"/>
  17.             <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
  18.             <aop:after method="checkSecurity" pointcut-ref="allAddMethod"/>
  19.         </aop:aspect>
  20.     </aop:config>   
  21. </beans>

客户端调用:

  1. package com.rx.spring;

  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;

  4. public class Client {

  5.     public static void main(String[] args) {
  6.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  7.         
  8.         UserManager userManager = (UserManager)factory.getBean("userManager");
  9.         
  10.         userManager.addUser("sd""123");
  11.         userManager.deleteUser(1);
  12.     }
  13. }

 

运行结果:

 

----------checkSecurity()---------------
-------UserManagerImpl.addUser()----------
----------checkSecurity()---------------
-------UserManagerImpl.deleteUser()----------

 

详细配置图

0 0
原创粉丝点击