Spring的AOP配置

 

Spring的AOP配置

1.先写一个普通类:

package com.spring.aop;

public class Common {
 public void execute(String username,String password){
     System.out.println("------------------普通类----------------");
   }

}

2.写一个切面类,用于合法性校验和日志添加:

package com.spring.aop;

public class Check {
 public void checkValidity(){
     System.out.println("------------------验证合法性----------------");
 }

public void addLog(JoinPoint j){
  System.out.println("------------------添加日志----------------");
  Object obj[] = j.getArgs();
   for(Object o :obj){
    System.out.println(o);
   }
   System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名
 }

}
3.配置AOP,使用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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  <bean id="common" class="com.spring.aop.Common"/>
  <bean id="check" class="com.spring.aop.Check"/>
   
  <aop:config>
    <aop:aspect id="myAop" ref="check">
      <aop:pointcut id="target" expression="execution(* com.spring.aop.Common.execute(..))"/>
      <aop:before method="checkValidity" pointcut-ref="target"/>
      <aop:after method="addLog" pointcut-ref="target"/>
    </aop:aspect>
  </aop:config>
</beans>
注意:

execution(* com.spring.aop.*.*(..))"/

这样写应该就可以了

这是com.aptech.jb.epet.dao.hibimpl 包下所有的类的所有方法。。

第一个*代表所有的返回值类型

第二个*代表所有的类

第三个*代表类所有方法

最后一个..代表所有的参数。

 

4.最后写一个测试:

package com.spring.aop;

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-aop.xml");
     Common c=(Common) factory.getBean("common");
     c.execute("zhengjunhua","zhengjunhua");

   }
}

注意:

需要添加三个包:spring-aop.jar , aspectjrt.jar ,aspectjweaver.jar,否则会报错。

 

输出结果:

------------------验证合法性----------------
------------------普通类----------------
------------------添加日志----------------
zhengjunhua
zhengjunhua
========checkSecurity==execute

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP配置可以通过XML方式或注解方式进行。 XML方式的配置步骤如下: 1. 在Spring配置文件中引入aop的约束,例如:xmlns:aop="http://www.springframework.org/schema/aop"。 2. 把通知Bean交给Spring管理,使用<bean>标签进行配置。 3. 使用<aop:config>标签开始AOP配置。 4. 使用<aop:aspect>标签配置切面。 5. 使用对应的标签配置通知的类型,例如<aop:before>表示前置通知。 6. 在<aop:before>标签中指定通知方法和切入点表达式。 注解方式的配置步骤如下: 1. 在配置类上使用@Configuration注解进行标记。 2. 使用@ComponentScan注解指定需要扫描的包。 3. 使用@EnableAspectJAutoProxy注解开启Spring对注解AOP的支持。 以上是关于Spring AOP配置的简要说明,具体的配置内容可以参考引用的资料\[1\]、\[2\]和\[3\]。 #### 引用[.reference_title] - *1* *3* [Spring AOP 应用:三种配置及实现方式](https://blog.csdn.net/qq_37829947/article/details/117955529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【spring配置】——spring aop配置](https://blog.csdn.net/javawebxy/article/details/50492616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值