Spring容器-BeanFactory和ApplicationContext使用詳解
將BeanFactory和ApplicationContext作為容器使用
在Spring中,BeanFactory和ApplicationContext是容器的兩種實(shí)現(xiàn)方式,可以使用它們來管理對象的生命周期以及實(shí)現(xiàn)依賴注入等功能。下面我們來分別演示一下BeanFactory和ApplicationContext子類的使用方式。
BeanFactory容器
BeanFactory是Spring容器中最基本的接口,它提供了定義和管理bean的基本方式。使用BeanFactory容器的步驟如下:
1.定義一個(gè)Bean對象
public class HelloBean { private String message; public void setMessage(String message) { this.message = message; } public void sayHello() { System.out.println("Hello " + message); } }
2.在Spring的配置文件中聲明一個(gè)bean對象
<beans> <bean id="helloBean" class="com.example.HelloBean"> <property name="message" value="World" /> </bean> </beans>
3.使用BeanFactory容器獲取bean對象
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml")); HelloBean helloBean = (HelloBean) beanFactory.getBean("helloBean"); helloBean.sayHello();
ApplicationContext容器
ApplicationContext是BeanFactory的子接口,它是Spring容器的另一種實(shí)現(xiàn)方式。和BeanFactory容器相比,ApplicationContext容器提供了更加豐富的功能,例如國際化支持、事件發(fā)布、資源管理等。使用ApplicationContext容器的步驟如下: 1.定義一個(gè)Bean對象
public class HelloBean { private String message; public void setMessage(String message) { this.message = message; } public void sayHello() { System.out.println("Hello " + message); } }
2.在Spring的配置文件中聲明一個(gè)bean對象
<beans> <bean id="helloBean" class="com.example.HelloBean"> <property name="message" value="World" /> </bean> </beans>
3.使用ApplicationContext容器獲取bean對象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); HelloBean helloBean = (HelloBean) applicationContext.getBean("helloBean"); helloBean.sayHello();
Spring內(nèi)嵌Web容器的過程
在Spring中,ApplicationContext容器有多個(gè)子類,其中包括了用于WEB開發(fā)的子類。這些子類可以直接啟動(dòng)Tomcat、Jetty等Web容器,使得我們可以在不使用第三方Web容器的情況下,直接在Spring應(yīng)用內(nèi)啟動(dòng)Web應(yīng)用。下面以SpringBoot的Web容器啟動(dòng)為例,來說明ApplicationContext子類的具體源代碼實(shí)現(xiàn)過程。 我們先來看一下SpringBoot的啟動(dòng)類:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
在這個(gè)啟動(dòng)類中,我們使用了SpringBoot提供的@SpringBootApplication注解。這個(gè)注解是一個(gè)組合注解,包含了多個(gè)其他注解,其中一個(gè)注解就是@Import({ ServletWebServerFactoryAutoConfiguration.class, WebMvcAutoConfiguration.class })。這個(gè)注解的作用是向Spring容器中注冊兩個(gè)自動(dòng)配置類:ServletWebServerFactoryAutoConfiguration和WebMvcAutoConfiguration。
接著我們來看一下ServletWebServerFactoryAutoConfiguration這個(gè)類:
@Configuration(proxyBeanMethods = false) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass(Servlet.class) @ConditionalOnMissingBean(value = { ServletWebServerFactory.class, WebServerFactoryCustomizerBeanPostProcessor.class }) public class ServletWebServerFactoryAutoConfiguration { @Bean public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer( ObjectProvider<WebServerFactoryCustomizerBeanPostProcessor> webServerFactoryCustomizerBeanPostProcessor) { return new ServletWebServerFactoryCustomizer( webServerFactoryCustomizerBeanPostProcessor.getIfAvailable(Collections::emptyList)); } @Bean @ConditionalOnMissingBean public TomcatServletWebServerFactory tomcatServletWebServerFactory( ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers, ObjectProvider<TomcatContextCustomizer> contextCustomizers, Environment environment) { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setEnvironment(environment); factory.setTomcatConnectorCustomizers(connectorCustomizers.orderedStream().collect(Collectors.toList())); factory.setTomcatContextCustomizers(contextCustomizers.orderedStream().collect(Collectors.toList())); return factory; } }
可以看到,這個(gè)類標(biāo)記為@Configuration注解,表示它是一個(gè)配置類。這個(gè)類提供了TomcatServletWebServerFactory這個(gè)bean定義,它利用Tomcat作為Web容器。這個(gè)類還提供了一個(gè)servletWebServerFactoryCustomizer的bean定義,它會在Web容器啟動(dòng)前對Web容器進(jìn)行自定義的配置。
除了ServletWebServerFactoryAutoConfiguration之外,還有一些其他的自動(dòng)配置類。例如:
- HttpEncodingAutoConfiguration:自動(dòng)配置請求和響應(yīng)的編碼過濾器。
- DispatcherServletAutoConfiguration:自動(dòng)配置一個(gè)DispatcherServlet,它用于處理Web請求。
在SpringBoot啟動(dòng)時(shí),會將這些自動(dòng)配置類都加載到Spring容器中。最終,我們就可以使用ApplicationContext容器來獲取Web容器實(shí)例,代碼如下:
@Bean public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() { int port = environment.getProperty("server.port", Integer.class, 8080); return factory -> { factory.setPort(port); }; } @Bean public ServletWebServerFactory servletContainer() { return applicationContext.getBean(ServletWebServerFactory.class); }
可以看到,這個(gè)代碼中直接調(diào)用了ApplicationContext容器的getBean方法,來獲取ServletWebServerFactory這個(gè)bean實(shí)例。
以上就是一個(gè)簡單的說明Spring內(nèi)嵌Web容器的過程。在實(shí)際的項(xiàng)目中,我們也可以使用ApplicationContext容器來啟動(dòng)其他的Web容器,例如Jetty等??偟膩碚f,Spring內(nèi)嵌Web容器的使用非常的方便,適合于小型的Web應(yīng)用的快速開發(fā)。
以上就是Spring容器-BeanFactory和ApplicationContext使用詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring BeanFactory ApplicationContext的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JDK9的新特性之String壓縮和字符編碼的實(shí)現(xiàn)方法
這篇文章主要介紹了JDK9的新特性之String壓縮和字符編碼的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05在Java Spring框架中使用的設(shè)計(jì)模式有哪些
面試中常會被問道Spring框架使用了哪些設(shè)計(jì)模式?關(guān)于這個(gè)問題本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09eclipse老是自動(dòng)跳到console解決辦法
eclipse啟動(dòng)服務(wù)后,想看一些properties信息或者別的,但老是自動(dòng)跳轉(zhuǎn)到console頁面,本文給大家介紹了解決辦法,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03