Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does spring have a way to load up things when the application first loads? At a global level

Is there any spring specific way in the framework to perform initialization when MVC loads up?

Say I need to create global objects based on configuration files, is there a place to do this or do I just create my own servlet and do this in oninit?

like image 837
codecompleting Avatar asked Dec 07 '11 17:12

codecompleting


People also ask

What happens when spring boot application starts?

Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation. For example, if MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.

What are the disadvantages of spring boot?

Disadvantages of Spring BootSpring Boot creates a lot of unused dependencies, resulting in a large deployment file; The complex and time-consuming process of converting a legacy or an existing Spring project to a Spring Boot application; Not suitable for large-scale projects.

What problems does Spring Framework solve?

Databases are an important part of an application as without smooth and easy communication with the database, our application can become plain. Spring ensures easy and effective communication with databases as it has DAO (Data Access Object) functionality which is meant to read and write data to a database.


1 Answers

What about standard @PostConstruct?

@Service
class AnySpringBean {

    @PostConstruct
    public void init() {
        //run when bean is created
    }

}

Works on @Controllers as well.

UPDATE: The more global place would be to subclass ContextLoaderListener) and override contextInitialized() and use it in web.xml (see user1076371 answer). I don't like this approach much, but at least the initialization is not tied to any Spring bean.

like image 114
Tomasz Nurkiewicz Avatar answered Jan 03 '23 14:01

Tomasz Nurkiewicz