欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring 在配置中使用*.properties

程序员文章站 2024-01-22 09:44:16
...
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <context:annotation-config/>
    <!-- picks up and registers AppConfig as a bean definition -->
    <context:component-scan base-package="com.web.spring.other" />
    
    <bean class="com.web.spring.other.AppConfig"/>
	方法一
    <context:property-placeholder location="classpath:jdbc.properties" />
    方法二
    <util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>
</beans>
  实现一
package com.web.spring.other;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath*:spring/spring-properties.xml")
public class AppConfig {
    private @Value("${jdbc.driverClassName}") String driverClassName;
    @Bean(initMethod = "init")
    public JDBCBean jdbc(){
        JDBCBean jdbc=new JDBCBean();
        jdbc.setDriverClassName(driverClassName);
        return jdbc;
    }

}
jdbc.driverClassName=org.hsqldb.jdbcDriver
  实现二
package com.web.spring.other;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    private @Value("#{jdbcProperties.driverClassName}") String driverClassName;
    //private @Value("#{jdbcProperties['jdbc.driverClassName']}") String driverClassName;
    @Bean(initMethod = "init")
    public JDBCBean jdbc(){
        JDBCBean jdbc=new JDBCBean();
        jdbc.setDriverClassName(driverClassName);
        return jdbc;
    }

}
driverClassName=org.hsqldb.jdbcDriver

--------------------------------------------------------------------------------------------------- 一、只读取单个 properties 文件

1、在 spring 的配置文件中,加入

引入命名空间:

xmlns:util=”http://www.springframework.org/schema/util”
xsi:schemaLocation=”http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd”

 内容中写入<util:properties id=”propertiesReader” location=”classpath:test.properties” />

2、在类中需要注入的属性实现 setter 和 getter 方法。

3、在 setter 方法前,添加 @Value 注解

@Value(“#{propertiesReader[propertiesName]}”)

propertiesName 为 properties 文件中的键。这样,在容器启动过程中, Spring 将自动注入值。

二、读取多个 properties 文件与上类似,只是在配置文件写入的内容不同。

<bean id=”propertiesReader”
class=”org.springframework.beans.factory.config.PropertiesFactoryBean”>
	<property name=”locations”>
	   <list>
			<value>classpath:param.properties</value>
			<value>classpath:base.properties</value>
		</list>
	</property>
</bean>