spring-提取配置信息到属性文件

/spring-ioc-v2/src/main/resources/config.properties

jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///test
jdbcUsername=root
jdbcPassword=123456

/spring-ioc-v1/src/main/resources/spring-configs.xml

IOC-->Map-->Map>
-- 系统底层会读取配置文件,并将配置信息封装到Properties对象(本质上是一个map)-->



    
    
    
    
        
    
    
         
         
         
         
        
        
        
        
    

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.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 utils;
/**
 * 模拟数据源对象
 * @author Administrator
 *javax.sql.DataSource
 *模拟写一个开源的连接池
 *如何将此对象交给Spring管理
 *1)以xml的方式对此对象进行描述(在配置文件中以标签描述)
 *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 + "]";
    }
    
}

九月 04, 2018 11:32:36 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Tue Sep 04 11:32:36 CST 2018]; root of context hierarchy
九月 04, 2018 11:32:36 上午 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:mysql:///test, userName=root, password=123456, coreSize=10, maxSize=100]
九月 04, 2018 11:32:37 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Tue Sep 04 11:32:36 CST 2018]; root of context hierarchy
OpenDataSource.destory()

你可能感兴趣的:(spring-提取配置信息到属性文件)