gpt4 book ai didi

java - AspectJ 不与 @Before 一起使用

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:47 25 4
gpt4 key购买 nike

我不知道为什么这个简单的应用程序不起作用。一切都正确连接在一起,我没有收到任何错误。我只是尝试使用 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();
}


}

相机.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);
}
}

记录器.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);
}
}

关于java - AspectJ 不与 @Before 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30139514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com