java三大框架spring之spring自动配置web环境
2016-08-19 20:12
597 查看
在 WEB 环境下使用 Spring
①. 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
②. Spring 的配置文件, 和非 WEB 环境没有什么不同
③. 需要在 web.xml 文件中加入如下配置:
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在其需要用到IOC容器的地方直接调用即可。代码如下:
<%@page import="com.levi.spring.struts2.beans.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1. 从 appication 域对象中得到 IOC 容器的实例
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
//2. 从 IOC 容器中得到 bean
Person person = ctx.getBean(Person.class);
//3. 使用 bean
person.hello();
%>
</body>
</html>其中还需要配置下bean那些
①. 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
②. Spring 的配置文件, 和非 WEB 环境没有什么不同
③. 需要在 web.xml 文件中加入如下配置:
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在其需要用到IOC容器的地方直接调用即可。代码如下:
<%@page import="com.levi.spring.struts2.beans.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1. 从 appication 域对象中得到 IOC 容器的实例
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
//2. 从 IOC 容器中得到 bean
Person person = ctx.getBean(Person.class);
//3. 使用 bean
person.hello();
%>
</body>
</html>其中还需要配置下bean那些
相关文章推荐
- java三大框架之spring手动配置web应用环境
- java框架之spring(web环境配置)
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- java web,从零开始,一步一步配置ssm(Spring+SpringMVC+MyBatis)框架
- Java Web框架play framework的下载与环境变量配置
- jbuilder9如何配置JSP运行环境,java高级配置,struts4.0,三大框架的一起应用
- Java Web框架play framework的下载与环境变量配置
- 初学Java三大框架集成需要掌握的基本配置[Spring,MyBatis,Struts]
- java注解方式(不用xml)配置web框架spring+SpringMVC
- JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架
- Java三大框架之spring aop 配置错误一
- 从零开始学JAVA WEB(1)——环境配置与框架选取
- java三大框架之spring配置template连接数据库中文乱码问题
- SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
- Java maven项目的搭建,配置web框架以及Spring
- Java_Web三大框架之Hibernate配置文件(二)
- JAVA_WEB项目之Struts2、Spring和Hibernate三大框架的整合
- struts2,hibernate,spring三大框架整合的注册程序(xml 配置,mysql数据库)
- myeclipse javaweb开发环境配置详解
- web框架之Spring-MVC环境搭建