0

阅读关于 SO 的这篇文章,并有一些澄清问题。

我将 config.properties 放在 src/main/resources 下

spring-servlet.xml config file

我添加了以下内容:

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

在我的业务层中,我试图通过

@Value("${upload.file.path}")
private String uploadFilePath;

Eclipse 显示错误:

The attribute value is undefined for the annotation type Value

我不能访问业务层中的属性还是只能在控制器中读取属性文件?

更新:: src/main/java/com.companyname.controllers/homecontroller.java

public String home(Locale locale, Model model) {
    MyServiceObject myObj = new MyServiceObject();
    System.out.println("Property from my service object: = " + myObj.PropertyValue());

    if(myObj.PerformService())
    {
      ///
    }
}

src/main/java/com.companyname.services/MyService.java

public class MyServiceObject {

    @Value("${db.server.ip}")
    private String _dbServerIP;


    public String PropertyValue() {

        return _dbServerIPaseURL;
    }



}

我找到解释的另一个网站

4

2 回答 2

0

请检查您是否从 org.springframework.beans.factory.annotation 包中导入了 Value:

import org.springframework.beans.factory.annotation.Value;

属性占位符也必须在相应的上下文配置文件中声明,如果是控制器,它可能是 Spring 调度程序 servlet 配置文件。

更新property-placeholder对 post 处理包含美元符号${<property name>}的bean 值感到困惑, Spring 表达式语言容器扩展处理包含哈希符号的值#{<Spring expression language expression>},在您显示的链接中使用了后一种方法。

关于 MyServiceObject myObj 的实例化 如果您希望对象由 Spring 管理,您应该将其创建委托给容器:

  • 如果MyServiceObject是无状态服务,那么它是具有单例 bean 范围的单例,您应该在应用程序上下文中注册它,例如使用以下 xml 配置:

    <bean class="my.package.MyServiceObject"/>
    

    并将其注入您的控制器:

    private MyServiceObject myServiceObject;
    @Autowired 
    public void setMyServiceObject(MyServiceObject myServiceObject){
       this.myServiceObject = myServiceObject;
    }
    
  • 如果需要许多实例MyServiceObject,您可以将其声明为具有其他(非单例)bean 范围(例如原型或请求)的 bean。但是,由于控制器只有一个实例,因此不能仅仅让 Spring 容器将MyServiceObject实例自动装配到控制器字段,因为将只有一个字段和多个MyServiceObject类实例。您可以在文档的相应部分了解解决此问题的不同方法(针对不同的 bean 范围)。

于 2012-11-16T16:59:47.603 回答
0

这是一种允许我们从属性文件中获取所需值的方法。这可以在 Java 代码(控制器类)或 JSP 中。

  • 创建一个属性文件 WEB-INF/classes/ messageSource.properties它将位于类路径中,并且可以在控制器和 JSTL 中访问。像任何属性文件一样,这个文件由一个键和一个值组成

    例如:
    hello=Hello JSTL、Hello Contoller

对于控制器

  • 找到用于定义 Spring bean 的 xml 文件。在我的例子中,它被命名为 servlet-context.xml。在使用 servlet contextConfigLocation 属性的情况下,您可能需要通过查看 web.xml 来确定这一点。

  • 使用 id="messageSource" 添加一个新的 Spring bean 定义。这个 bean 将在运行时由 Spring 与属性文件键值对一起加载。使用以下属性创建 bean:

    • bean id="messageSource"

    • 类 = org.springframework.context.support.ReloadableResourceBundleMessageSource

    • 属性名称="basename" 值="WEB-INF/classes/messageSource

  • 在控制器类 (testController) 的 bean 定义文件中,添加 messageSource 作为属性。这会将 messageSource bean 注入控制器。

    • bean id="testController" 类="com.app.springhr.TestController"
    • 豆类:属性名称=“消息来源”参考=“消息来源
  • 在控制器 JAVA 类中,添加 messageSource Spring Bean 字段及其 getter 和 setter。注意字段类型是 ReloadableResourceBundleMessageSource。

    私人 org.springframework.context.support。ReloadableResourceBundleMessageSource消息源;

    公共 org.springframework.context.support。ReloadableResourceBundleMessageSource getMessageSource() { return messageSource; }

    公共无效 setMessageSource(org.springframework.context.support.ReloadableResourceBundleMessageSource messageSource){ this.messageSource = messageSource; }

  • 在您的控制器代码中,您现在可以从包中获取任何已知的属性值。

    String propValue = getMessageSource().getMessage("hello", objArray, null);

使用 JSTL

由于属性文件 messageSource.properties 在类路径中,JSTL 将能够找到它并获取给定键的值。

  • 添加 fmt taglib 的导入

    • taglib uri="http://java.sun.com/jsp/jstl/fmt" 前缀="fmt"

    • 使用 JSP 中的 fmt:tag 从属性文件中获取值。

      抱歉伪语法,这个编辑器似乎没有呈现任何 XML。

      fmt:bundle basename="messageSource"

      fmt:消息键="你好"

    • 希望这对其他人有帮助

于 2015-02-05T22:09:19.813 回答