Spring Java Config: configure already existing bean

3.3k Views Asked by At

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?

2

There are 2 best solutions below

5
Flame239 On BEST ANSWER

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

2
dimirsen Z On

Do you want to import one Config in AnotherConfig on? It can be done via annotation placed on AnotherConfig:

import org.springframework.context.annotation.Import;
...
@Import(value = {Config.class})
public class AnotherConfig ... {}