Spring 注入int属性时的NumberFormatException

Spring 注入int属性时的NumberFormatException,spring,setter,numberformatexception,Spring,Setter,Numberformatexception,这是我的班级: ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); PropertyPlaceholderConfigurer pph = new PropertyPlaceholderConfigurer(); pph.setLocations(new Resource[]{new ClassPathResource("one.properties"), new Cl

这是我的班级:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
PropertyPlaceholderConfigurer pph = new PropertyPlaceholderConfigurer();
pph.setLocations(new Resource[]{new ClassPathResource("one.properties"), new ClassPathResource("two.properties")});
context.addBeanFactoryPostProcessor(pph);
context.refresh();

Controller obj1 = (Controller) context.getBean("controller");
System.out.println(obj1.getMessage());

Controller2 obj2 = (Controller2) context.getBean("controller2");
System.out.println(obj2.getMessage());
System.out.println(obj2.getInteger());
这是相关的xml配置:

   <bean id="controller" class="com.sample.controller.Controller">
       <property name="message" value="${ONE_MESSAGE}"/>
   </bean>
   <bean id="controller2" class="com.sample.controller.Controller2">
       <property name="message" value="${TWO_MESSAGE}"/>
        <property name="integer" value="${TWO_INTEGER}"/>
   </bean>
二、物业:

ONE_MESSAGE=ONE
TWO_MESSAGE=TWO
TWO_INTEGER=30
将双_消息正确分配为字符串2。 当注入一个2_整数时,我得到NumberFormatException。 有没有一种方法可以在不添加setter的情况下实现这一点,setter接受String并在Controller2类中将其转换为int

错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controller2' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'integer'; nested exception is java.lang.NumberFormatException: For input string: "${TWO_INTEGER}"

谢谢。

您的应用程序可能属于这一行(如果我出错,请提供完整的stacketrace):

因为Spring无法解析
${TWO_INTEGER}
(这个属性还没有加载到上下文中)。因此,您可以在加载属性后移动上下文初始化:

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
 PropertyPlaceholderConfigurer pph = new PropertyPlaceholderConfigurer();
 pph.setLocations(new Resource[]{new ClassPathResource("one.properties"), new ClassPathResource("two.properties")});
 context.addBeanFactoryPostProcessor(pph);
 context.setConfigLocation("beans.xml");
 context.refresh();

希望有此帮助。

PropertyPlaceHolderConfigure显然找不到属性文件,或者属性文件不包含任何类似
${TWO\u INTEGER}
的属性。你查过了吗?我查过了。事实上,同一文件中的其他字符串属性被正确分配。1.properties有:
one\u MESSAGE=one
和two.properties有:
two\u MESSAGE=two-two-two\u INTEGER=30
和“two\u MESSAGE”属性读取正确。只是,澄清一下。两行中有两行。属性的格式与此处显示一行的格式不同。谢谢。您可以编辑问题并添加此相关信息。它的格式最好是在注释中。为什么不能在xml文件中定义PropertyPlaceHolderConfigure?LaurentG:编辑了这个问题@bellabax:属性文件名在运行时决定。除非有一种方法在xml文件中定义它,然后通过编程对其进行更改,否则我必须这样做。谢谢,现在可以用了。非常感谢您的解答和解释。非常感谢。好奇:为什么字符串属性即使在同一个文件中也能正确读取?
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
 PropertyPlaceholderConfigurer pph = new PropertyPlaceholderConfigurer();
 pph.setLocations(new Resource[]{new ClassPathResource("one.properties"), new ClassPathResource("two.properties")});
 context.addBeanFactoryPostProcessor(pph);
 context.setConfigLocation("beans.xml");
 context.refresh();