Могу ли я обеспечить автоматическое связывание свойств в указанном порядке c с помощью Spring или использовать конструктор, отличный от конструктора по умолчанию? - PullRequest
0 голосов
/ 07 мая 2020

У меня есть требование динамически создавать управляемый класс 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, поэтому на них нужно ссылаться как на суперкласс.

@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-компонентах) или autowire ldapTemplate после того, как я установил свойства объекта вручную.

1 Ответ

0 голосов
/ 07 мая 2020

Не используйте @Bean в классе @Service. @Bean методы принадлежат к @Configuration классам.

Переместите метод, чтобы его можно было вызвать до того, как Spring попытается автоматически подключить поле ldapTemplate.

...