我正在尝试在我的应用程序中使用简单的日志记录方面,但建议没有运行,并且在控制台中没有收到任何日志。
添加了依赖项-
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
实体类-
package com.ayushsingh.aopproject.entity;
import org.springframework.stereotype.Component;
@Component
public class Student {
private Long id;
private String name;
private String address;
private Integer age;
private Long phone;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getPhone() {
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
}
方面类-
package com.ayushsingh.aopproject.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
LoggingAspect() {
System.out.println("Logging Aspect instansiated");
}
// -Run before a getter
@Before("execution(public String com.ayushsingh.aopproject.entity.Student.getName())")
public void LoggingAdvice1() {
System.out.println("Advice 1");
}
// - Run before a method of a particular class
@Before("execution(public String com.ayushsingh.aopproject.entity.Student.getName())")
public void LoggingAdvice2() {
System.out.println("Adivce 2");
}
// - Run before all getters
@Before("execution(public String com.ayushsingh.aopproject.*.get*())")
public void LoggingAdvice3() {
System.out.println("Advice 3");
}
// - Run before all setters (methods with argument)
@Before("execution(public void com.ayushsingh.aopproject.*.set*(*))") // -one or more arguments
public void LoggingAdvice4() {
System.out.println("Advice 4");
}
// -for getters of all entity classes in the entity package
@Before("execution(public * com.ayushsingh.aopproject.entity.*.get*())")
public void LoggingAdvice5() {
System.out.println("Advice 5");
}
}
主课-
package com.ayushsingh.aopproject;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.ayushsingh.aopproject.entity.Student;
@SpringBootApplication
@EnableAspectJAutoProxy
public class AopProjectApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(AopProjectApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// -Create Student object
Student student = new Student();
student.setId(1L);
student.setAddress("Address 1");
student.setAge(25);
student.setName("Ayush");
student.setPhone(8145000047L);
String name=student.getName();
System.out.println("Name: " + student.getName());
}
}
现在在运行应用程序时,我还必须从建议中获取日志,但由于我还使用调试器和断点进行了检查,因此这些方面根本没有运行。 我得到的输出是-
2024-02-07T15:20:41.325+05:30 INFO 21704 --- [ restartedMain] c.a.aopproject.AopProjectApplication : No active profile set, falling back to 1 default profile: "default"
2024-02-07T15:20:41.408+05:30 INFO 21704 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
Logging Aspect instansiated
2024-02-07T15:20:42.527+05:30 INFO 21704 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2024-02-07T15:20:42.607+05:30 INFO 21704 --- [ restartedMain] c.a.aopproject.AopProjectApplication : Started AopProjectApplication in 1.732 seconds (process running for 2.185)
Name: Ayush
请帮我解决这个问题。
不要直接调用组件构造函数,让 Spring 容器通过在
getBean(..)
返回的应用程序上下文上调用 SpringApplication.run()
(当前已被您丢弃)来为您完成此操作。
还建议您在使用新技术时至少阅读最少的文档,或者可以从功能示例应用程序开始,逐步了解更多功能,始终只更改一些小部分,如果某些功能不起作用,您可以恢复这些内容。