JavaEE-SSM:023 Spring 加载属性(properties)文件

配置文件就是一些properties文件:

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

使用配置文件方便更改而不是硬编码。

使用注解读取properties文件:

@PrppertySource(value={"classpath:database-config.properties"},ignoreResourceNotFound=true)
public class ApplicationConfig{

}
ApplicationContext content = new AnnotationConfigApplicationContext(ApplicationConfig.class);

String url = context.getEnvironment().getProperty("jdbc.database.url");

使用XML方式加载属性文件

<context:property-placeholder ignore-resource-found = "true" location="classpath:database-config.properties"/>

<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="true"/>

</bean>

猜你喜欢

转载自blog.csdn.net/Day_and_Night_2017/article/details/84644403