文章目录
1 Spring配置数据源
1.1 数据源(连接池)的作用
数据源就是数据库连接,数据源的作用:
1,提高程序性能
2,事先实例化数据源,初始部分连接资源
3,使用连接资源时从数据源中获取
4,使用完毕后将连接资源归还给数据源
常见的数据源有:DBCP,C3P0,BoneCP,Druid等
1.2 数据源的开发步骤
1,导入数据源的相关jar包
2,创建数据源对象
3,设置数据源的基本连接数据(驱动,用户名,密码...)
4,使用数据源获取连接资源进行操作
5,使用结束后归还连接资源
1,导入数据源相关jar包:
2,创建c3p0数据源对象并设置基本连接数据:
3,创建druid数据源对象并设置基本连接数据:
1.3 使用properties文件
上述使用数据源获取连接的过程中,无论是驱动还是用户名,密码都被写死到了代码里,这些内容应该存入文件中,通过更改properties文件中的配置信息就能获取不同的数据源连接
修改代码,使用properties文件中的数据:
1.4 使用Spring配置数据源
对于c3p0和druid这样第三方jar包内的对象,也可以在Spring配置文件中配置成Bean直接使用
<!-- 使用Spring配置c3p0数据源的Bean对象 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sys"></property>
<property name="user" value="root"></property>
<property name="password" value="111"></property>
</bean>
1.5 Spring加载properties文件
在Spring配置文件中同样可以加载properties文件来为属性赋值,如c3p0的Bean需要的driver,url等,可以不用直接填写字符串,将这些信息写入properties文件中,在Spring配置文件中加载,修改时就只需要修改properties文件,实现解耦
1,首先,需要在Spring文件头引入context命名空间和约束路径:
命名空间:
xmlns:context="http://www.springframework.org/schema/context"
约束路径:
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
Spring配置文件头:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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">
2,加载外部properties文件
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 加载外部properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 使用properties文件中的值注入Bean -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${name}"></property>
<property name="password" value="${password}"></property>
</bean>
</beans>
2 Spring注解开发
2.1 Spring原始注解介绍
Spring是轻代码而重配置的框架,配置比较繁重,会影响开发效率,所以注解开发必然是一种趋势,注解替代XML配置可以简化配置,提高开发效率
Spring原始注解:
1,使用XML配置文件时:
2,使用注解配置时:
注解配置完后,还需要增加注解的组件扫描,这个注解的组件扫描会告诉Spring哪个包下的类需要被进行扫描以生成Bean,在applicationContext.xml中增加context命名空间:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 配置组件扫描 Spring将会扫描base-package包下的所有类 将带有Component注解的类生成Bean -->
<context:component-scan base-package="com.coisini"></context:component-scan>
</beans>
2.2 Spring原始注解详解
1,Component,Controller,Service,Repository
这四个注解的作用相同,都是告知Spring被注解的类需要创建Bean
唯一不同的是Controller,Service,Repository可以表现出明显的语义
被Controller注解的Bean就可以知道是用于Web层...
2,Autowired,Qualifier,Resource
Autowired: 按照数据类型从Spring容器中进行匹配
Spring扫描到该注解时,会在容器中寻找对应类型的Bean注入
但是如果容器中有多个相同类型Bean,会出现错误
Qualifier: 按照id名称从容器中进行匹配
但是Qualifier需要结合Autowired一起使用
Resource: 相当于Autowired+Qualifiuer一起使用
3,Value
Value: 用于注入普通属性
可以直接注入数据,也可以使用properties文件中的值
4,Scope
Scope: 标明该Bean是单例还是原型
取值可选"singleton"或"prototype"
5,PostConstruct,PreDestroy
PostConstruct: 该Bean被创建后自动执行的初始化方法
PreDestroy: 该Bean被销毁前自动执行的销毁方法
2.3 Spring新注解
使用原始注解还不能完全替代XML配置文件,还需要使用注解替代的配置如下:
1,非自定义的Bean的配置 <bean>
2,加载properties文件的配置 <context:property-placeholder>
3,组件扫描配置 <context:component-scan>
4,引入其它文件 <import>
Spring新注解:
配置全部基于注解,一个核心配置类,若干被import的配置类:
获取Spring容器也需要换成新的API,提供核心配置类:
3 Spring集成Junit
在测试类中,可以看到每个测试方法都有部分重复的代码:
如获取context对象的步骤都重复出现,为了避免重复地写这些代码,可以:
1,让SpringJunit负责创建Spring容器
2,将需要进行的Bean直接在测试类中进行注入
Spring集成Junit步骤:
1,导入junit和spring-test的jar包
2,使用@Runwith注解替换原来的运行期
3,使用@ContextConfiguration指定配置文件或配置类
4,使用@Autowired注入需要测试的对象
5,创建测试方法进行测试
1,导入junit和spring-test的jar包:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2,添加@Runwith注解和@ContextConfiguration注解,将需要测试的Bean使用@Autowired注解装配,再使用@Test注解的方法直接测试:
4 Spring集成web环境
4.1 基本三层架构环境搭建
1,导入Spring以及Servlet等相关jar包
2,创建Spring的配置文件或配置类
3,构建Dao层Service层接口及其实现类
并将实现类装配成Bean加入Spring容器中
4,构建Web层创建Servlet程序
从容器中取得Bean完成业务逻辑
5,在web.xml文件中配置Servelt程序及其映射关系
1,导入Spring以及Servlet等相关jar包
2,创建Spring的配置文件或配置类
3,构建Dao层Service层接口及其实现类,并将实现类装配成Bean加入Spring容器中
4,构建Web层创建Servlet程序,从容器中取得Bean完成业务逻辑
5,在web.xml文件中配置Servelt程序及其映射关系
在浏览器访问UserServlet页面后:
成功调用了Dao层的Bean,完成了业务处理:
4.2 自定义ContextLoaderListener
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService service = (UserService) context.getBean("userService");
service.save();
}
}
回看UserServlet中的代码,这里每次调用Dao层对象处理业务前,都需要创建一个ApplicationContext对象,如果在每个doGet和doPost方法中都先加载一次配置文件/类,这无疑是不必要且低效的
解决方法很简单,可以使用ServletContextListener监听Web项目的启动,可以在Web项目启动时,加载Spring配置,创建一份ApplicationContext对象,并将其存放到最大的域ServletContext域中,这样就可以在任意位置使用ApplicationContext对象
自定义监听器的步骤:
1,创建一个类实现ServletContextListener
覆盖其中的contextInitialized()和contextDestroyed()
2,在contextInitialized()中创建ApplicationContext对象context
并获取ServletContext域,将context作为属性存入ServletContext域
3,在web.xml文件中配置好自定义的监听器
4,在所有Servlet程序中直接获取ServletContext域
从中取得context对象,完成业务处理
1,2,创建一个类实现ServletContextListener,覆盖其中的contextInitialized()和contextDestroyed()
在contextInitialized()中创建ApplicationContext对象context
并获取ServletContext域,将context作为属性存入ServletContext域
/**
* @author 雫
* @date 2021/7/10 - 3:50
* @function 自定义监听器实现了ServletContextListener
* 能在Web项目启动和关闭时做出反应
* 这里用来创建ApplicationContext对象并存入ServletContext域中
*/
public class ContextLoaderListener implements ServletContextListener {
/**
* @Author 雫
* @Description 每当Web程序启动时 能被监听到
* 此时加载Spring配置 将Spring容器存储到Web项目最大的域ServletContext中
* @Date 2021/7/10 3:53
* @Param [sce]
* @return void
**/
@Override
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("context", context);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {}
}
3,在web.xml文件中配置好自定义的监听器
4,从ServletContext域中取得ApplicationContext对象,获取Bean并使用
4.3 Spring集成Web环境
4.2中自定义的ContextLoaderListener实际不用自己实现,Spring提供了监听器ContextLoaderListener就能封装上述功能,该监听器内部加载Spring配置文件,创建ApplicationContext对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获取ApplicationContext对象
使用该功能需要:
1,导入spring-web相关jar包
2,配置全局初始化参数,在web.xml中配置ContextLoaderListener监听器
3,使用WebApplicationContextUtils获取ApplicationContext对象
1,导入spring-web相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2,配置全局初始化参数,在web.xml中配置ContextLoaderListener监听器
<!-- 全局初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring-web提供的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3,使用WebApplicationContextUtils获取ApplicationContext对象
启动服务器,浏览器访问UserService: