基於aspectj的pcd的@annotation實現

siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springaop
│   │               ├── advice
│   │               │   └── LogUser.java
│   │               ├── anno
│   │               │   └── CustomAnno.java
│   │               ├── setup
│   │               │   └── Appconfig.java
│   │               └── target
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springaop
    │               └── target
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

18 directories, 7 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjtools</artifactId>
    <version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<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;

// 說明,摘錄的英文說明文檔皆出自spring的官方文檔。
@Aspect
@Component
public class LogUser {

//	Other pointcut types
//	The full AspectJ pointcut language supports additional pointcut designators that are not supported
//	in Spring. These are: call, get, set, preinitialization, staticinitialization,
//	initialization, handler, adviceexecution, withincode, cflow, cflowbelow,
//	if, @this, and @withincode. Use of these pointcut designators in pointcut expressions
//	interpreted by Spring AOP will result in an IllegalArgumentException being thrown.
//	The set of pointcut designators supported by Spring AOP may be extended in future releases to
//	support more of the AspectJ pointcut designators.

	// 多個pcd可以使用 && || ! 進行組合限制匹配

	private final Logger logger = Logger.getLogger(this.getClass());

	// @anntation(M) : 對匹配標註M註解的方法執行連接點(不支持通配符)
	private final String expression = "@annotation(ocn.site.springaop.anno.CustomAnno)";

	@Before(expression)
	public void beforeAdvice() {
		logger.info("before advice");
	}

}
package ocn.site.springaop.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnno {

}
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;

import ocn.site.springaop.anno.CustomAnno;

@Component
public class User {

	private final Logger logger = Logger.getLogger(this.getClass());

	@CustomAnno
	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.eat();
		user.work();
	}

}
<?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.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 19:32:03 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 19:32:04 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@2c6a3f77, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@246ae04d, org.springframework.test.context.support.DirtiesContextTestExecutionListener@62043840]
19-09-06 19:32:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-06 19:32:04 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@51081592: startup date [Fri Sep 06 19:32:04 CST 2019]; root of context hierarchy
19-09-06 19:32:04 ocn.site.springaop.target.User  =====>>> 補充能量
19-09-06 19:32:04 ocn.site.springaop.advice.LogUser  =====>>> before advice
19-09-06 19:32:04 ocn.site.springaop.target.User  =====>>> 工作任務
19-09-06 19:32:04 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@51081592: startup date [Fri Sep 06 19:32:04 CST 2019]; root of context hierarchy
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章