spring学习(4):Spring加载属性(properties)文件

    xiaoxiao2023-11-09  158

    文章目录

    1.使用注解@PropertySource加载属性文件1.1@PropertySource注解1.2属性文件解析类PropertySourcesPlaceholderconfigurer 2.使用XML方式加载属性文件2.1.使用<context:property-placeholder/>元素加载属性文件2.2.配置PropertyPlaceholderConfigurer的Bean加载属性文件

    1.使用注解@PropertySource加载属性文件

    1.1@PropertySource注解

    Spring提供注解@PropertySource来加载属性文件,该注解包含的配置项有:

    name:字符串,配置这次属性配置的名称。value:字符串数组,可以配置多个属性文件。ignoreResourceNotFound:boolean值,默认为false,其含义为如果找不到对应的属性文件是否进行忽略处理,由于默认值为false,所以在默认的情况下找不到对应的配置文件会抛出异常。encoding:编码,默认为””。

    1.2属性文件解析类PropertySourcesPlaceholderconfigurer

    注意: 如果只有@PropertySource的加载,Spring只会把对应文件加载进来,而没有解析属性占位符的能力。S任凭推荐使用一个属性文件解析类进行处理,即PropertySourcesPlaceholderconfigurer,使用它就意味着允许Spring解析对应的属性文件,并通过占位符去引用对应的配置。

    (1)属性文件database-config.properties:

    jdbc.database.driver=com.mysql.jdbc.Driver jdbc.database.url=jdbc:mysql://localhost:3306/chapter10 jdbc.database.username=root jdbc.database.password=123456

    (2)使用@PropertySource注解加载数据库属性文件

    package com.ssm.chapter10.annotation.config; import java.util.Properties; import javax.sql.DataSource; 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 org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @ComponentScan(basePackageClasses = { Role.class, RoleServiceImpl.class }, excludeFilters = {@Filter(type = FilterType.REGEX, pattern="com.ssm.chapter10.annotation.config.AutowiredConfig")}) @PropertySource(value={"classpath:database-config.properties"}, ignoreResourceNotFound=true) public class ApplicationConfig { @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }

    (3)在Bean中引入属性文件的配置

    package com.ssm.chapter10.annotation.config; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @Component public class DataSourceBean { @Value("${jdbc.database.driver}") private String driver = null; @Value("${jdbc.database.url}") private String url = null; @Value("${jdbc.database.username}") private String username = null; @Value("${jdbc.database.password}") private String password = null; public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } 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; } @Bean(name = "dataSource1") public DataSource getDataSource() { Properties props = new Properties(); props.setProperty("driver", driver); props.setProperty("url", url); props.setProperty("username", username); props.setProperty("password", password); DataSource dataSource = null; try { dataSource = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { e.printStackTrace(); } return dataSource; } }

    2.使用XML方式加载属性文件

    2.1.使用<context:property-placeholder/>元素加载属性文件

    <context:property-placeholder ignore-resource-not-found="false" location="classpath:database-config.properties" /> 属性ignore-resource-not-found代表是否允许文件不存在;属性location配置属性文件路径,可以配置单个文件和多个文件,多个文件之间使用逗号分隔;

    2.2.配置PropertyPlaceholderConfigurer的Bean加载属性文件

    <!--字符串数组,可配置多个属性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:database-config.properties</value> <value>classpath:log4j.properties</value> </array> </property> <property name="ignoreResourceNotFound" value="false" /> </bean>
    最新回复(0)