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