spring3中使⽤@value注解获取属性值
在spring 3.0中,可以通过使⽤@value,对⼀些如xxx.properties⽂件中的⽂件,进⾏键值对的注⼊。
@Value有两种使⽤⽅式:@Value("#{configProperties['t1.msgname']}")或者@Value("${t1.msgname}");
其次,下⾯我们来看看如何使⽤这两形式,在配置上有什么区别:
resource和autowired注解的区别
1、@Value("#{configProperties['t1.msgname']}")这种形式的配置中有“configProperties”,其实它指定的是配置⽂件的加载对象:配置如下:
<bean id="configProperties" class="org.springframework.fig.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/config/t1.properties</value>
</list>
</property>
</bean>
这样配置就可完成对属性的具体注⼊了;
2、@Value("${t1.msgname}")这种形式不需要指定具体加载对象,这时候需要⼀个关键的对象来完成PreferencesPlaceholderConfigurer,这个对象的配置可以利⽤上⾯配置1中的配置,也可以⾃⼰直接⾃定配置⽂件路径。
如果使⽤配置1中的配置,可以写成如下情况:
<bean id="propertyConfigurer" class="org.springframework.fig.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
如果直接指定配置⽂件的话,可以写成如下情况:
<bean id="propertyConfigurer" class="org.springframework.fig.PreferencesPlaceholderConfigurer">
<property name="location">
<value>config/t1.properties</value>
</property>
</bean>
如果使⽤注解,可以写成如下情况:
@Configuration
@ComponentScan(basePackages = "fig")
public class ServiceConfiguration2 {
public ServiceConfiguration2() {
System.out.println("ServiceConfiguration zheli");
}
@Bean
public static PropertyPlaceholderConfigurer configurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource resources = new ClassPathResource( "spring/config.properties" );
ppc.setLocation(resources);
return ppc;
}
@Bean
public Configs2 configs2(@Value("${ds.user}") String user, @Value("${key1}") String key1) {
Configs2 configs = new Configs2();
configs.setApiKeyId(user);
configs.setSecretApiKey(key1);
System.out.println("in ServiceConfiguration" + configs);
return configs;
}
}
⽰例1:xml中配置PropertyPlaceholderConfigurer
1.⾸先在l中加⼊:
1. <beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.0.xsd">
的命名空间,然后
<bean id="propertyConfigurer" class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:test.properties</value>
</list>
</property>
</bean>
3.创建test.properties
abc=123
4.读取配置⽂件内容
1. import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/admin/images")
@Controller
public class ImageAdminController {
private String imageDir;
@Value("${abc}")
public void setImageDir(String val) {
this.imageDir = val;
}
}
这样就将test.abc的值注⼊了imageDir中了。
⽰例2:context:property-placeholder
有时候需要从properties⽂件中加载配置,以前的⽅式是这样的:
<bean id="jdbcProperties" class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:/spring/jdbc.properties</value>
</list>
</property>
</bean>
最近发现这样也可以,更整洁:
1. <context:property-placeholder location="classpath:spring/jdbc.properties"/>
在bean定义中依然可以通过“${}”这种⽅式来去值:
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
</bean>
说明:
<context:property-placeholder ... /> is the XML equivalent to the PropertyPlaceholderConfigurer. So, prefer that. The <util:properties/> simply factories a java.util.Properties instance that you can inject.
⽰例3:@PropertySource + Environment
在spring3.1中(不是spring3.0)还可以这样:
package fig;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
import t.annotation.PropertySource;
import nv.Environment;
@Configuration
@ComponentScan(basePackages = "fig")
@PropertySource(value = "classpath:spring/config.properties")
public class ServiceConfiguration {
@Autowired
Environment environment;
public ServiceConfiguration() {
System.out.println("ServiceConfiguration zheli");
}
/
/@Bean
public javax.sql.DataSource dataSource(){
String user = Property("ds.user");
System.out.println(user);
return null;
}
}
配置⽂件config.properties:
key1=abc
key2=aaaaaaaaaaaaaaaaaaaa
key3=bbbbbbbbbbbbbbbbbbbbbbbbbbb
key4=cccccccccc
ds.user=admin
测试类:
package fig;
import t.ApplicationContext;
import t.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ServiceConfiguration.class);
ServiceConfiguration hc2 = (ServiceConfiguration) Bean("serviceConfiguration");
hc2.dataSource();
}
}
结果:
⼗⼀⽉ 02, 2017 10:20:43 上午 t.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing t.annotation.AnnotationConfigApplicationContext@3f91beef: startup date [Thu Nov 02 10:20:43 CST 2017]; root of context hierarchy ServiceConfiguration zheli
admin
说明:
In Spring 3.0, you can "access" properties defined using the PropertyPlaceHolderConfigurer mechanism using the SpEl annotations: PropertyPlaceHolderConfigurer可以在xml中配置,如上⾯提过的。
@Value("${ds.user}")
private String user;
If you want to remove the XML all together, simply register the PropertyPlaceholderConfigurer manually using Java configuration. I prefer the 3.1 approach. But, if youre using the Spring 3.0 approach (since 3.1's not ), you can now define the above XML like this:
如果不采⽤xml配置,在java编码配置中可以⽤如下的注册⽅式来注册PropertyPlaceholderConfigurer,如下
⽰例4: PropertyPlaceholderConfigurer + @Value
package fig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.fig.PropertyPlaceholderConfigurer;
import t.annotation.Bean;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
import io.ClassPathResource;
import io.Resource;
@Configuration
@ComponentScan(basePackages = "fig")
public class ServiceConfiguration2 {
public ServiceConfiguration2() {
System.out.println("ServiceConfiguration zheli");
}
@Bean
public static PropertyPlaceholderConfigurer configurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource resources = new ClassPathResource( "spring/config.properties" );
ppc.setLocation(resources);
return ppc;
}
@Bean
public Configs2 configs2(@Value("${ds.user}") String user, @Value("${key1}") String key1) {        Configs2 configs = new Configs2();
configs.setApiKeyId(user);
configs.setSecretApiKey(key1);
System.out.println("in ServiceConfiguration" + configs);
return configs;
}
}
package fig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestConfigs2 {
@Autowired
Configs2 configs2;
@Autowired
Configs configs;
public void testConfigs2() {
System.out.println("configs:"+ApiKeyId());
System.out.println("configs2:"+ApiKeyId());
}
}
package fig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Configs {
@Value("${key1}")
public String apiKeyId;
@Value("${ds.user}")
public String secretApiKey;
public String getApiKeyId() {
return apiKeyId;
}
public String getSecretApiKey() {
return secretApiKey;
}
}
package fig;
//@Component
public class Configs2 {
public String apiKeyId;
public String secretApiKey;
public String getApiKeyId() {
return apiKeyId;
}
public void setApiKeyId(String apiKeyId) {
this.apiKeyId = apiKeyId;
}
public String getSecretApiKey() {
return secretApiKey;
}
public void setSecretApiKey(String secretApiKey) {
this.secretApiKey = secretApiKey;
}
@Override
public String toString() {
return "Configs2 [apiKeyId=" + apiKeyId + ", secretApiKey=" + secretApiKey + "]";
}
}
测试类:
package fig;
import t.ApplicationContext;
import t.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ServiceConfiguration2.class);
TestConfigs2 hc2 = (TestConfigs2) Bean("testConfigs2");
}
}
结果:
ServiceConfiguration zheli
ServiceConfiguration zheli
in ServiceConfigurationConfigs2 [apiKeyId=admin, secretApiKey=abc]
configs:abc
configs2:admin
说明:
1、static类型的会优先在spring容器中注册。Note that the PPC is defined using a static bean definition method. This is required to make sure the bean is registered early, because the PPC is a BeanFactoryPostProcessor - it can influence the registration of the beans themselves in the context, so it necessarily has to be registered before everything else.
2、上⾯⽰例中有2中场景:
configs是spring容器管理的bean,configs的成员变量通过@Value注解获取配置⽂件的配置值;
configs2是普通的pojo类,没有注册到spring容器中,在初始化时,通过其创建⽅法中通过@Value注解获取传⼊配置⽂件中的配置值;
不要忘记这时候需要⼀个关键的对象来完成PreferencesPlaceholderConfigurer来⽀持;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。