简体   繁体   中英

Can Spring Autowire beans created in a BeanFactoryPostProcessor

I have a standard bean with some properties that need to be autowired.

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    private FirstRepository first;

    public MyServiceImpl() {

    }

I use a Java Config to find the beans:

@Configuration
@ComponentScan(basePackages = "com.company", excludeFilters = { @Filter(Configuration.class) })
public class MainConfig {
}

However, the FirstRepository Bean doesn't exist so I create it in a BeanFactoryPostProcessor:

public class RepoGeneratorPostProcessor implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactory) throws BeansException {

        GenericBeanDefinition jpaR = new GenericBeanDefinition();
        jpaR.setBeanClass(JpaRepositoryFactoryBean.class);
        jpaR.setAutowireCandidate(true);
        jpaR.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
        jpaR.setLazyInit(false);
        jpaR.setPropertyValues(new MutablePropertyValues().add("repositoryInterface", FirstRepository.class));

        RootBeanDefinition definition = new RootBeanDefinition();
        definition.setBeanClass(FirstRepository.class);
        definition.setAutowireCandidate(true);
        definition.setFactoryBeanName("&jpaR");
        definition.setFactoryMethodName("getObject");
        definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_NAME);
        definition.setLazyInit(false);
        definition.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);


        BeanDefinitionRegistry registry = (BeanDefinitionRegistry)beanFactory;
        registry.registerBeanDefinition("jpaR", jpaR);
        registry.registerBeanDefinition("first", definition);

 }

When I start my application I get the following exception which seems to suggest that Spring can't find the FirstRepository bean.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.FirstRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

If I remove the @Autowired annotation I can see after start up that the FirstRepository bean is properly created.

Any suggestions?

This exception is saying that there is no bean defined for the FirstRepository class when the project is being built. Which I cannot see it here either.

The simplest solution would be to have a bean definition in your application-context.xml like this:

<bean id="firstRepository" class="your.package.FirstRepository" autowire="byName"/>

In this case, at the start up, there will be that bean definition.

I don't think you need the & before the beanname in

definition.setFactoryBeanName("&jpaR");

I used something like that in my project

definition.setFactoryBeanName("jpaR");

and it worked as expected

The & is needed if you need to get the factory bean of the bean named first. &first should return jpaR.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM