Common.Java
- package com.aspect;
- public class Common {
- public void fn()
- {
- System.out.println("****************");
- }
- public void tc()
- {
- System.out.println("|||||||||||||||||");
- }
- }
check.java
- package com.aspect;
- public class Check {
- public void check()
- {
- System.out.println("验证!!!!!!!!!!!");
- }
- }
web.xml配置文件如下
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
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.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="com.aspect.Common"/>
- <bean id="check" class="com.aspect.Check"/>
- <aop:config>
- <aop:aspect id="deal" ref="check">
- <aop:pointcut id="target" expression="execution(* com.aspect.Common.*(..))"/>
- <aop:before method="check" pointcut-ref="target"/>
- </aop:aspect>
- </aop:config>
- </beans>
测试类Client.java
- package com.aspect;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- public class Client {
- public static void main(String[] args) {
- ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- }
- //下面这句也可以
- // BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
- Common c=(Common) factory.getBean("common");
- c.fn();
- c.tc();
- }
- }
结果如下:
- 验证!!!!!!!!!!!
- ****************
- 验证!!!!!!!!!!!
- |||||||||||||||||
导入的5个jar包如下:
aopalliance.jar
aspectjweaver.jar
cglib-nodep-2.2.3.jar
commons-logging-1.0.4.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar