gpt4 book ai didi

java - 我可以确保使用 Spring 按特定顺序 Autowiring 属性,或者使用非默认构造函数吗?

转载 作者:行者123 更新时间:2023-12-01 16:52:08 28 4
gpt4 key购买 nike

我需要动态创建一个 Spring 托管类,该类依赖于传递的属性,然后才能设置其实例化属性之一。

理想情况下,我想在构造函数中传递所需的属性,但 Spring 似乎不允许在实例化 bean 时使用非默认构造函数。

SubBean.java:

@Configuration
public class SubBean extends SuperBean {

@Autowired
private LdapTemplate ldapTemplate;

public SubBean() {
super();
}

@Bean
public LdapTemplate setLdapTemplate() {
LdapContextSource contextSource= new LdapContextSource();

int separatorLocation = this.string.indexOf('/', 8);

contextSource.setUrl(this.string.substring(0, separatorLocation));
contextSource.setBase(this.string.substring(separatorLocation + 1));
contextSource.setAnonymousReadOnly(true);
return new LdapTemplate(contextSource);
}

public List<Computer> searchDirectory() {
SearchControls searchControls = new SearchControls();
searchControls.setCountLimit(25);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

List<Computer> computers = (List<Computer>) ldapTemplate.search(
LdapQueryBuilder.query().filter("OU=Domain Controllers"), new ComputerAttributesMapper());
return computers;
}

private class ComputerAttributesMapper implements AttributesMapper<Computer> {

@Override
public Computer mapFromAttributes(Attributes attributes) throws NamingException {
Computer computer = new Computer();
computer.setName((String) attributes.get("name").get(0));
return computer;
}

}

@Override
public void doStuff() {
List<Computer> computers = searchDirectory();
System.out.println(computers);
}

}

SpringBootConfiguration.java:SubBean 有多种类型,因此需要将它们作为父类(super class)进行引用。

@SpringBootApplication
public class SpringBootConfiguration {
@Autowired
private ConfigFile configFile;

@Autowired
private AutowireCapableBeanFactory beanFactory;

public static void main(String[] args) {
SpringApplication.run(SpringBootConfiguration.class, args);
}

@Bean
//Load configuration from a JSON file
public ConfigFile configFile() throws JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
File file = new File("configuration.json");
ConfigFile cf = mapper.readValue(file, ConfigFile.class);
cf.setFile(file);
return cf;
}

@Bean
public CommandLineRunner run(RestTemplate restTemplate, ApplicationContext ctx) throws Exception {
return args -> {
System.out.println(configFile);

List<Poller> configuredPollers = configFile.getConfiguration().getPollers();
for (Poller p : configuredPollers) {

//The class name is specified in the JSON configuration file.
Class<SuperBean> clazz = (Class<SuperBean>) Class.forName(p.getType());

//Use Spring to set up the object - this will try to autowire the ldapTemplate property and fail
SuperBean superBean = (SuperBean) beanFactory.createBean(clazz, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);

//Because the beanFactory only uses the default constructor, I have to explicitly set its properties
superBean.setString(p.getString());

superBean.doStuff();
}
};
}
}

当运行时,我收到异常:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'subBean': Unsatisfied dependency expressed through field 'ldapTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'setLdapTemplate' defined in class path resource [test/SubBean.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ldap.core.LdapTemplate]: Circular reference involving containing bean 'subBean' - consider declaring the factory method as static for independence from its containing instance. Factory method 'setLdapTemplate' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at test.SpringBootConfiguration.main(SpringBootConfiguration.java:28)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'setLdapTemplate' defined in class path resource [test/SubBean.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ldap.core.LdapTemplate]: Circular reference involving containing bean 'subBean' - consider declaring the factory method as static for independence from its containing instance. Factory method 'setLdapTemplate' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 19 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ldap.core.LdapTemplate]: Circular reference involving containing bean 'subBean' - consider declaring the factory method as static for independence from its containing instance. Factory method 'setLdapTemplate' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 32 more
Caused by: java.lang.NullPointerException
at test.SubBean.setLdapTemplate(SubBean.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 33 more

这是因为 SubBean 中的 intseparatorLocation = this.string.indexOf('/', 8); 未设置 this.string。 java.我需要能够在构造函数中设置 string (Spring 不允许在托管 bean 中设置)或在手动设置对象属性后 Autowiring ldapTemplate

最佳答案

不要在@Service类中使用@Bean@Bean 方法属于 @Configuration 类。

移动该方法,以便在 Spring 尝试 Autowiring ldapTemplate 字段之前调用它。

关于java - 我可以确保使用 Spring 按特定顺序 Autowiring 属性,或者使用非默认构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61666080/

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