siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│ ├── java
│ │ ├── log4j.properties
│ │ └── ocn
│ │ └── site
│ │ └── springaop
│ │ ├── advice
│ │ │ └── LogUser.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 org. apache. log4j. Logger;
import org. aspectj. lang. annotation. Aspect;
import org. aspectj. lang. annotation. Before;
import org. springframework. stereotype. Component;
@Aspect
@Component
public class LogUser {
private final Logger logger = Logger. getLogger ( this . getClass ( ) ) ;
private final String execution = "bean(user)" ;
@Before ( value = execution)
public void beforeAdvice ( ) {
logger. info ( "before advice" ) ;
}
}
package ocn. site. springaop. setup;
import org. springframework. context. annotation. ComponentScan;
import org. springframework. context. annotation. Configuration;
import org. springframework. context. annotation. EnableAspectJAutoProxy;
@Configuration
@ComponentScan ( { "ocn.site.springaop.advice" , "ocn.site.springaop.target" } )
@EnableAspectJAutoProxy ( proxyTargetClass = true )
public class Appconfig {
}
package ocn. site. springaop. target;
import org. apache. log4j. Logger;
import org. springframework. stereotype. Component;
@Component ( "user" )
public class User {
private final Logger logger = Logger. getLogger ( this . getClass ( ) ) ;
public void work ( ) {
logger. info ( "工作任务" ) ;
}
public void eat ( ) {
logger. info ( "补充能量" ) ;
}
}
package ocn. site. springaop. target;
import org. junit. Test;
import org. junit. runner. RunWith;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. test. context. ContextConfiguration;
import org. springframework. test. context. junit4. SpringRunner;
@RunWith ( SpringRunner. class )
@ContextConfiguration ( "classpath:config/application.xml" )
public class Runtest {
private @Autowired User user;
@Test
public void run ( ) throws Exception {
user. work ( ) ;
user. eat ( ) ;
}
}
<?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: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
< aop: aspectj-autoproxy proxy-target-class = " true" > </ aop: aspectj-autoproxy>
< context: component-scan base-package = " ocn.site.springaop.advice" > </ context: component-scan>
< context: component-scan base-package = " ocn.site.springaop.target" > </ context: component-scan>
</ beans>
19-09-06 12:55:52 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-06 12:55:52 org.springframework.test.context.support.DefaultTestContextBootstrapper =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@180bc464, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1324409e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@2c6a3f77]
19-09-06 12:55:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-06 12:55:53 org.springframework.context.support.GenericApplicationContext =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@5034c75a: startup date [Fri Sep 06 12:55:53 CST 2019]; root of context hierarchy
19-09-06 12:55:53 ocn.site.springaop.advice.LogUser =====>>> before advice
19-09-06 12:55:53 ocn.site.springaop.target.User =====>>> 工作任务
19-09-06 12:55:53 ocn.site.springaop.advice.LogUser =====>>> before advice
19-09-06 12:55:53 ocn.site.springaop.target.User =====>>> 补充能量
19-09-06 12:55:53 org.springframework.context.support.GenericApplicationContext =====>>> Closing org.springframework.context.support.GenericApplicationContext@5034c75a: startup date [Fri Sep 06 12:55:53 CST 2019]; root of context hierarchy