spring 加載方式
對于可執行文件方式,我們一般的加載spring 配置的方式是
classpathxmlapplicationcontext
1
2
3
4
5
6
|
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); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<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 配置的注冊
1
2
3
4
5
6
7
8
9
10
11
|
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中的
1
|
<context:annotation-config/> |
demoservice 很簡單,如下的方式
1
2
3
4
5
6
|
@service (value = "demoservice" ) public class demoservice { public string hello() { return "hello world" ; } } |
web應用的初始化
- web.xml配置方式
- 注解的方式
web.xml 配置方式
利用spring 自帶的servlet 進行初始注冊
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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,就是以這種方式來初始化上下文內容的
1
2
3
4
5
6
7
8
|
<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
1
2
3
4
5
6
7
8
|
@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 ); } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/52623dde2ef2