我可以确保使用Spring或以非默认的构造函数来自动按特定顺序排列属性吗?

问题描述 投票:0回答:1

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

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

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有多种类型,因此需要将它们作为超类引用。

@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

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

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

java spring spring-ldap
1个回答
0
投票

请勿在@Bean类中使用@Service@Bean方法属于@Configuration类。

© www.soinside.com 2019 - 2024. All rights reserved.