- Spring 5 Design Patterns
- Dinesh Rajput
- 365字
- 2021-07-08 09:59:27
Creating a container with an application context
Spring provides several flavors of application context as a bean container. There are multiple core implementations of the ApplicationContext interface, as shown here:
- FileSystemXmlApplicationContext: This class is an implementation of ApplicationContext that loads application context bean definitions from the configuration files (XML) located in the file system.
- ClassPathXmlApplicationContext: This class is an implementation of ApplicationContext that loads application context bean definitions from the configuration files (XML) located in the classpath of the application.
- AnnotationConfigApplicationContext: This class is an implementation of ApplicationContext that loads application context bean definitions from the configuration classes (Java based) from the class path of the application.
Spring provides you with a web-aware implementation of the ApplicationContext interface, as shown here:
- XmlWebApplicationContext: This class is a web-aware implementation of ApplicationContext that loads application context bean definitions from the configuration files (XML) contained in a web application.
- AnnotationConfigWebApplicationContext: This class is a web-aware implementation of ApplicationContext that loads Spring web application context bean definitions from one or more Java-based configuration classes.
We can use either one of these implementations to load beans into a bean factory. It depends upon our application configuration file locations. For example, if you want to load your configuration file spring.xml from the file system in a specific location, Spring provides you with a FileSystemXmlApplicationContext, class that looks for the configuration file spring.xml in a specific location within the file system:
ApplicationContext context = new
FileSystemXmlApplicationContext("d:/spring.xml");
In the same way, you can also load your application configuration file spring.xml from the classpath of your application by using a ClassPathXmlApplicationContext class provided by Spring. It looks for the configuration file spring.xml anywhere in the classpath (including JAR files):
ApplicationContext context = new
ClassPathXmlApplicationContext("spring.xml");
If you are using a Java configuration instead of an XML configuration, you can use AnnotationConfigApplicationContext:
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
After loading the configuration files and getting an application context, we can fetch beans from the Spring container by calling the getBean() method of the application context:
TransferService transferService =
context.getBean(TransferService.class);
In the following section, we will learn about the Spring bean life cycle, and how a Spring container reacts to the Spring bean to create and manage it.
- 黑客攻防從入門到精通(實戰秘笈版)
- ExtGWT Rich Internet Application Cookbook
- 潮流:UI設計必修課
- 摩登創客:與智能手機和平板電腦共舞
- MySQL數據庫應用與管理 第2版
- Java Web基礎與實例教程(第2版·微課版)
- 簡單高效LATEX
- Responsive Web Design with HTML5 and CSS3
- Android 7編程入門經典:使用Android Studio 2(第4版)
- TypeScript實戰指南
- Nexus規模化Scrum框架
- Apache Spark 2.x for Java Developers
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- Test-Driven JavaScript Development
- SQL Server 入門很輕松(微課超值版)