luckystar +
send mail to luckystar sina weibo
关于订阅 luckystar's blog » Project »
分类:
标签:

在Spring3中,可以通过@Value注解为Bean的属性注入配置文件中的配置。
配置文件applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.hyxt"></context:component-scan>

    <bean id="propertyConfiguer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config.properties</value>
            </list>
        </property>
    </bean>
</beans>

一个测试的Service:

package com.hyxt.cache.ehcache.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.rmi.transport.ObjectTable;

import java.util.Date;

/**
 * Created by qince on 2015/3/24.
 */
@Service
public class ConfigTestService {

    @Value(value = "${name}")
    private String name;
    @Value(value = "${age}")
    private int age;
    @Value(value = "${birthday}")
    private String birthday;

    public Object[] getConfigs() {
        Object[] configArray = new Object[3];
        configArray[0] = name;
        configArray[1] = age;
        configArray[2] = birthday;

        return configArray;
    }
}

测试类:

package com.hyxt.cache.ehcache.demo;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class ConfigTestServiceTest {
    private ConfigTestService configTestService;
    @Before
    public void setUp() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        configTestService = applicationContext.getBean(ConfigTestService.class);
    }

    @After
    public void tearDown() throws Exception {
        configTestService = null;
    }

    @Test
    public void testGetConfigs() throws Exception {
        System.out.println("测试通过注解的方式注入Bean的属性");
        Object[] result =configTestService.getConfigs();
        if (null != result) {
            for (Object item : result) {
                System.out.println(item);
            }
        }
    }
}
<bean id="propertyConfiguer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config.properties</value>
        </list>
    </property>
</bean>

也可以换成:

<context:property-placeholder file-encoding="UTF-8" location="config.properties"/>
作者:qincidong
出处:http://qincidong.github.io/blog/2015/03/24/spring-value-annotation.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
分类: 标签:
友荐云推荐