Spring的AOP分为注解和配置两种方式实现. 网上注解的例子比较多.
看了视频, 写了个简单的以备后用.
Common.java 普通的类
package trytry.aop;
/**
* @author 李晨
* @version 创建时间:Jul 28, 2009 3:01:01 PM
*/
public class Common {
public void show(){
System.out.println("------------------普通----------------");
}
}
Check.java 验证的类
package trytry.aop;
/**
* @author 李晨
* @version 创建时间:Jul 28, 2009 3:03:08 PM
*/
public class Check {
public void show(){
System.out.println("------------------验证----------------");
}
}
下面是核心的Spring的配置文件applicationContext-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"
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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="common" class="trytry.aop.Common"/>
<bean id="check" class="trytry.aop.Check"/>
<aop:config>
<aop:aspect id="deal" ref="check">
<aop:pointcut id="target" expression="execution(* trytry.aop.Common.show(..))"/>
<aop:before method="show" pointcut-ref="target"/>
</aop:aspect>
</aop:config>
</beans>
最后是主程序调用,测试测试
package trytry.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 李晨
* @version 创建时间:Jul 28, 2009 3:06:07 PM
*/
public class Client {
public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext-aop.xml");
Common c=(Common) factory.getBean("common");
c.show();
}
}
测试结果,在控制台打印
------------------验证----------------
------------------普通----------------
完
分享到:
相关推荐
Spring AOP配置的实例,最基本的Spring AOP配置
springAOP配置实现动态代理,有利于熟悉动态代理原理,深入了解spring。
Spring AOP配置事务方法,描述了spring的事务配置,方便开发应用和数据库的连接管理。
利用SPring AOP配置切面的一个例子
里面包含了多种Spring AOP配置,十分详细。
SpringAop面向切面编程
spring aop配置常用jar包,但是spring官网不提供下载。
Spring aop 配置 Spring aspect 配置 Spring advisor 配置 Spring pointcut 配置
使用Spring配置文件实现AOP
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....
aop 配置的重要概括,记录详细,有示例说明。
spring AOP详细说明
最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式
NULL 博文链接:https://free9277.iteye.com/blog/1935297
spring-aop标签和配置文件两种方式实例,spring要求2.5及以上
SpringAop xml方式配置通知
spring配置文件的汇总,参考后对spring的配置将会学到很多
SpringAop配置事务管理,有两种配置方式。一种直接使用注解的方式,另外一种非注解