Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a void setup method in Spring Context @Configuration

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?

like image 452
ptimson Avatar asked Aug 15 '16 15:08

ptimson


People also ask

What is use of @configuration in spring?

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.

What is @configuration and @bean in spring?

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.

Do we need @configuration in spring boot?

@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.

How do @configuration annotated classes support Singleton beans?

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.

How to load multiple configuration classes using @contextconfiguration in spring?

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.

How to execute a method on startup in Spring Boot?

With SpringBoot, we can execute a method on startup via @EventListener annotation @Component public class LoadDataOnStartUp { @EventListener (ApplicationReadyEvent.class) public void loadData () { // do something } }

How do I run logic after context is initialized in spring?

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:

Should main() be run when starting a spring application context?

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).


1 Answers

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");
    }

    ...

}
like image 176
Miloš Milivojević Avatar answered Sep 21 '22 03:09

Miloš Milivojević