gpt4 book ai didi

java - Spring依赖@PostConstruct

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

假设我在 Spring @Configuration 中有这个依赖:

@Bean
public SomeClass someClass(SomeClass1 someClass1, SomeClass2 someClass2, ...) {
return new SomeClass(someClass1, someClass2, ...);
}

假设我想在 @PostConstruct 中做一些包含 someClass 依赖的事情:

@PostConstruct
public void init() {
someClass.doSomething();
}

这不能注入(inject):

@PostConstruct
public void init(SomeClass someClass) {
someClass.doSomething();
}

原因:

Caused by: java.lang.IllegalStateException: Lifecycle method annotation requires a no-arg method: ...

这不能像这样在相同的配置中 Autowiring :

@Autowire
private SomeClass someClass;

@Bean
public SomeClass someClass(SomeClass1 someClass1, SomeClass2 someClass2, ...) {
return new SomeClass(someClass1, someClass2, ...);
}

因为这导致:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'globalBus': Requested bean is currently in creation: Is there an unresolvable circular reference?

一个配置可以拆分(所以 @Bean 转到另一个配置)和 @Import -ed 由这个配置,它工作正常。可能存在其他解决方案 - 例如创建一个单独的初始化 bean 左右。

有没有办法在一个@Configuration中做到这一点?

编辑

应@SotiriosDelimanolis 的要求,sscce对于使用 @Autowired 时的异常:

public class ConfigPostConstructDependenciesPrb {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
ctx.getBean(Service.class);
ctx.close();
}

public static class Service {
private final Dependency dependency;

public Service(Dependency dependency) {
this.dependency = dependency;
}

public void work() {
System.out.println(dependency.getNum());
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Service [dependency=");
sb.append(dependency);
sb.append("]");
return sb.toString();
}
}

public static class Dependency {
private final int num;

public Dependency(int num) {
this.num = num;
}

public int getNum() {
return this.num;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("SomeClass1 [num=");
sb.append(num);
sb.append("]");
return sb.toString();
}
}

@Configuration
public static class BaseConfig {
@Autowired
private Service service;

@Bean
public Dependency dependency() {
return new Dependency(42);
}

@Bean
public Service service(Dependency dependency) {
return new Service(dependency);
}

@PostConstruct
public void init() {
service.work();
}
}

@Configuration
@Import(BaseConfig.class)
public static class Config {
@Autowired
private Service service;
}
}

最佳答案

(在 Spring 4.3.6 中测试)

在您的@Configuration 中创建一个嵌套类,并将@Autowired 服务@PostConstruct init() 的声明放在那里:

@Configuration
public static class BaseConfig {

//...

@Bean
public Service service(Dependency dependency) {
return new Service(dependency);
}

@Configuration
public static class Setup {

@Autowired
private Service service;

@PostConstruct
public void init() {
service.work();
}
}
}

以下是相应更新的完整示例。

请注意,您不必添加对 BaseConfig.Setup 的显式引用(查看 Config 类之前的 @Import 注释 -它仅指 BaseConfig 本身)。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;

import javax.annotation.PostConstruct;

public class ConfigPostConstructDependenciesPrb {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
ctx.getBean(Service.class);
ctx.close();
}

public static class Service {
private final Dependency dependency;

public Service(Dependency dependency) {
this.dependency = dependency;
}

public void work() {
System.out.println(dependency.getNum());
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Service [dependency=");
sb.append(dependency);
sb.append("]");
return sb.toString();
}
}

public static class Dependency {
private final int num;

public Dependency(int num) {
this.num = num;
}

public int getNum() {
return this.num;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("SomeClass1 [num=");
sb.append(num);
sb.append("]");
return sb.toString();
}
}

@Configuration
public static class BaseConfig {
@Bean
public Dependency dependency() {
return new Dependency(42);
}

@Bean
public Service service(Dependency dependency) {
return new Service(dependency);
}

@Configuration
public static class Setup {
@Autowired
private Service service;

@PostConstruct
public void init() {
service.work();
}
}
}

@Configuration
@Import(BaseConfig.class)
public static class Config {
@Autowired
private Service service;
}
}

关于java - Spring依赖@PostConstruct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31060017/

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