Spring整合web应用
1、添加依赖
<?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.yan</groupId>
<artifactId>spring51</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--使用tomcat7插件,则不需要额外安装Tomcat服务器-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
添加web应用对应的目录
src/main/webapp
然后再添加web应用的核心配置文件src/main/webapp/WEB-INF/web.xml
配置ContextLoaderListener用于在服务器启动时初始化IoC/DI容器,并将容器存储在application中,供所有的用户共享使用
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--上下参数,供ContextLoaderListener使用,配置Spring容器的核心配置文件的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
定义Servlet
在Servlet或者jsp中引用IoC/DI容器
- BeanFactory一般用于内存敏感的受限环境开发
- ApplicationContext是BeanFactory的子接口,提供BeanFactory的所有功能,同时提供了针对企业级开发的功能[针对singleton受管bean的处理问题]
- WebApplicationContext是ApplicationContext的子接口,针对web应用开发提供特殊支持
WebApplicationContext wac= WebApplicationContextUtils.getRequiredWebApplicationContext(application);
Spring整合JDBC
添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<!-- 阿里的连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
定义表结构
create table t_users(
id bigint primary key auto_increment,
username varchar(20) not null unique,
password varchar(20) not null,
birth timestamp default current_timestamp,
sex boolean default 1
)engine=innodb default charset utf8;
定义实体类
@Data
public class UserBean implements Serializable{
private Long id;
private String username;
private String password;
private Date birth;
private Boolean sex;
}
定义DAO
public interface IUserDao{}
实现类
@Repository
public class UserDaoImpl implements IUserDao{}
使用注解配置受管bean,所以必须修改Spring配置文件,打开自动组件扫描
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.yan">
</context:component-scan>
</beans>
业务接口
public interface IUserServ{}
实现类
@Service
public class UserServImpl implements IUserServ {
@Autowired
private IUserDao userDao;
}