I wish to perform a couple of setup methods within my Spring Context.
I currently have the following code but it doesn't work as I am saying they are beans
and have no return type.
@Configuration
@Component
public class MyServerContext {
...
// Works
@Bean
public UserData userData() {
UserData userData = new AWSUserDataFetcher(urlUtil()).fetchUserData();
return userData;
}
// Doesn't work
@Bean
public void setupKeyTrustStores() {
// Setup TrustStore & KeyStore
System.setProperty(SYS_TRUST_STORE, userData().get(TRUST_STORE_PATH));
System.setProperty(SYS_TRUST_STORE_PASSWORD, userData().get(TRUST_STORE_PASSWORD));
System.setProperty(SYS_KEY_STORE, userData().get(KEY_STORE_PATH));
System.setProperty(SYS_KEY_STORE_PASSWORD, userData().get(KEY_STORE_PASSWORD));
// Prevents handshake alert: unrecognized_name
System.setProperty(ENABLE_SNI_EXTENSION, "false");
}
...
}
How can I run this method automatically by the @Configuration
context without the @Bean
annotation?
One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
@Configuration is: not required, if you already pass the annotated class in the sources parameter when calling the SpringApplication. run() method; required, when you don't pass the annotated class explicitly, but it's in the package that's specified in the @ComponentScan annotation of your main configuration class.
If you use @Configuration, all methods marked as @Bean will be wrapped into a CGLIB wrapper which works as if it's the first call of this method, then the original method's body will be executed and the resulting object will be registered in the spring context.
We use @ContextConfiguration as following. Find the sample test class. Find the print screen of the output. To load multiple configuration classes we can specify them as following. Here we will load XML configuration class. Suppose we have spring-config.xml in classpath. We use @ContextConfiguration as following.
With SpringBoot, we can execute a method on startup via @EventListener annotation @Component public class LoadDataOnStartUp { @EventListener (ApplicationReadyEvent.class) public void loadData () { // do something } }
An ApplicationListener We can use this approach for running logic after the Spring context has been initialized. So, we aren't focusing on any particular bean. We're instead waiting for all of them to initialize. In order to do this, we need to create a bean that implements the ApplicationListener<ContextRefreshedEvent> interface:
This is a good idea in general but when starting up your spring application context from a integration test, main is never run! @JonasGeiregat: Plus, there are other scenarios where there is no main () at all, for example when using an application framework (e.g. JavaServer Faces).
You can use the @PostConstruct
annotation instead of @Bean
:
@Configuration
@Component
public class MyServerContext {
@Autowired
private UserData userData; // autowire the result of userData() bean method
@Bean
public UserData userData() {
UserData userData = new AWSUserDataFetcher(urlUtil()).fetchUserData();
return userData;
}
@PostConstruct
public void setupKeyTrustStores() {
// Setup TrustStore & KeyStore
System.setProperty(SYS_TRUST_STORE, userData.get(TRUST_STORE_PATH));
System.setProperty(SYS_TRUST_STORE_PASSWORD, userData.get(TRUST_STORE_PASSWORD));
System.setProperty(SYS_KEY_STORE, userData.get(KEY_STORE_PATH));
System.setProperty(SYS_KEY_STORE_PASSWORD, userData.get(KEY_STORE_PASSWORD));
// Prevents handshake alert: unrecognized_name
System.setProperty(ENABLE_SNI_EXTENSION, "false");
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With