1,数据库配置–配置数据源
使用properties文件引入,定制数据源时,引入配置文件中的属性
<!-- 引入properties文件-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"/>
</bean>
<!-- 数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${uname}"/>
<property name="password" value="${password}"/>
</bean>
properties文件中
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8
password=root
uname=root
2,JNDI(javaNameAndDirectoryInterface)配置数据源
在Tomcat中conf目录下的context.xml文件中,配置如下
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<!--
|- name:表示以后要查找的名称。通过此名称可以找到DataSource,此名称任意更换,但是程序中最终要查找的就是此名称,
为了不与其他的名称混淆,所以使用jdbc/oracle,现在配置的是一个jdbc的关于oracle的命名服务。
|- auth:由容器进行授权及管理,指的用户名和密码是否可以在容器上生效
|- type:此名称所代表的类型,现在为javax.sql.DataSource
|- maxActive:表示一个数据库在此服务器上所能打开的最大连接数
|- maxIdle:表示一个数据库在此服务器上维持的最小连接数
|- maxWait:最大等待时间。10000毫秒
|- username:数据库连接的用户名
|- password:数据库连接的密码
|- driverClassName:数据库连接的驱动程序
|- url:数据库连接的地址
-->
<!--配置MySQL数据库的JNDI数据源-->
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&characterEncoding=utf-8"/>
</GlobalNamingResources>
在Spring核心配置文件中编写如下代码:
<!-- 通过JNDI配置DataSource-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<!-- name的值时固定的,value的值java:comp/env/此部分是固定的,后面的部分是JNDI数据源中的name -->
<property name="jndiName" value="java:comp/env/jdbc/mysql"/>
</bean>
</beans>
3,拆分Spring配置文件—2种方式
1>把Spring配置文件按照一定规则拆分成若干个文件,通过ClassPathXmlApplicationContext类提供的重载进行合成加载。
如果有多个配置文件需要载入,可以用多字符串参数或String[]数组形式传入多个配置文件名,也可通过通配符的形式进行传参;
//1--使用通配符的方法
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext*.xml");
//2--用多字符串参数
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext-dao.xml","applicationContext-service.xml");
//3--使用String[]数组形式
String [] configs={"applicationContext.xml","applicationContext-dao.xml","applicationContext-service.xml"};
ApplicationContext context=new ClassPathXmlApplicationContext(configs);
2>通过import标签,在Spring核心配置文件中载入其他部分的配置文件,加载时,只需指定核心配置文件即可。
<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
Bean的自动装配
1,在xml配置文件中使用bean标签的autowire属性
其属性值:
值 | 说明 |
---|---|
no | 不使用自动装配。必须通过property元素定义 |
byName | 根据属性明自动装配。BeanFactory查找容器中的全部Bean,找到id与属性的setter方法匹配的Bean,找到则自动注入,否则什么都不做。 |
byType | 根据属性类型自动装配,BeanFactory查找容器中的全部Bean,如果正好有一个依赖属性类型相同的Bean,则自动装配这个属性;如果有多个这样的bean,则会抛出异常;如果没有匹配的Bean,则什么都不会发生,属性不会被设置。 |
constructor | 应用于构造器参数,与byType相似,如果容器中没有与之构造器参数类型一直的bean,则会抛出异常。 |
2,配置整个配置文件中的beans根节点统一配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName">
</beans>
bean的作用域(声明周期)
其属性值有:
属性 | 说明 |
---|---|
singleton | 默认值,以单例模式创建bean的实例。 |
prototype | 每次从容其中获取bean时,都会创建一个新的实例。 |
1> 使用时:在bean标签中的scope属性中指定
<!--Eg:-->
<bean class="com.demo.pojo.User" id="user" autowire="byType" scope="singleton">
<property name="userName" value="小明"/>
<property name="id" value="2"/>
</bean>
2> 使用时:在在类上加上相应注解
/**
*此时,这个类在使用时,只能被创建一次
*/
@Scope("singleton")
@Service
public class ProviderServiceImpl implements ProviderService {
@Autowired
private ProviderMapper providerMapper;
@Override
public int addinfo(Provider provider) {
return providerMapper.addinfo(provider);
}