一.注解方式加载属性properties
1.数据库的配置文件
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=MidnightSun user=sa password=abc123456* #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
2.扫描包
@PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound = false) public class ApplicationConfig { }
3.测试类
public static void main(String []y){ BeanTest beanTest = new BeanTest(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class); String url = context.getEnvironment().getProperty("url"); System.out.print(url);
4.下面是项目结构
二.注解方式加载属性文件,解析${}表达式
1.属性文件配置
现在maven中添加jar
<dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency>
package com.hly.spring.pojo.springBean; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import javax.sql.DataSource; import java.util.Properties; /** * @author :hly * @date :2018/5/11 */ @ComponentScan public class DataSourceBean { @Value("${driver}") private String driver = null; @Value("${url}") private String url = null; @Value("${user}") private String user = null; @Value("${password}") private String password = null; @Bean(name = "dataSource") public DataSource getDataSource() { Properties props = new Properties(); props.setProperty("driver", driver); props.setProperty("url", url); props.setProperty("user", user); props.setProperty("password", password); DataSource dataSource = null; try { dataSource = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { e.printStackTrace(); } System.out.println("url:"+url); return dataSource; } }
2.解析占位符的配置
package com.hly.spring.pojo.springBean; import com.hly.spring.ServiceImpl.RoleServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; /** * @author :hly * @date :2018/5/10 */ @ComponentScan(basePackages = {"com.hly.spring.pojo.springBean"}) @PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound = true) public class ApplicationConfig { //spring能够解析属性占位符,没有这一句将不会解析${url} @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){ return new PropertySourcesPlaceholderConfigurer(); } }
3.测试类
import com.hly.spring.pojo.springBean.ApplicationConfig; //import com.hly.spring.pojo.springBean.PojoConfig; import com.hly.spring.pojo.springBean.DataSourceBean; import com.hly.spring.pojo.springBean.Role; import com.hly.spring.pojo.springIOC.JuiceMaker; import com.hly.spring.service.RoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Component; import javax.sql.DataSource; /** * @author :hly * @date :2018/5/10 */ @Component public class BeanTest { public static void main(String[] y) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class,DataSourceBean.class); //String url = context.getEnvironment().getProperty("url"); DataSource dataSource = (DataSource) context.getBean("dataSource"); System.out.println(dataSource); } }