Spring initialization, inject configuration file properties into beans

The first is the Spring configuration file

applicationContext.xml 

<!-- Open annotations-->
	<context:annotation-config/>
	<!-- Scan the package, be sure to scan the corresponding VO class -->
	<context:component-scan base-package="com.ssh.*" />
    <!-- Load the properties file for injecting into javabean -->
	<bean name="myproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    	<property name="locations">
    		<list>
    			<value>classpath:META-INF/app_config/properties/sysconfig.properties</value>
    			<value>classpath:META-INF/app_config/properties/sysconfig-debug.properties</value>
    		</list>
    	</property>
    </bean>
    <!-- Load properties file to replace placeholder variables (${varible}) in Spring configuration file -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="properties" ref="myproperties"></property>
    	<property name="locations">
    		<list>
    			<value>classpath:META-INF/app_config/properties/global.framwork.properties</value>
    		</list>
    	</property>
    </bean>

 

SysConfigVo

package com.ssh.service;

import org.springframework.stereotype.Component;
import org.springframework.bean.factory.annotation.Value;

@Component
public class SysConfig {

	//myProperties must be the same as the name of the PropertiesFactoryBean in applicationContext.xml
	@Value("#{myProperties}['gloable.model']")
	private String gloable_model;

	public String getGloable_model() {
		return gloable_model;
	}

	public void setGloable_model(String gloable_model) {
		this.gloable_model = gloable_model;
	}
	
}

 Use this VO in service

package com.ssh.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.dao.UserDao;
import com.ssh.entity.User;

@Service
public class UserService {

	@Resource
	private SysConfig sysConfig;
	logger.info(sysConfig.getGloable_model());
	
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487170&siteId=291194637