2012-11-16 57 views
0

read this article on SO,并有一些澄清问题。创建和访问属性文件在春天mvc

我把我的config.properties在src /主/资源

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 

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

UPDATE :: 的src /主/爪哇/ 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 /主/爪哇/ com.companyname.services/MyService.java

public class MyServiceObject { 

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


    public String PropertyValue() { 

     return _dbServerIPaseURL; 
    } 



} 

Another site where I found the explanation

回答

0

请检查您从org.springframework.beans.factory.annotation包进口值:

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

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

更新你混淆了property-placeholder是后期处理豆值,包含美元符号${<property name>}Spring expression language container extension这个过程包含一个哈希符号#{<Spring expression language expression>}值中的链接,你已经证明了后一种方法被使用。

关于MyServiceObject MyObj中的实例 如果你希望对象管理的春天,你应该委托其创作的容器:

  • 如果MyServiceObject是一个无状态的服务的话,它与一个单身单身豆范围,你应该在你的应用程序上下文中有下面的XML配置寄存器它,例如:

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

    的ð它注入到你的控制器:

    private MyServiceObject myServiceObject; 
    @Autowired 
    public void setMyServiceObject(MyServiceObject myServiceObject){ 
        this.myServiceObject = myServiceObject; 
    } 
    
  • 如果需要的MyServiceObject很多情况下,你可以声明它与其他一些(非单)bean的作用域豆(原型,或请求,例如)。 但是,由于只有一个控制器实例,因此只能让Spring容器自动将MyServiceObject实例连接到控制器字段,因为只有一个字段和许多MyServiceObject类实例。您可以在the respective section of the documentation中阅读有关解决此问题的不同方法(针对不同的bean作用域)。

+0

如果不是在控制器中,相应的上下文配置文件位于何处? – user1361914

+0

在控制器java文件中更改导入是否解决了问题? Spring MVC中,你通常有IoC容器和至少两个配置文件http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s/11709272#11709272 –

+0

如果它是一个dao或一个服务(而不是控制器),那么它将成为根Web应用程序上下文配置文件,我相信。当然,假设DAO在根web应用上下文中被实例化。 –

0

这里是一个方法,允许我们从属性文件中获取任何需要的值。这可以在Java代码(Controller类)中或在JSP中。

  • 创建一个属性文件WEB-INF /班/ messageSource.properties这将是在两个控制器和JSTL类路径和访问。 喜欢的任何属性的文件,这其中包括键和值

    。例如:
    你好=你好JSTL,你好位指示

为控制器

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

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

    • 豆ID = “为messageSource”

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

    • 属性名称= “基本名称” 值= “WEB-INF/classes/messageSource

  • 在控制器类的bean定义文件(testCont滚筒)添加messageSource作为一个属性。这会将messageSource bean注入到控制器中。

    • 豆ID = “的TestController” 级= “com.app.springhr.TestController”
    • 豆:属性名= “为messageSource” REF = “为messageSource
  • 在控制器JAVA类,添加messageSource Spring Bean字段及其getter和setter。请注意,字段类型是ReloadableResourceBundleMessageSource。

    private org.springframework.context.support。 ReloadableResourceBundleMessageSource messageSource;

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

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

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

    字符串propValue = getMessageSource()的getMessage( “你好”,objArray,NULL);

使用JSTL

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

  • 添加FMT的taglib

    • 的taglib URI = “http://java.sun.com/jsp/jstl/fmt” PREFIX = “FMT”

    • 进口

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

      对不起,关于伪语法,这个编辑器似乎没有渲染任何XML。

      FMT:束基本名称= “为messageSource”

      FMT:消息密钥= “你好”

    • 希望这有助于他人