Spring 在Web中的应用
Spring 在Web中的应用
在web项目开发中,不会直接实例化ApplicationContext对象,如果想用到ApplicationContext,一般的步骤:
配置一个监听器ContextLoaderListener,这个监听器是ServletContext的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>可选的,设定你的xml配置文件的路径
如果文件在web-info 下面,并且名字为applicationContext.xml,就不需要再额外配置
如果文件不符合上面的规则,就需要配置context-param、
利用一个WebApplicationContextUtils来读取第二部创建的ApplicationContext对象
导相关依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com</groupId>
<artifactId>spring-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.1.jre8</version>
</dependency> <!-- hibernate --> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency> <!-- 下面的依赖里面至少有LocalSessionFactoryBean -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--Web依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.1.RELEASE</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置web.xml文件
<listener>
<!--配置一个监听器ContextLoaderListener,这个监听器是ServletContext的监听器-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<!--spring的配置文件路径-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Springweb.xml</param-value>
</context-param>
配置Spring的依赖注入:SpringWeb.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dao" class="web.UserDaoImpl"></bean>
<bean id="service" class="web.Service">
<property name="userDao" ref="dao"></property>
</bean>
</beans>
dao接口
public interface UserDao {
void add();
}
dao实现
public class UserDaoImpl implements UserDao
{
public void add() {
System.out.println("aaaaaaaa");
}
}
service
public class Service {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void insert(){
userDao.add();
}
}
servlet
@WebServlet("/fist")
public class Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/* ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Springweb.xml");
Service service = applicationContext.getBean("service",Service.class);
service.insert();*/
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
Service service = applicationContext.getBean("service",Service.class);
service.insert();
}
}
Spring 在Web中的应用的更多相关文章
- spring在WEB中的应用。
1:创建IOC容器.在WEB应用程序启动的时候就创建.利用到监听器. ServletContextListener类的contextInitialized方法中 package com.struts2 ...
- 如何在Spring异步调用中传递上下文
以下文章来源于aoho求索 ,作者aoho 1. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步 ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- 使用Spring时web.xml中的配置
使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...
- spring web中的filter
昨天看了会spring web中部分代码,主要是各种filter,回顾一下: Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所 ...
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
- 转载:如何让spring mvc web应用启动时就执行
转载:如何让spring mvc web应用启动时就执行特定处理 http://www.cnblogs.com/yjmyzz/p/4747251.html# Spring-MVC的应用中 一.Appl ...
- 第一个Spring Boot Web程序
需要的环境和工具: 1.Eclipse2.Java环境(JDK 1.7或以上版本)3.Maven 3.0+(Eclipse已经内置了) 写个Hello Spring: 1.新建一个Maven项目,项目 ...
- 在Spring的bean中注入HttpServletRequest解密
我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这 ...
随机推荐
- springboot多数据库及分布式事务配置
1.导入相应的jar包依赖 <!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot< ...
- DPS首战鞍山
10月17日就即将飞机去鞍山了,不知不觉进入ACM的世界已经有一年了.虽然这一年我不够勤奋, 还是经常打LOL ,但是感觉自己真的学到了很多 . 在SCAU无数次选拔赛, 淘汰赛,个人排位赛 ,组队训 ...
- 如何在vue框架中兼容IE
IE目前已经放弃了自己的独特化,正一步步迎入互联网的主流怀抱.但迫于有用户存在,还是要兼容到IE8,9, 以上. 下面聊一下如何在vue框架中兼容IE 1.首先在index.html <meta ...
- ios打包,通过Xcode生成ipa文件
ios打包,通过Xcode生成ipa文件 干货文章 ·2018-03-21 19:03:47 打开ios项目目录,配置证书 将运行设备选择,如下图 选择:Product -> Scheme -& ...
- htop资源管理器
htop是linux资源管理器,安装后界面如下图: 当我们用安装yum -y htop时,会报错,这是因为需要安装扩展源 yum -y epel 扩展源 安装完扩展源之后,就可以安装了
- Eclipse导入的Maven项目没有Build Path
我导入的是 Signal-Server项目到 Eclipse中,发现 src 文件夹上面没有#号,包视图和语法提示都没有 ~~ 解决方法: 修改 Project Facets 在 项目右键 -> ...
- Spring Boot 2.0 常见问题总结(二)
使用 IDEA 生成 POJO 实体类 a. 使用 idea 连接上需要操作的数据库. b. 选中要生成实体类的数据库表:右键 ---> Scripted Extensions ---> ...
- leetcode-13双周赛-1257-最小公共区域
题目描述: 方法: class Solution(object): def findSmallestRegion(self, regions, region1, region2): parent = ...
- 函数计算工具链新成员 —— Fun Local 发布啦
刚刚,我们发布了函数计算工具链的新成员,Fun Local.欢迎大家使用! 如果你还不了解 Fun 是什么,我们来简单解释下. Fun 是什么 Fun 是 have Fun with Serverle ...
- 【dart学习】-- Dart之类和对象
一,概述 类(Class)是面向对象程序设计,实现信息封装的基础.类是一种用户定义的类型.每个类包含数据说明和一组操作数据或传递消息的函数.类的实例称为对象. Dart的类与其它语言都有很大的区别,比 ...