Spring 加載方式
對於可執行文件方式,我們一般的加載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開始,開始使用註解的方式來進行spring 配置的註冊
public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); // 告訴要掃描的包,通常是應用的根目錄的Application類annotationConfigApplicationContext.scan(Main.class.getPackage().getName()); // 刷新上下文,使用得相應的bean註冊成功annotationConfigApplicationContext.refresh(); // 通過名稱的方式獲取相應的DemoService DemoService demoService = (DemoService) annotationConfigApplicationContext.getBean("demoService"); String text = demoService.hello(); System.out.println(text); }demoService是定義的一個Service的名稱,xml配置的方式也是可以設定好是否採用註解的方式進行掃描,如1中的
<context:annotation-config/>
demoService 很簡單,如下的方式
@Service(value = "demoService")public class DemoService { public String hello() { return "hello world"; }}Web應用的初始化
web.xml 配置方式
利用spring 自帶的Servlet 進行初始註冊
<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進行註冊,像Spring+structs,就是以這種方式來初始化上下文內容的
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
註解的方式
也是利用Servlet的方式來配置初始化參數,不過這次裡要用基於註解的類AnnotationConfigWebApplicationContext,同時要註冊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); }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。