Spring 在Web中的应用

  在web项目开发中,不会直接实例化ApplicationContext对象,如果想用到ApplicationContext,一般的步骤:

  1. 配置一个监听器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>
  2. 可选的,设定你的xml配置文件的路径

    1. 如果文件在web-info 下面,并且名字为applicationContext.xml,就不需要再额外配置

    2. 如果文件不符合上面的规则,就需要配置context-param、

  3. 利用一个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();
}
}

最新文章

  1. jquery中attr和prop的区别
  2. java中hashMap的排序
  3. android修改系统时区
  4. linux应用程序地址布局,王明学learn
  5. WebSocket 服务器3
  6. bzoj 1588: [HNOI2002]营业额统计 treap
  7. [cocoapods]如何卸载cocoapods
  8. Git中的fetch和pull
  9. 第十一章 认识与学习BASH
  10. SVN与TortoiseSVN实战:补丁详解(转)
  11. css-文本垂直居中(转)
  12. TCP/IP,http,socket,长连接,短连接——小结。
  13. Intellij IDEA中使用Protobuf的正确姿势
  14. Python小代码_10_判断是否为素数
  15. 检索每个字符串的子串(python散列表实现)
  16. devstack 安装(centos7)
  17. Maven打包跳过测试,文档生成
  18. Java显式锁学习总结之四:ReentrantLock源码分析
  19. Python3 安装第三方包
  20. Swoole学习(二)Swoole之TCP服务器的创建

热门文章

  1. 运维监控篇Zabbix简单的性能调优
  2. csdn加入暂时会话功能
  3. Java技术专区-虚拟机系列-虚拟机参数(常用)
  4. Postfix+Dovecot+MySQL搭建邮件服务器
  5. linux环境安装opencv导入依赖报错问题
  6. 分布式项目中增加品牌前端页面出现Uncaught Error: [$injector:modulerr] bug后的原因以及改正方式
  7. thinkphp整合后台模板
  8. 模拟+细节题——cf1236D
  9. Redis启动Sentinel出现警告的解决
  10. 安装debian总结以及编译linux内核