English 中文(简体)
信库和不动产持有人
原标题:MessageSource and PropertyPlaceholderConfigurer cannot load messages but with classpath*

My applicationContext.xml is in path:

src/main/resources/META-INF/spring

and property files are in path:

src/main/resources/messages

and I load spring context in web.xml as follows:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:META-INF/spring/applicationContext.xml</param-value>
</context-param>

当我把电文和<密码”混为一谈时。 PropertyPlaceholderConfigurer :

<bean id="propertyPlaceholderConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:messages/apps.properties</value>        
        </list>
    </property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:messages/ValidationMessages</value>
            <value>classpath:messages/app</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

they both don t work, it only works when I change classpath to classpath* Any ideas why?

最佳回答
问题回答

Look at this excellent article for classpath v classpth* difference in regards to spring resource loading, I did some testing on your problem in my testing it worked whether i used classpath or classpath*

I am listing the code here about the test i did

  • I created a this directory structure (META-INF/spring under src/main/resource) placed context.xml
  • 我在此列出完整的背景。

       <?xml version="1.0" encoding="UTF-8"?>
         <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <bean id="propertyPlaceholderConfigurer"
                 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:messages/apps.properties</value>
            </list>
        </property>
      </bean>
    
        <bean class="prasanna.service.TestBean">
           <property name="appName" value="${appname}"></property>
         </bean>
    
    <bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
         <list>
           <value>classpath:messages/ValidationMessages</value>
               <value>classpath:messages/apps</value>
           </list>
       </property>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
        </beans>
    
  • 库存。

     appname=spring mvc app
    
  • 职业资格清单

      error.name=Invalid name
    
  • 测验

        public class TestBean 
        {
            private String appName;
    
            public String getAppName() {
                return appName;
            }
    
            public void setAppName(String appName) {
                this.appName = appName;
            }
    
        }
    
  • 我正在使用一个简单的java类别来装载财产档案并阅读这些档案。

    public class LoadContext 
    {
        public static void main(String[] args) 
        {
            ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:META-INF/spring/context.xml"});
            ReloadableResourceBundleMessageSource msgs = ctx.getBean(ReloadableResourceBundleMessageSource.class);
    
                  TestBean testBean = ctx.getBean(TestBean.class);
        Assert.assertTrue(testBean.getAppName().equals("spring mvc app"));
    
            String msg = msgs.getMessage("appname", new Object[]{new DefaultMessageSourceResolvable("appname")}, null);
            System.out.println(" "+ msg);
    
            String msg2 = msgs.getMessage("error.name", new Object[]{new DefaultMessageSourceResolvable("error.name")}, null);
            System.out.println(" "+ msg2);
        }
    
    }
    




相关问题
Recommended way to develop using Jetty and Eclipse

I am currently developing a J2EE application and I would like to use Jetty. I would like to have iot integrated with Eclipse, so I could debug the appliaction. I ve tried out couple of plugins (...

Call function periodically in Java

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server ...

Why make an EJB rather than a Web Service?

I would have thought that there is a lot of information out there on this, but I haven t found anything that really answers my question. What are the advantages of making an EJB rather than a web ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

JNDI Names -- Is Prefix "jdbc/" needed?

What s up with JNDI names? I m trying to get a javax.sql.DataSource using the new annotations feature of Java 5. It s not working for me, so I want to ask... I have a in my web.xml, inside of it is ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

热门标签