gpt4 book ai didi

java - Spring @EnableSpringConfigured 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:58 25 4
gpt4 key购买 nike

我尝试使用@EnableSpringConfigured,但它不起作用。

引擎.java

@Component
public class Engine {

public void run() {
System.out.println("Engine run");
}
}

实体.java

@Component
@Configurable(autowire = Autowire.BY_TYPE)
public class Entity {

@Autowired
private Engine engine;

public void exec() {
if (engine != null)
engine.run();
else
System.out.println("exec failure");
}

}

实体生成器.java

@Component
public class EntityBuilder {

public Entity create() {
return new Entity();
}
}

实体应用程序.java

@Configuration
@ComponentScan
@EnableSpringConfigured
public class EntityApplication {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EntityApplication.class);
EntityBuilder builder = context.getBean(EntityBuilder.class);
builder.create().exec();
}
}

以上四个java在同一个包中,我尝试运行EntityApplication.java并期望看到“Engine run”,但实际结果总是“exec failure”。


救命啊!代码在 https://github.com/lemonguge/spring/tree/master/spring-core/spring-aspect/src/main/java/cn/homjie/spring/aspect/newx

最佳答案

只有在启用加载时间波动的情况下,Spring 才能配置对象。您应该使用 @EnableLoadTimeWeaving 注释标记您的配置

对于 Configurable 注释,您需要加载时织入或编译时织入

关于java - Spring @EnableSpringConfigured 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660723/

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