Spring应用扩展
使用其他方式配置数据源
配置PropertyPlaceholderConfigurer类加载properties文件中的数据库配置信息
引入properties文件方式
在applicationcontext.xml文件中引入databases.properties文件.
将applicationcontext.xml文件中引入数据源的方法改为如下:
<!-- 配置数据源 方式一: -->
<!-- 使用属性文件配置数据源 -->
<!-- 采用PropertyPlaceHolderConfigurer引入属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:databases.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
深入理解和配置Bean的作用域
Spring中Bean中的5个作用域
作用域 | 说明 |
---|---|
singleton | 默认值。以单例模式创建Bean实例,即容器中该Bean的实例只有一个 |
prototype | 每次从容器中获取Bean时,都会创建一个新的实例 |
request | 用于Web应用环境,针对每次HTTP请求都会创建一个实例 |
session | 用于Web应用环境,同一个会话共享同一个实例,不同的会话使用不同的实例 |
global session | 仅在Portlet的Web应用中使用,同一个全局会话共享一个实例。对于非Portlet环境,等同于session |
使用注解指定Bean的作用域
在用户业务实现类中添加 “@Scope(“prototype”)”,具体代码如下
@Scope("prototype") // 添加该注解
@Service("userService")
public class UserServiceImpl implements UserService {
// 省略其他代码
}
基于XML配置Spring的自动装配
autowire属性值及其说明
autowire属性值 | 说明 |
---|---|
no | 不使用自动装配。Bean依赖关系必须通过property元素定义 |
byType | 根据属性类型自动装配。BeanFactory查找容器中的全部Bean,如果正好有一个与依赖属性类型相同的Bean,就会自动装配这个属性;如果有多个这样的Bean,Spring无法决定注入哪个Bean,就会抛出一个致命异常;如果没有匹配的Bean,就什么都不会发生,属性不会被设置 |
byName | 根据属性名自动装配。BeanFactory查找容器中的全部Bean,找出id与属性的setter方法匹配的Bean。找到即自动注入,否则什么都不做 |
constructor | 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的Bean,那么将会抛出异常 |
代码示例
<!--配置DAO,根据属性名称自动装配-->
<bean id="userMapper" class="cn.kgc.dao.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
将applicationcontext.xml文件中的上面的代码改为如下代码:
<!--配置DAO,根据属性名称自动装配-->
<bean id="userMapper" class="cn.kgc.dao.UserMapperImpl" autowire="byName">
</bean>
也可以在beans元素中定义default-autowire属性来影响全局
Spring配置文件的拆分策略和拆分方法
使用XML方式进行配置Spring项目,项目规模较大时,庞大的Spring配置文件可读性、可维护性差。此外,在进行团队开发时,多人修改同一配置文件也容易发生冲突,降低开发效率。
下面代码是mybatis与spring整合的配置文件applicationcontext.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 配置数据源 方式一: -->
<!-- 使用属性文件配置数据源 -->
<!-- 采用PropertyPlaceHolderConfigurer引入属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:databases.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="cn.kgc.entity"/>
<!--<property name="configLocation" value="mybatis-config.xml"/>-->
<property name="mapperLocations">
<list>
<value>classpath:mapper/usermapper.xml</value>
<value>classpath:mapper/billmapper.xml</value>
</list>
</property>
</bean>
<!-- 注入映射器的两种方式 2: -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kgc.dao"/>
</bean>
<!-- 加入service注解-->
<context:component-scan base-package="cn.kgc.service"/>
<!-- !!!!!!!!!!!!!!!!!!!!!!!! 事务控制模块 !!!!!!!!!!!!!!!!!-->
<!-- 1. 使用 配置 方式添加事务控制-->
<!-- 数据源进行事务控制(添加bean组件) -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 2. 使用 注解 的方式添加事务控制-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
将以上代码拆分成三个部分applicationcontext.xml(配置数据源、SqlSessionFactoryBean、事务控制)、applicationcontext-dao.xml(DAO层)、applicationcontext-service.xm(Service层)。
applicationcontext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据源 方式一: -->
<!-- 使用属性文件配置数据源 -->
<!-- 采用PropertyPlaceHolderConfigurer引入属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:databases.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="cn.kgc.entity"/>
<!--<property name="configLocation" value="mybatis-config.xml"/>-->
<property name="mapperLocations">
<list>
<value>classpath:mapper/usermapper.xml</value>
<value>classpath:mapper/billmapper.xml</value>
</list>
</property>
</bean>
<!-- !!!!!!!!!!!!!!!!!!!!!!!! 事务控制模块 !!!!!!!!!!!!!!!!!-->
<!-- 1. 使用 配置 方式添加事务控制-->
<!-- 数据源进行事务控制(添加bean组件) -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 2. 使用 注解 的方式添加事务控制-->
<tx:annotation-driven transaction-manager="txManager"/>
<import resource="classpath:applicationcontext-dao.xml"/>
<import resource="classpath:applicationcontext-service.xml"/>
</beans>
applicationcontext-dao.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">
<!-- 注入映射器的两种方式 2: -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kgc.dao"/>
</bean>
</beans>
applicationcontext-service.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"
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-4.3.xsd">
<!-- 加入service注解-->
<context:component-scan base-package="cn.kgc.service"/>
</beans>