相关疑难解决方法(0)

使用@PropertySource注释时@Value未解析.如何配置PropertySourcesPlaceholderConfigurer?

我有以下配置类:

@Configuration
@PropertySource(name = "props", value = "classpath:/app-config.properties")
@ComponentScan("service")
public class AppConfig {
Run Code Online (Sandbox Code Playgroud)

我有财产服务:

@Component 
public class SomeService {
    @Value("#{props['some.property']}") private String someProperty;
Run Code Online (Sandbox Code Playgroud)

当我想要测试AppConfig配置类时,我收到错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String service.SomeService.someProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'props' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' 
Run Code Online (Sandbox Code Playgroud)

该问题记录在SPR-8539中

但无论如何我无法弄清楚如何配置PropertySourcesPlaceholderConfigurer 以使其工作.

编辑1

这种方法适用于xml配置

<util:properties …
Run Code Online (Sandbox Code Playgroud)

configuration spring

56
推荐指数
6
解决办法
9万
查看次数

Spring @Value没有解析为属性文件中的值

之前我曾经在其他项目中工作,我只是重新做同样的事情,但由于某种原因,它不起作用.Spring @Value不是从属性文件中读取,而是从字面上理解值

AppConfig.java

@Component
public class AppConfig
{
    @Value("${key.value1}")
    private String value;

    public String getValue()
    {
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml中:

<context:component-scan
    base-package="com.test.config" />
<context:annotation-config />

<bean id="appConfigProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:appconfig.properties" />
</bean>
Run Code Online (Sandbox Code Playgroud)

appconfig.properties

key.value1=test value 1
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我有:

@Autowired
private AppConfig appConfig;
Run Code Online (Sandbox Code Playgroud)

应用程序启动很好,但是当我这样做

appConfig.getValue()
Run Code Online (Sandbox Code Playgroud)

它返回

${key.value1}
Run Code Online (Sandbox Code Playgroud)

它不会解析为属性文件中的值.

思考?

java spring spring-properties

56
推荐指数
6
解决办法
11万
查看次数

@Controller类中的Spring @Value注释不评估属性文件中的值

我是Spring的新手,并尝试使用带@Value("${loginpage.message}")注释注释的控制器内部的注释注入一个@Controller值,并且我的字符串的值被评估为字符串"${loginpage.message}"而不是我的属性文件中的内容.

下面是我的控制器,我想要注入字符串'message'.

@Controller
public class LoginController extends BaseController {
    @Value("${loginpage.message}")
    private String message;

    @RequestMapping("/")
    public String goToLoginPage(Model model) {
        model.addAttribute("message", message);

        return "/login";
    }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序上下文如下所示:

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

<context:annotation-config />

<context:component-scan base-package="com.me.application" />
Run Code Online (Sandbox Code Playgroud)

我的属性文件有以下行:

loginpage.message=this is a test message
Run Code Online (Sandbox Code Playgroud)

Spring必须在某个时刻获取值,因为每当我更改@Value("${loginpage.message}")为不在属性文件中的值时@Value("${notInPropertiesFile}"),我都会得到异常.

spring properties

54
推荐指数
2
解决办法
9万
查看次数