Spring c-namespace问题 - 构造函数参数' _street'指定无效的整数

时间:2017-03-25 17:44:39

标签: spring

我在尝试将文字值注入构造函数时使用c-namespace。如果我通过名称引用构造函数参数,我遇到了一个问题。如果我通过位置(即0,1,2 ......)来引用它,它就可以完美地运行。

我在eclipse中设置了变量调试检查(我正在使用STS) - Windows>偏好> Java>编译器>将变量属性添加到生成的类文件中,但我仍然看到相同的问题

以下是我的代码 -

接口

package com.nilaysundarkar.spring.demos;

public interface Person {
public void speak();
}

实施

package com.nilaysundarkar.spring.demos;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.inject.Named;

@Component
@Named("Russland")
public class Russian implements Person, BeanNameAware {

private String language = "Russian";
private Address address;

@Autowired
public Russian(Address address){
    this.address = address;
}

public void speak() {
    System.out.println("I am a Person. I speak " + language + ".");
    System.out.println("I stay at " + address + ".");
}

public void setBeanName(String beanName) {
    System.out.println(beanName + " has been initialized.");
}

}

地址类

package com.nilaysundarkar.spring.demos;

public class Address {

private String street;

public Address(String street){
    this.street = street;
}

public String toString(){
    return street;
}

}

引导上下文 -

package com.nilaysundarkar.spring.demos;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

public static void main(String[] args) {
    ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
    Person person = context.getBean(Person.class);
    person.speak();
    ((FileSystemXmlApplicationContext) context).close();
}

}

的beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
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.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.nilaysundarkar.spring.demos"/>


<!-- Works -->
<!--<bean id="address" class="com.nilaysundarkar.spring.demos.Address" c:_0="200 Baker Street" />-->


<!-- Does'nt Work -->
<bean id="address" class="com.nilaysundarkar.spring.demos.Address" c:_street="200 Baker Street" />

</beans>

使用c:_street -

运行时出错
Mar 25, 2017 1:39:44 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@179d3b25: startup date [Sat Mar 25 13:39:44 EDT 2017]; root of context hierarchy
Mar 25, 2017 1:39:44 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Users\Nilay\Documents\workspace-sts-3.8.3.RELEASE\demos\beans.xml]
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Constructor argument '_street' specifies an invalid integer
Offending resource: file [C:\Users\Nilay\Documents\workspace-sts-3.8.3.RELEASE\demos\beans.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:72)
at org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler.decorate(SimpleConstructorNamespaceHandler.java:111)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateIfRequired(BeanDefinitionParserDelegate.java:1448)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1427)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1415)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:301)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:190)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:169)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
at com.nilaysundarkar.spring.demos.App.main(App.java:9)

使用c:_0

运行时
Mar 25, 2017 1:42:35 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@179d3b25: startup date [Sat Mar 25 13:42:35 EDT 2017]; root of context hierarchy
Mar 25, 2017 1:42:35 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Users\Nilay\Documents\workspace-sts-3.8.3.RELEASE\demos\beans.xml]
Mar 25, 2017 1:42:36 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Russland has been initialized.
I am a Person. I speak Russian.
I stay at 200 Baker Street.
Mar 25, 2017 1:42:36 PM org.springframework.context.support.FileSystemXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.FileSystemXmlApplicationContext@179d3b25: startup date [Sat Mar 25 13:42:35 EDT 2017]; root of context hierarchy

pom.xml -

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nilaysundarkar.spring</groupId>
<artifactId>demos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
</dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

正确的方法是使用c:street而不是c:_street,因为c:前缀后面的值必须与构造函数参数名称匹配。