我有一个spring boot应用,有如下几个类,配置类和方面。下面的例子是为了说明我所面临的问题。
我有一个office类,它的打印机列表是使用外部属性文件配置创建的依赖关系,我想在调用打印机.getFilename方法时执行一个方面。我想在调用Printer.getFilename方法时执行一个方面。如果我有打印机列表,就不会触发方面,但是当我有一个没有列表的打印机对象时,它就会工作。
package com.example
public class Office {
private final List<Printer> printersList;
public Office(Printer printersList){
this.printersList = printersList;
}
public void printFiles(){
for(Printer printer: printersList)
printer.getFileName();
}
}
package com.example
public class Printer {
private deviceId;
public String getFileName(){
return "fileName";
}
}
@Configuration
public class ApplicationConfiguration{
@Bean
public Office office(){
List<Printer> printerList = new ArrayList<>();
// Adding to the list based on printer id based on some external property file configuration
printerList.add(new Printer());
printerList.add(new Printer());
return new Office(printerList);
}
}
@Aspect
@Component
public class PrinterFileNameAspect {
@Pointcut("execution(* com.example.Printer.getFileName())")
private void getFileNameJp() {}
@Around("getFileNameJp()")
public String returnFileName(ProceedingJoinPoint pjp) {
return "Modified File Name";
}
}
我发现Bean的列表没有在Spring容器中注册。因此,我修改了配置类来注册Bean。
@Configuration
public class ApplicationConfiguration{
@Autowired
private GenericWebApplicationContext context;
@Bean
public Office office(){
List<Printer> printerList = new ArrayList<>();
// Adding to the list based on printer id
Printer colorPrinter = new Printer();
context.registerBean("colorPrinter", Printer.class, () -> colorPrinter);
printerList.add(colorPrinter);
Printer specialPrinter = new Printer();
context.registerBean("specialPrinter", Printer.class, () -> specialPrinter);
printerList.add(specialPrinter);
return new Office(printerList);
}
}
以上配置的改变并没有帮助。我想我错过了一些spring aop的基础知识。我想用Printer的列表来实现spring aop,因为我不能改变列表生成逻辑(列表生成逻辑很复杂,必须是动态的)。
我添加了一个备选答案,因为你似乎很想学习如何使用新方法。GenericApplicationContext.registerBean(..)
在Spring 5中引入的。由于我不是Spring用户,我也想了解一下这是怎么回事,于是想到了这个解决方案。
同样,我提供的是完整的类定义。它们与我的第一个答案相似,但略有不同。具体来说。Printer
不再是一个原型范围的 @Component
但是一个POJO。我还是离开了 Office
为方便起见,将其改为单人组件。如果你也需要多个实例,你可以随时根据你的需要调整代码。
现在重要的是解决你的问题。在以编程方式注册Bean之后,你应该通过以下方式从应用程序上下文中获取它们 getBean()
而不仅仅是将手动创建的POJO实例添加到你的打印机列表中。只有当你从应用上下文中获取bean时,Spring才会在必要的地方照顾到也创建AOP代理。
package de.scrum_master.spring.q61661740;
public class Printer {
private String deviceId;
public Printer(String deviceId) {
this.deviceId = deviceId;
}
public String getFileName() {
return deviceId + ".pdf";
}
}
package de.scrum_master.spring.q61661740;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class Office {
private final List<Printer> printersList = new ArrayList<>();
public void addPrinter(Printer printer) {
printersList.add(printer);
}
public void printFiles() {
for (Printer printer : printersList)
System.out.println(printer.getFileName());
}
}
package de.scrum_master.spring.q61661740;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class PrinterFileNameAspect {
// Package name is optional if aspect is in same name as Printer
@Pointcut("execution(* de.scrum_master.spring.q61661740.Printer.getFileName())")
private void getFileNameJp() {}
@Around("getFileNameJp()")
public String returnFileName(ProceedingJoinPoint pjp) throws Throwable {
return "modified_" + pjp.proceed();
}
}
package de.scrum_master.spring.q61661740;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import java.util.stream.Stream;
@SpringBootApplication
@Configuration
@EnableAspectJAutoProxy
public class Application {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(Application.class)) {
// If you want to get rid of the `@SpringBootApplication` annotation, add this:
// appContext.scan(Application.class.getPackage().getName());
Office office = appContext.getBean(Office.class);
Stream
.of("colorPrinter", "specialPrinter")
.forEach(deviceID -> {
appContext.registerBean(deviceID, Printer.class, () -> new Printer(deviceID));
office.addPrinter(appContext.getBean(deviceID, Printer.class));
});
office.printFiles();
}
}
}
控制台日志看起来是这样的(缩短了)。
(...)
18:20:54.169 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'office'
18:20:54.177 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'colorPrinter'
18:20:54.178 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'specialPrinter'
colorPrinter.pdf
specialPrinter.pdf
(...)
怎么样添加包含以下内容的包 Printer
类到SpringBoot应用类中,并在 @SpringBootApplication
@Aspect
@Component
public class PrinterAspect
{
@Around( "getFileNameJp()" )
private String returnFileName( ProceedingJoinPoint joinPoint ) throws Throwable
{
return "Modified File Name"; // This will always return this name
}
}
最后,使启用 AspectJAutoproxy
用于申请
@SpringBootApplication(scanBasePackages = "com.example.*" )
@EnableAspectJAutoProxy( proxyTargetClass = true )
public class Application
{
public static void main( String[] args )
{
SpringApplication.run( Application.class, args );
}
}
你也错过了 @Component
在 Printer
和 Office
类。所以总的来说,你的 Office
类应该是这样的。
@Component
public class Office
{
private final List<Printer> printer;
public Office( List<Printer> printer )
{
this.printer = printer;
}
public void printFiles()
{
// my code logic ...
// Demo logic
for( Printer printer1 : printer )
{
System.out.println( printer1.getFileName() );
}
// my code logic ...
}
}
而打印机类应该是这样的
@Component
public class Printer
{
private int deviceId;
private String fileName;
public String getFileName()
{
// my code logic here ...
fileName = String.valueOf( System.nanoTime() ); // demo logic
return fileName;
}
}
而用法应该是这样的。
@Autowired
private Office office;
@GetMapping( "/demo" )
public List<String> demo()
{
office.printFiles();
return fileNames(); // To be implemented
}
这个基于原型scoped beans的简单解决方案怎么样?
package de.scrum_master.spring.q61661740;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Printer {
public String getFileName() {
return "fileName";
}
public void configureIndividually(String whatever) {
System.out.println("printer being configured individually: " + whatever);
}
}
package de.scrum_master.spring.q61661740;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class Office {
private final List<Printer> printersList = new ArrayList<>();
public void addPrinter(Printer printer) {
printersList.add(printer);
}
public void printFiles() {
for (Printer printer : printersList)
System.out.println(printer.getFileName());
}
}
package de.scrum_master.spring.q61661740;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class PrinterFileNameAspect {
// Package name is optional if aspect is in same name as Printer
@Pointcut("execution(* de.scrum_master.spring.q61661740.Printer.getFileName())")
private void getFileNameJp() {}
@Around("getFileNameJp()")
public String returnFileName(ProceedingJoinPoint pjp) {
return "modified file name";
}
}
package de.scrum_master.spring.q61661740;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Application {
public static void main(String[] args) {
try (ConfigurableApplicationContext appContext = SpringApplication.run(Application.class, args)) {
doStuff(appContext);
}
}
private static void doStuff(ConfigurableApplicationContext appContext) {
Printer colorPrinter = appContext.getBean(Printer.class);
colorPrinter.configureIndividually("my color config");
Printer specialPrinter = appContext.getBean(Printer.class);
specialPrinter.configureIndividually("my special config");
Office office = appContext.getBean(Office.class);
office.addPrinter(colorPrinter);
office.addPrinter(specialPrinter);
office.printFiles();
}
}
现在你可以让容器负责生成bean实例,但你仍然可以单独配置它们。我不明白为什么在这种情况下你必须手动注册bean。
控制台的日志会是
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE)
2020-05-10 15:52:24.817 INFO 10404 --- [ main] d.s.spring.q61661740.Application : Starting Application on Xander-Ultrabook with PID 10404 (C:\Users\alexa\Documents\java-src\spring-aop-playground\target\classes started by alexa in C:\Users\alexa\Documents\java-src\spring-aop-playground)
2020-05-10 15:52:24.821 INFO 10404 --- [ main] d.s.spring.q61661740.Application : No active profile set, falling back to default profiles: default
2020-05-10 15:52:25.895 INFO 10404 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-05-10 15:52:25.918 INFO 10404 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2020-05-10 15:52:26.454 INFO 10404 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$377fd151] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-05-10 15:52:27.148 INFO 10404 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-10 15:52:27.189 INFO 10404 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-10 15:52:27.190 INFO 10404 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2020-05-10 15:52:27.375 INFO 10404 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-10 15:52:27.376 INFO 10404 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2486 ms
2020-05-10 15:52:27.681 INFO 10404 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-10 15:52:28.005 INFO 10404 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-10 15:52:28.007 INFO 10404 --- [ main] d.s.spring.q61661740.Application : Started Application in 3.735 seconds (JVM running for 5.395)
printer being configured individually: my color config
printer being configured individually: my special config
modified file name
modified file name
2020-05-10 15:52:28.135 INFO 10404 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'