user1081751
user1081751

Reputation: 11

How to I initiate specific spring bean during server start up

I'm using some support data in my application and I want to load that data at the time of server start. I'm using spring context. If I would be able to load application context at the time of server start up, the problem would be solved.

Upvotes: 1

Views: 881

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328750

Register a ServletContextListener and fetch the bean in contextInitialized() (see Servlet Life-Cycle Events).

This has two advantages:

  1. You can fetch several beans in a predefined order (this should never be necessary but, well, reality always wins)
  2. It won't interfere with your tests

Upvotes: 2

BalusC
BalusC

Reputation: 1109342

Not sure about the Spring part as I don't use it, but in JSF you can use an @ApplicationScoped @ManagedBean whose eager attribtue is set to true.

@ManagedBean(eager=true)
@ApplicationScoped
public class Data {

    // ...

}

This will autoconstruct the managed bean on webapp's startup.

Upvotes: 2

Related Questions