Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Java Config: configure already existing bean

I want to configure in my @Configuration class bean which is already created by other library's autoconfiguration. I just need to change some fields in that class after it's being initialized.

But I can't find a proper way how to provide code block in @Configuration class and not using @Bean annotation. Is there an ideomatic way to do so in spring?

like image 854
Igor Konoplyanko Avatar asked Sep 13 '19 09:09

Igor Konoplyanko


1 Answers

One way to do this:

@Configuration
class TestConfig {
    @Autowired
    private SomeBean someBean;

    @PostConstruct
    private void initSomeBean() {
       // someBean.setProperty("qwe");
    }
}

@PostConstruct annotation defines init-method, which is getting called after SomeBean is autowired. In this method you can adjust your bean

like image 61
Flame239 Avatar answered Nov 15 '22 07:11

Flame239