AspectJ 不使用@Before
AspectJ not working with @Before
我不知道为什么这个简单的应用程序无法运行。一切都正确连接在一起,我没有收到任何错误。我只是想使用 AspectJ 在另一个方法之前调用一个方法。
package com.springpractice.app;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/springpractice/app/Beans.xml");
Camera camera = (Camera) context.getBean("camera");
try {
camera.snap();
camera.snap(100);
} catch (Exception e) {
System.out.println("Caught Exception "+ e.getMessage());
}
context.close();
}
}
Camera.java
package com.springpractice.app;
import org.springframework.stereotype.Component;
@Component("camera")
public class Camera implements PhotoSnapper {
public Camera(){
//System.out.println("Hello from constructor");
}
void snap(){
System.out.println("Snaped a picture...");
}
void snap(int exposure){
System.out.println("Exposure.."+exposure);
}
}
Logger.java
package com.springpractice.app;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component("logger")
public class Logger {
@Pointcut("execution(* com.springpractice.app.Camera.snap(..))")
public void cameraSnap(){
}
@Before("cameraSnap()")
public void aboutToTakePicture(){
System.out.println("About to take a picture");
}
}
beans.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: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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.springpractice.app"></context:component-scan>
<aop:aspectj-autoproxy />
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.proxies</groupId>
<artifactId>Proxies_Practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</project>
输出
Snaped a picture...
Exposure..100
snap
方法需要成为 public API 的一部分。
package com.springpractice.app;
import org.springframework.stereotype.Component;
@Component("camera")
public class Camera implements PhotoSnapper {
public Camera() {
//System.out.println("Hello from constructor");
}
public void snap() {
System.out.println("Snaped a picture...");
}
public void snap(int exposure) {
System.out.println("Exposure.."+exposure);
}
}
我不知道为什么这个简单的应用程序无法运行。一切都正确连接在一起,我没有收到任何错误。我只是想使用 AspectJ 在另一个方法之前调用一个方法。
package com.springpractice.app;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/springpractice/app/Beans.xml");
Camera camera = (Camera) context.getBean("camera");
try {
camera.snap();
camera.snap(100);
} catch (Exception e) {
System.out.println("Caught Exception "+ e.getMessage());
}
context.close();
}
}
Camera.java
package com.springpractice.app;
import org.springframework.stereotype.Component;
@Component("camera")
public class Camera implements PhotoSnapper {
public Camera(){
//System.out.println("Hello from constructor");
}
void snap(){
System.out.println("Snaped a picture...");
}
void snap(int exposure){
System.out.println("Exposure.."+exposure);
}
}
Logger.java
package com.springpractice.app;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component("logger")
public class Logger {
@Pointcut("execution(* com.springpractice.app.Camera.snap(..))")
public void cameraSnap(){
}
@Before("cameraSnap()")
public void aboutToTakePicture(){
System.out.println("About to take a picture");
}
}
beans.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: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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.springpractice.app"></context:component-scan>
<aop:aspectj-autoproxy />
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.proxies</groupId>
<artifactId>Proxies_Practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</project>
输出
Snaped a picture...
Exposure..100
snap
方法需要成为 public API 的一部分。
package com.springpractice.app;
import org.springframework.stereotype.Component;
@Component("camera")
public class Camera implements PhotoSnapper {
public Camera() {
//System.out.println("Hello from constructor");
}
public void snap() {
System.out.println("Snaped a picture...");
}
public void snap(int exposure) {
System.out.println("Exposure.."+exposure);
}
}