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("------------------普通----------------");
}
}
/**
* @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("------------------验证----------------");
}
}
/**
* @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>
<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();
}
}
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();
}
}
测试结果,在控制台打印
------------------验证----------------
------------------普通----------------
------------------普通----------------
完