我是java和Spring的新手,目前正在努力理解aop。我正试图用我的自定义注释和@After建议实现非常简单的代码,但由于某种原因,它不起作用,我所期望的是在控制台1和2上显示我只得到了1,你们知道为什么吗?
注释
package Terative.Services;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}
pom
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
psvm
package Terative;
import Terative.Services.Simulation_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
Simulation_service mm = new Simulation_service();
mm.metoda();
}
}
方位
package Terative.Services;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@RequiredArgsConstructor
@org.springframework.stereotype.Service
@Aspect
@Component
public class Simulation_service {
@Pointcut("@annotation(MyAnnotation)")
public void MyAnnotationMethod() {
}
@MyAnnotation
public void metoda() {
System.out.println("1");
}
@After("MyAnnotationMethod()")
public void metoda2() {
System.out.println("2");
}
}
C:Usersmtsge.jdksazul-13.0.6binjava.exe "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2019.3.4libidea_rt.jar=51747:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2019.3.4bin" -Dfile.encoding=UTF-8 -classpath C:UsersmtsgeOneDrivePulpitjdemotargetclasses;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-aop2.5.2spring-boot-starter-aop-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter2.5.2spring-boot-starter-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot2.5.2spring-boot-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-autoconfigure2.5.2spring-boot-autoconfigure-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-logging2.5.2spring-boot-starter-logging-2.5.2.jar;C:Usersmtsge.m2repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;C:Usersmtsge.m2repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;C:Usersmtsge.m2repositoryorgapachelogginglog4jlog4j-to-slf4j2.14.1log4j-to-slf4j-2.14.1.jar;C:Usersmtsge.m2repositoryorgapachelogginglog4jlog4j-api2.14.1log4j-api-2.14.1.jar;C:Usersmtsge.m2repositoryorgslf4jjul-to-slf4j1.7.31jul-to-slf4j-1.7.31.jar;C:Usersmtsge.m2repositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;C:Usersmtsge.m2repositoryorgyamlsnakeyaml1.28snakeyaml-1.28.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-aop5.3.8spring-aop-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-beans5.3.8spring-beans-5.3.8.jar;C:Usersmtsge.m2repositoryorgaspectjaspectjweaver1.9.6aspectjweaver-1.9.6.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-data-jpa2.5.2spring-boot-starter-data-jpa-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-jdbc2.5.2spring-boot-starter-jdbc-2.5.2.jar;C:Usersmtsge.m2repositorycomzaxxerHikariCP4.0.3HikariCP-4.0.3.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-jdbc5.3.8spring-jdbc-5.3.8.jar;C:Usersmtsge.m2repositoryjakartatransactionjakarta.transaction-api1.3.3jakarta.transaction-api-1.3.3.jar;C:Usersmtsge.m2repositoryjakartapersistencejakarta.persistence-api2.2.3jakarta.persistence-api-2.2.3.jar;C:Usersmtsge.m2repositoryorghibernatehibernate-core5.4.32.Finalhibernate-core-5.4.32.Final.jar;C:Usersmtsge.m2repositoryorgjbossloggingjboss-logging3.4.2.Finaljboss-logging-3.4.2.Final.jar;C:Usersmtsge.m2repositoryorgjavassistjavassist3.27.0-GAjavassist-3.27.0-GA.jar;C:Usersmtsge.m2repositorynetbytebuddybyte-buddy1.10.22byte-buddy-1.10.22.jar;C:Usersmtsge.m2repositoryantlrantlr2.7.7antlr-2.7.7.jar;C:Usersmtsge.m2repositoryorgjbossjandex2.2.3.Finaljandex-2.2.3.Final.jar;C:Usersmtsge.m2repositorycomfasterxmlclassmate1.5.1classmate-1.5.1.jar;C:Usersmtsge.m2repositoryorgdom4jdom4j2.1.3dom4j-2.1.3.jar;C:Usersmtsge.m2repositoryorghibernatecommonhibernate-commons-annotations5.1.2.Finalhibernate-commons-annotations-5.1.2.Final.jar;C:Usersmtsge.m2repositoryorgglassfishjaxbjaxb-runtime2.3.4jaxb-runtime-2.3.4.jar;C:Usersmtsge.m2repositoryorgglassfishjaxbtxw22.3.4txw2-2.3.4.jar;C:Usersmtsge.m2repositorycomsunistackistack-commons-runtime3.0.12istack-commons-runtime-3.0.12.jar;C:Usersmtsge.m2repositorycomsunactivationjakarta.activation1.2.2jakarta.activation-1.2.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkdataspring-data-jpa2.5.2spring-data-jpa-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkdataspring-data-commons2.5.2spring-data-commons-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-orm5.3.8spring-orm-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-context5.3.8spring-context-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-tx5.3.8spring-tx-5.3.8.jar;C:Usersmtsge.m2repositoryorgslf4jslf4j-api1.7.31slf4j-api-1.7.31.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-aspects5.3.8spring-aspects-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-web2.5.2spring-boot-starter-web-2.5.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-json2.5.2spring-boot-starter-json-2.5.2.jar;C:Usersmtsge.m2repositorycomfasterxmljacksoncorejackson-databind2.12.3jackson-databind-2.12.3.jar;C:Usersmtsge.m2repositorycomfasterxmljacksoncorejackson-annotations2.12.3jackson-annotations-2.12.3.jar;C:Usersmtsge.m2repositorycomfasterxmljacksoncorejackson-core2.12.3jackson-core-2.12.3.jar;C:Usersmtsge.m2repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.12.3jackson-datatype-jdk8-2.12.3.jar;C:Usersmtsge.m2repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.12.3jackson-datatype-jsr310-2.12.3.jar;C:Usersmtsge.m2repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.12.3jackson-module-parameter-names-2.12.3.jar;C:Usersmtsge.m2repositoryorgspringframeworkbootspring-boot-starter-tomcat2.5.2spring-boot-starter-tomcat-2.5.2.jar;C:Usersmtsge.m2repositoryorgapachetomcatembedtomcat-embed-core9.0.48tomcat-embed-core-9.0.48.jar;C:Usersmtsge.m2repositoryorgapachetomcatembedtomcat-embed-el9.0.48tomcat-embed-el-9.0.48.jar;C:Usersmtsge.m2repositoryorgapachetomcatembedtomcat-embed-websocket9.0.48tomcat-embed-websocket-9.0.48.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-web5.3.8spring-web-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-webmvc5.3.8spring-webmvc-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-expression5.3.8spring-expression-5.3.8.jar;C:Usersmtsge.m2repositorymysqlmysql-connector-java8.0.25mysql-connector-java-8.0.25.jar;C:Usersmtsge.m2repositoryorgprojectlomboklombok1.18.20lombok-1.18.20.jar;C:Usersmtsge.m2repositoryjakartaxmlbindjakarta.xml.bind-api2.3.3jakarta.xml.bind-api-2.3.3.jar;C:Usersmtsge.m2repositoryjakartaactivationjakarta.activation-api1.2.2jakarta.activation-api-1.2.2.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-core5.3.8spring-core-5.3.8.jar;C:Usersmtsge.m2repositoryorgspringframeworkspring-jcl5.3.8spring-jcl-5.3.8.jar Terative.DemoApplication
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)
2021-06-26 16:15:03.432 INFO 8188 --- [ main] Terative.DemoApplication : Starting DemoApplication using Java 13.0.6 on DESKTOP-61G00PJ with PID 8188 (C:UsersmtsgeOneDrivePulpitjdemotargetclasses started by mtsge in C:UsersmtsgeOneDrivePulpitjdemo)
2021-06-26 16:15:03.432 INFO 8188 --- [ main] Terative.DemoApplication : No active profile set, falling back to default profiles: default
2021-06-26 16:15:05.764 INFO 8188 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-06-26 16:15:05.800 INFO 8188 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 JPA repository interfaces.
2021-06-26 16:15:07.758 INFO 8188 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-06-26 16:15:07.779 INFO 8188 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-26 16:15:07.779 INFO 8188 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-06-26 16:15:08.007 INFO 8188 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-26 16:15:08.007 INFO 8188 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4292 ms
2021-06-26 16:15:08.462 INFO 8188 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-06-26 16:15:08.555 INFO 8188 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-06-26 16:15:08.801 INFO 8188 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-06-26 16:15:08.961 INFO 8188 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-06-26 16:15:09.603 INFO 8188 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-06-26 16:15:09.681 INFO 8188 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2021-06-26 16:15:10.478 INFO 8188 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-06-26 16:15:10.499 INFO 8188 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-06-26 16:15:10.612 WARN 8188 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-06-26 16:15:11.930 INFO 8188 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-26 16:15:11.945 INFO 8188 --- [ main] Terative.DemoApplication : Started DemoApplication in 9.53 seconds (JVM running for 10.837)
1
Process finished with exit code -1
在这里您自己创建实例。Spring不能在上面使用任何方面,因为它不属于Spring上下文。
Simulation_service mm = new Simulation_service();
mm.metoda();
您需要从spring上下文中检索实例。然后调用该方法将导致方面执行。
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
Simulation_service mm = context.getBean(Simulation_service.class)
mm.metoda();
}
如果其他一切都经过微调,它就会起作用。在任何情况下,我在这里所描述的都是为了向前迈进所必须的。
发现建议和自定义注释不能在同一个类中,这是可以理解的,因为创建aop的原因,我将带有自定义注释metoda((的方法移动到另一个类,并将其标记为@Bean annotation