Profiles related

1. Load xml configuration by way 

  Spring to instantiate dataSource for example, first create a new conn.properties file in the src directory of the project, which is written on the top dataSource configuration:

dataSource=com.mchange.v2.c3p0.ComboPooledDataSource  
driverClass=com.mysql.jdbc.Driver  
jdbcUrl=jdbc:mysql://localhost:3306/shop 
username=root  
password=root  

 

  Then only you need to make the following changes in the beans.xml:

<context: Property-placeholder LOCATION = "CLASSPATH: conn.Properties" /> <-! Load profile ->   
  
 ! <- com.mchange.v2.c3p0.ComboPooledDataSource class package c3p0-0.9.5.1.jar the package com.mchange.v2.c3p0 ->   
 <the bean ID = "the dataSource" class = " $ {} the dataSource "> <-! Spring these configurations will go to find the start of conn.properties ->   
    < Property name = "driverClass" value = " $ {driverClass} " />   
    <Property name = "the jdbcUrl" value = " $ {the jdbcUrl} " />   
    <Property name = "User" value = " $ {User} " />   
    <Property name = "password" value = " $ {password} " />  
 </bean> 

 

  @Value configuration property values ​​acquired by the java code

    @Value("${shop.url}")
    private String url;

 

2.0 spring load configuration notes

 

@Configuration
@PropertySource("classpath:jdbc.properties")
//@ConfigurationProperties(prefix = "demo")使用这个可以省略@value
public class JdbcConfig{ @Value("jdbcUrl") String jdbcUrl; @Value("driverClass") String driverClass; @Value("user") String user; @Value("password") String password @DataSource public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDrieverClassName(driverClass); dataSource.setUrl(jdbcUrl); dataSource.setUserName(user); dataSource.setPassword(password); return dataSource; } }

  

Guess you like

Origin www.cnblogs.com/helloworldmybokeyuan/p/11622358.html