##spring4.3 引入***.yml配置文件mysql
YamlPropertiesFactoryBeanredis
yml 後綴的配置文件spring
#mysql mysql: jdbc: url: jdbc:mysql://localhost:3306 dirverClass: com.mysql.jdbc.Driver username: root password: root #redis #mongodb
spring 配置文件sql
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean"> <property name="resources"> <list> <value>classpath:application.yml</value> </list> </property> </bean>
spring 測試類mongodb
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring-beans.xml"}) public class SpringDemo { @Autowired public ApplicationContext applicationContext; @Autowired @Qualifier("yamlProperties") private Properties yamlProperties; @Test public void test() { String jdbcurl = yamlProperties.getProperty("mysql.jdbc.url"); String dirverClass = yamlProperties.getProperty("mysql.jdbc.dirverClass"); String username = yamlProperties.getProperty("mysql.jdbc.username"); String password = yamlProperties.getProperty("mysql.jdbc.password"); System.out.println(jdbcurl); System.out.println(dirverClass); System.out.println(username); System.out.println(password); } }
YamlMapFactoryBeanapp
yml 後綴的配置文件curl
#mysql mysql: jdbc: url: jdbc:mysql://localhost:3306 dirverClass: com.mysql.jdbc.Driver username: root password: root #redis #mongodb
spring 配置文件測試
<bean id="yamlMap" class="org.springframework.beans.factory.config.YamlMapFactoryBean"> <property name="resources"> <list> <value>classpath:application.yml</value> </list> </property> </bean>
spring 測試類url
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring-beans.xml"}) public class SpringDemo { @Autowired public ApplicationContext applicationContext; @Autowired private Map<String, Object> yamlMap; @Test public void test() { Map<String, Object> enviroment = (Map<String, Object>) yamlMap.get("yamlMap"); Map<String, Object> mysql = (Map<String, Object>) enviroment.get("mysql"); Map<String, Object> jdbc = (Map<String, Object>) mysql.get("jdbc"); String url = (String) jdbc.get("url"); System.out.println(url); } }