spring--加载资源文件

 

1.resources下(src/main/resources)

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("authfile.txt");
 Properties  properties = new Properties();
            properties.load(in);

 

2

Properties props = PropertiesLoaderUtils.loadAllProperties("authfile.txt");

 

3

File files = ResourceUtils.getFile("classpath:authfile.txt");

 

 

4

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
    <property name="location" value="(propertis文件名)Xxx.properties" />
</bean>



三、访问properties中的内容:
<bean id="Xxx" ....>
        <property name="Xxx" value=" ${键} " />
        //${键},键properties文件中的键值对的键 
</bean>

 

5

@Component
  @PropertySource("classpath:db.properties")
  public class DBConnection {

    @Value("${DB_DRIVER_CLASS}")
    private String driverClass;

    @Value("${DB_URL}")
    private String dbUrl;

    @Value("${DB_USERNAME}")
    private String userName;

    @Value("${DB_PASSWORD}")
    private String password;

 

posted @ 2020-11-13 16:52  jentary  阅读(158)  评论(0编辑  收藏  举报