NIK
NIK

Reputation: 1

@autowired annotation for service class is not working in @configure class spring boot

when i am using @autowire to inject my dependencies in Configuration class its giving me as null please refer the code below .

@Configuration
public class DataSourceConfig {



    @Autowired
    AppService   appService;

    @Bean
    public BeanDefinitionRegistryPostProcessor beanPostProcessor() {
        return new BeanDefinitionRegistryPostProcessor() {

            public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {

            }

            public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanRegistry) throws BeansException {

                createBeans(beanRegistry);

            }

        };
    }

    private void createBeans(BeanDefinitionRegistry beanRegistry,DataSourceConfigService ds) {


        appService.getDbDetails();

appService is null here if i will call it using this way BeanDefinitionRegistryPostProcessor beanPostProcessor(AppService
appService) then in AppServiceImpl class AppDao dependency will be null

}
}


//// Service

@Service
public class AppServiceImpl  implements AppService{


    @Autowired
    AppDao ds;


    @Override
    public List<A> getDatabaseConfiguration() {
        return ds.getDbDetails(); // here ds is null 
    }


}

//dao
@Repository
public class AppDaoImpl implements AppDao {

    @Qualifier("nameParamJdbcTemplate")
    @Autowired
    public NamedParameterJdbcTemplate nameParamJdbcTemplate;

    @Override
    public List<A> getDbDetails() {
        return nameParamJdbcTemplate.query(SELECT_QUERY, new DataSourceMapper());  // nameParamJdbcTemplate is null 
    }


// datasource config

@Configuration
public class DataSourceBuilderConfig {

 @Bean(name = "dbSource")
 @ConfigurationProperties(prefix = "datasource")
 @Primary
 public DataSource dataSource1() {

    return DataSourceBuilder.create().build();

 }

 @Bean(name = "nameParamJdbcTemplate")
 @DependsOn("dbSource")
 @Autowired
 public NamedParameterJdbcTemplate jdbcTemplate1(@Qualifier("dbSource") DataSource dbSource) {
       return new NamedParameterJdbcTemplate(dbSource);
 }




}

What i want is when ever my beanPostProcessor() is executed i want all my dependent beans should be instantiated ie

 @Autowired
  AppService appService;  

@Autowired
AppDao ds;    
@Qualifier("nameParamJdbcTemplate")
@Autowired
public NamedParameterJdbcTemplate nameParamJdbcTemplate; 

I am new to spring so any help or working examples would be great. Thanks

Upvotes: 0

Views: 1529

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

It is null because this @Configuration class also defines a BeanDefinitionRegistryPostProcessor that forces the context to create that bean very early on.

Because you are using field injection, the context has to resolve AppService bean but it can't yet because the post-processor have to be applied first.

Your configuration looks very complex so you may want to simplify it a bit:

  • Separate low-level infrastructure configuration from main configuration
  • Always define such post processor as public static method so that the context can invoke the @Bean method without having to construct the class first.

Upvotes: 1

Related Questions