<!--系统底层会根据constructor-arg元素调用对应参数个数的构造函数-->
<constructor-arg value="10"></constructor-arg>
<constructor-arg value="100"></constructor-arg>
默认为,可以不写index
<!--系统底层会根据constructor-arg元素调用对应参数个数的构造函数
这种属性初始化方式称之为构造注入。-->
<constructor-arg index="0" value="10"></constructor-arg>
<constructor-arg index="1" value="100"></constructor-arg>
注意构造方法可以私有
private OpenDataSource(int coreSize,int maxSize) {
System.out.println("OpenDataSource.OpenDataSource()-2");
this.coreSize=coreSize;
this.maxSize=maxSize;
}
package utils;
/**
* 模拟数据源对象
* @author Administrator
*javax.sql.DataSource
*模拟写一个开源的连接池
*如何将此对象交给Spring管理
*1)以xml的方式对此对象进行描述(在配置文件中以<bean>标签描述)
*2)以注解的方式对此对象进行描述
*/
public class OpenDataSource {
private String driverClassName;
private String url;
private String userName;
private String password;
private Integer coreSize;
private Integer maxSize;
public OpenDataSource() {
System.out.println("OpenDataSource.OpenDataSource()-0");
}
public OpenDataSource(int coreSize){
System.out.println("OpenDataSource.OpenDataSource()-1");
this.coreSize=coreSize;
}
//为属性赋值的过程叫值的注入,也叫依赖注入,通过调用对象的set方法赋值
public OpenDataSource(int coreSize,int maxSize) {
System.out.println("OpenDataSource.OpenDataSource()-2");
this.coreSize=coreSize;
this.maxSize=maxSize;
}
public Integer getCoreSize() {
return coreSize;
}
public void setCoreSize(Integer coreSize) {
this.coreSize = coreSize;
}
public Integer getMaxSize() {
return maxSize;
}
public void setMaxSize(Integer maxSize) {
this.maxSize = maxSize;
}
public void init(){
System.out.println("OpenDataSource.init()");
}
public void close(){
System.out.println("OpenDataSource.destory()");
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "OpenDataSource [driverClassName=" + driverClassName + ", url=" + url + ", userName=" + userName
+ ", password=" + password + ", coreSize=" + coreSize + ", maxSize=" + maxSize + "]";
}
}
package test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBase {
protected ClassPathXmlApplicationContext ctx;
@Before
public void init(){
ctx=new ClassPathXmlApplicationContext("spring-configs.xml");
}
@After
public void close(){
ctx.close();
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
import utils.OpenDataSource;
public class TestDataSource01 extends TestBase{
@Test
public void testOpenDataSource(){
//获取bean对象
OpenDataSource ds = ctx.getBean(OpenDataSource.class);
//测试对象值是否为空
Assert.assertNotEquals(null, ds);
System.out.println(ds);
}
}
九月 04, 2018 10:34:28 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Tue Sep 04 10:34:28 CST 2018]; root of context hierarchy
九月 04, 2018 10:34:28 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-configs.xml]
OpenDataSource.OpenDataSource()-2
OpenDataSource.init()
OpenDataSource [driverClassName=com.mysql.jdbc.driver, url=jdbc:///test, userName=root, password=123456, coreSize=10, maxSize=100]
九月 04, 2018 10:34:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Tue Sep 04 10:34:28 CST 2018]; root of context hierarchy
OpenDataSource.destory()