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?
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.
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.
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.
What about standard @PostConstruct
?
@Service
class AnySpringBean {
@PostConstruct
public void init() {
//run when bean is created
}
}
Works on @Controller
s 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.
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