淺談Spring Context加載方式
Spring 加載方式
對(duì)于可執(zhí)行文件方式,我們一般的加載Spring 配置的方式是
ClassPathXmlApplicationContext
public static void main(String[] args) { ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); DemoService demoService = (DemoService) xmlApplicationContext.getBean("demoService"); String text = demoService.hello(); System.out.println(text); }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 采用注釋的方式配置bean --> <context:annotation-config/> <!-- 配置要掃描的包 --> <context:component-scan base-package="com.jin.lesson.context"/> </beans>
從spring 3.0開始,開始使用注解的方式來進(jìn)行spring 配置的注冊(cè)
public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); // 告訴要掃描的包,通常是應(yīng)用的根目錄的Application類 annotationConfigApplicationContext.scan(Main.class.getPackage().getName()); // 刷新上下文,使用得相應(yīng)的bean注冊(cè)成功 annotationConfigApplicationContext.refresh(); // 通過名稱的方式獲取相應(yīng)的DemoService DemoService demoService = (DemoService) annotationConfigApplicationContext.getBean("demoService"); String text = demoService.hello(); System.out.println(text); }
demoService是定義的一個(gè)Service的名稱,xml配置的方式也是可以設(shè)定好是否采用注解的方式進(jìn)行掃描,如1中的
<context:annotation-config/>
demoService 很簡(jiǎn)單,如下的方式
@Service(value = "demoService") public class DemoService { public String hello() { return "hello world"; } }
Web應(yīng)用的初始化
- web.xml配置方式
- 注解的方式
web.xml 配置方式
利用spring 自帶的Servlet 進(jìn)行初始注冊(cè)
<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
利用 Listener進(jìn)行注冊(cè) ,像Spring+structs,就是以這種方式來初始化上下文內(nèi)容的
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
注解的方式
也是利用Servlet的方式來配置初始化參數(shù),不過這次里要用基于注解的類AnnotationConfigWebApplicationContext,同時(shí)要注冊(cè)Servlet
@Override public void onStartup(ServletContext servletContext) throws ServletException { ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", DispatcherServlet.class); dispatcher.setInitParameter("contextConfigLocation", getClass().getName()); dispatcher.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName()); dispatcher.addMapping("/*"); dispatcher.setLoadOnStartup(1); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
配置IDEA中java項(xiàng)目配置swagger全過程
這篇文章主要介紹了配置IDEA中java項(xiàng)目配置swagger全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Java8使用Stream流實(shí)現(xiàn)List列表查詢、統(tǒng)計(jì)、排序以及分組
List的Stream流操作可以簡(jiǎn)化我們的代碼,減少程序運(yùn)行的壓力,應(yīng)對(duì)上面的問題,下面這篇文章主要給大家介紹了關(guān)于Java8使用Stream流實(shí)現(xiàn)List列表查詢、統(tǒng)計(jì)、排序以及分組的相關(guān)資料,需要的朋友可以參考下2023-06-06Mybatis的特點(diǎn)及優(yōu)點(diǎn)
Mybatis 本是apache的一個(gè)開源項(xiàng)目iBatis, 2010年這個(gè)項(xiàng)目由apache software foundation 遷移到了google code,并且改名為MyBatis。mybatis有哪些特點(diǎn)和優(yōu)點(diǎn)呢?通過本文一起學(xué)習(xí)吧2016-12-12java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例
今天小編就為大家分享一篇java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07