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