Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@Value注释和setter注入差异_Java_Spring - Fatal编程技术网

Java Spring@Value注释和setter注入差异

Java Spring@Value注释和setter注入差异,java,spring,Java,Spring,当我使用属性文件中的@value注释设置值时,它不起作用,而使用setter注入的设置起作用 我的属性文件是这样的 app.work.dir=file:${user.home}/WORK 类文件,如下所示 @Value("#{configProperties['app.work.dir']}") private String workdir; 这是我的xml设置 <util:properties id="configProperties" location="classpath:con

当我使用属性文件中的@value注释设置值时,它不起作用,而使用setter注入的设置起作用

我的属性文件是这样的

app.work.dir=file:${user.home}/WORK
类文件,如下所示

@Value("#{configProperties['app.work.dir']}")
private String workdir;
这是我的xml设置

<util:properties id="configProperties" location="classpath:config.properties" />
属性文件:

work.dir=file:${user.home}/WORK
work.dir.test=${work.dir}/TEST
aaa=bbb
和junit测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/testcontext.xml" })
public class ExampleTest {

    @Inject
    private SampleA sampleA;

    @Inject
    private SampleB sampleB;

    @Test
    public void testValueAnnotation() {

        assertThat(sampleA.getAaa(), is("bbb"));//ok
        String tempdir = sampleA.getTempdir();
        String workdir = sampleA.getWorkdir();
        assertFalse("[sampleA] temp dir should not have ${work.dir}", tempdir.indexOf("${work.dir}") >= 0);//ng
        assertFalse("[sampleA] workdir dir should not have ${user.home}", workdir.indexOf("${user.home}") >= 0);//ng
    }


}
生成的bean

<util:properties id="sampleProperties" location="classpath:spring.properties" />
请求使用键
work.dir
映射的其中一个值。返回的值没有发生属性解析。这是字面意思

另一方面,

<property name="tempdir" value="${work.dir.test}" />


请求一个名为
work.dir.test
的属性,该属性可以递归解析。

当use@Value可以获取文件:{user.home}/work但use setter injection可以获取文件:/Users/hoge/work。发布完整配置。你有属性解析器吗?预期结果是什么?添加了完整配置请参考感谢您的回答。您的回答是@Value annotation不能支持递归属性设置吗?@rakuraku不,可以。使用
@Value(“${work.dir}”)
,它的行为将与您的XML示例相同。是
{…}
符号不进行属性解析。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/testcontext.xml" })
public class ExampleTest {

    @Inject
    private SampleA sampleA;

    @Inject
    private SampleB sampleB;

    @Test
    public void testValueAnnotation() {

        assertThat(sampleA.getAaa(), is("bbb"));//ok
        String tempdir = sampleA.getTempdir();
        String workdir = sampleA.getWorkdir();
        assertFalse("[sampleA] temp dir should not have ${work.dir}", tempdir.indexOf("${work.dir}") >= 0);//ng
        assertFalse("[sampleA] workdir dir should not have ${user.home}", workdir.indexOf("${user.home}") >= 0);//ng
    }


}
<util:properties id="sampleProperties" location="classpath:spring.properties" />
@Value("#{sampleProperties['work.dir']}")
<property name="tempdir" value="${work.dir.test}" />