spring+junit4+注解方式PropertiesFactoryBean读取properties

下载链接地址
http://pan.baidu.com/s/1bonE9Uf

spring+junit4+注解方式PropertiesFactoryBean读取properties_第1张图片

db.properties

jdbc.username=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///db_book

jdbc.initPoolSize=5
jdbc.maxPoolSize=10

JDBCProperties

package com.konglingfu.propertiesDemo;

import org.springframework.beans.factory.annotation.Value;

public class JDBCProperties {


    @Value("#{configProperties['jdbc.username']}")
    private String userName;

    @Value("#{configProperties['jdbc.jdbcUrl']}")
    private String jdbcUrl;

    @Value("#{configProperties['jdbc.password']}")
    private String password;

    @Value("#{configProperties['jdbc.driverClass']}")
    private String driverClass;

    @Value("#{configProperties['jdbc.initPoolSize']}")
    private String initPoolSize;


    @Override
    public String toString() {
        return "JDBCProperties [userName=" + userName + ", jdbcUrl=" + jdbcUrl
                + ", password=" + password + ", driverClass=" + driverClass
                + ", initPoolSize=" + initPoolSize + "]";
    }


}

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.konglingfu.propertiesDemo.JDBCProperties;



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class PropertiesTest extends AbstractJUnit4SpringContextTests  {

    @Autowired
    private JDBCProperties jdbcProperties;

    @Test
    public void testPrintProperties(){
        String result = jdbcProperties.toString();
        System.out.println(result);
    }



}

输出结果

JDBCProperties [userName=root, jdbcUrl=jdbc:mysql:///db_book, password=root, driverClass=com.mysql.jdbc.Driver, initPoolSize=5]

你可能感兴趣的:(spring,Junit)