Spring加载Properties配置文件,java通过注解读取数据

 1、用法示例: 在spring.xml配置文件中添加标签

<context:property-placeholder location="classpath:salesman.properties"/>

 2.方式是使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值 ,

   注意:变量不能用static修饰;

<!-- 第二种方式是使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值 -->
 <bean id="PropertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 	<property name="locations"><!-- 这里是PropertiesFactoryBean类,它也有个locations属性,也是接收一个数组,跟上面一样
 		<array>
 			<value>classpath:salesman.properties</value>
 		</array>
 	</property>
    <property name="fileEncoding" value="UTF-8"/>
 </bean>



@Component("fileUpload")
public class FileUploadUtil implements FileUpload {

	@Value("#{PropertiesFactory.filePath}") 
	private String filePath;
	//@Value表示去beans.xml文件中找id="prop"的bean,它是通过注解的方式读取properties配置文件的,然后去相应的配置文件中读取key=filePath的对应的value值
	public void setFilePath(String filePath) {
		System.out.println(filePath);
		this.filePath = filePath;

猜你喜欢

转载自blog.csdn.net/qq_15204179/article/details/82178802
今日推荐