siye@r480:~/svlution/workspace/springaop4322$ tree src/
src/
├── main
│ ├── java
│ │ ├── log4j.properties
│ │ └── ocn
│ │ └── site
│ │ └── springaop
│ │ ├── advice
│ │ │ └── MethodBeforeAdviceImpl.java
│ │ ├── setup
│ │ │ └── Appconfig.java
│ │ └── target
│ │ └── User.java
│ └── resources
└── test
├── java
│ └── ocn
│ └── site
│ └── springaop
│ └── target
│ └── Runtest.java
└── resources
└── config
└── application.xml
17 directories, 6 files
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.22.RELEASE</version>
<scope>test</scope>
</dependency>
package ocn.site.springaop.advice;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;
@Component
public class MethodBeforeAdviceImpl implements MethodBeforeAdvice {
private final Logger logger = Logger.getLogger(this.getClass());
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
logger.info("before advice");
}
}
package ocn.site.springaop.setup;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan({ "ocn.site.springaop.advice", "ocn.site.springaop.target" })
public class Appconfig {
@Bean
public ProxyFactoryBean getProxyFactoryBean() {
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setProxyTargetClass(true);
return proxyFactoryBean;
}
@Bean
public AspectJExpressionPointcut getAspectJExpressionPointcut() {
return new AspectJExpressionPointcut();
}
@Bean
public DefaultPointcutAdvisor getDefaultPointcutAdvisor() {
return new DefaultPointcutAdvisor();
}
}
package ocn.site.springaop.target;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@Component
public class User {
private final Logger logger = Logger.getLogger(this.getClass());
public void work() {
logger.info("工作任务");
}
public void eat() {
logger.info("补充能量");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.springframework.aop.aspectj.AspectJExpressionPointcut"></bean>
<bean class="org.springframework.aop.support.DefaultPointcutAdvisor"></bean>
<bean class="ocn.site.springaop.target.User"></bean>
<bean class="ocn.site.springaop.advice.MethodBeforeAdviceImpl"></bean>
</beans>
package ocn.site.springaop.target;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import ocn.site.springaop.advice.MethodBeforeAdviceImpl;
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {
private final Logger logger = Logger.getLogger(this.getClass());
private @Autowired ProxyFactoryBean factoryBean;
private @Autowired AspectJExpressionPointcut pointcut;
private @Autowired DefaultPointcutAdvisor advisor;
private @Autowired User target;
private @Autowired MethodBeforeAdviceImpl beforeAdviceImpl;
@Test
public void run() throws Exception {
String expression = "execution(public void ocn.site.springaop.target.User.*(..))";
pointcut.setExpression(expression);
advisor.setPointcut(pointcut);
advisor.setAdvice(beforeAdviceImpl);
factoryBean.setTarget(target);
factoryBean.addAdvisor(advisor);
User user = (User) factoryBean.getObject();
user.work();
user.eat();
logger.info("test finished");
}
}
19-09-05 18:31:16 org.springframework.test.context.support.DefaultTestContextBootstrapper =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-05 18:31:16 org.springframework.test.context.support.DefaultTestContextBootstrapper =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@153f5a29, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@7f560810, org.springframework.test.context.support.DirtiesContextTestExecutionListener@69d9c55]
19-09-05 18:31:16 org.springframework.beans.factory.xml.XmlBeanDefinitionReader =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-05 18:31:16 org.springframework.context.support.GenericApplicationContext =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@5e5792a0: startup date [Thu Sep 05 18:31:16 CST 2019]; root of context hierarchy
19-09-05 18:31:17 ocn.site.springaop.advice.MethodBeforeAdviceImpl =====>>> before advice
19-09-05 18:31:17 ocn.site.springaop.target.User =====>>> 工作任务
19-09-05 18:31:17 ocn.site.springaop.advice.MethodBeforeAdviceImpl =====>>> before advice
19-09-05 18:31:17 ocn.site.springaop.target.User =====>>> 补充能量
19-09-05 18:31:17 ocn.site.springaop.target.Runtest =====>>> test finished
19-09-05 18:31:17 org.springframework.context.support.GenericApplicationContext =====>>> Closing org.springframework.context.support.GenericApplicationContext@5e5792a0: startup date [Thu Sep 05 18:31:16 CST 2019]; root of context hierarchy