相关疑难解决方法(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万
查看次数

@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万
查看次数

Spring 3.0.5不会从属性中评估@Value注释

尝试在Spring 3.0.5.RELEASE中将属性自动连接到bean ,我正在使用:

因此,用户名被设置为字面意思 "${username}",因此表达式不会被解析.我对此类的其他自动连接依赖项进行了设置,Spring不会抛出任何异常.我也尝试添加,@Autowired但它没有帮助.

如果我将属性解析为单独的bean然后使用@Autowired+ @Qualifier,它可以工作:

<bean id="username" class="java.lang.String">
    <constructor-arg value="${username}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

任何想法如何使用只是@Value?也许我需要包含一些我没有的Spring依赖项?谢谢

spring frameworks properties autowired

29
推荐指数
1
解决办法
4万
查看次数