spring aop2

本文介绍了一个使用Spring框架实现面向切面编程(AOP)的具体案例。通过配置文件定义了切面,实现了对特定方法调用前的安全检查。示例中包含了安全管理器、用户管理接口及其实现,并展示了如何在不修改业务代码的情况下增加横切关注点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring对AOP的只是(采用配置文件的方式)


1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar
2、配置如下
<aop:config>
<aop:aspect id="security" ref="securityHandler">
<aop:pointcut id="allAddMethod" expression="execution(* com.bjsxt.spring.UserManagerImpl.add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>

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.addUser("张三", "123");
userManager.deleteUser(1);
}
}


public class SecurityHandler {

private void checkSecurity() {
System.out.println("----------checkSecurity()---------------");
}

}


public interface UserManager {


public void addUser(String username, String password);

public void deleteUser(int id);

public void modifyUser(int id, String username, String password);

public String findUserById(int id);
}


public class UserManagerImpl implements UserManager {


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


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


public String findUserById(int id) {
System.out.println("-------UserManagerImpl.findUserById()----------");
return null;
}


public void modifyUser(int id, String username, String password) {
System.out.println("-------UserManagerImpl.modifyUser()----------");
}

// private void checkSecurity() {
// System.out.println("----------checkSecurity()---------------");
// }
}


applicationContext.xml

<?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">

<bean id="securityHandler" class="com.bjsxt.spring.SecurityHandler"/>           

<bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>

<aop:config>
<aop:aspect id="security" ref="securityHandler">
<aop:pointcut id="allAddMethod" expression="execution(* com.bjsxt.spring.UserManagerImpl.add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值