0
リテラル値をコンストラクタに挿入しているときにc-namespaceの使用を試みています。その名前でコンストラクタの引数を参照すると、問題に遭遇しています。その位置(つまり、0,1,2 ...)で参照すると完全に正常に動作します。Springのc-namespaceの問題 - コンストラクタの引数 '_street'が無効な整数を指定しています
私は(私はSTSを使用しています)日食に変数のデバッグチェックを設定している - Windowsの>設定>ジャワ>コンパイラ>は生成されたクラスファイルに変数の属性を追加しますが、私はまだ同じ問題以下
が私のコードで見ます -
インタフェース
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.");
}
}
を実行すると
Mar 25, 2017 1:39:44 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.[email protected]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)
- _street:Cを実行したとき
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>
エラー -
Addressクラス
package com.nilaysundarkar.spring.demos;
public class Address {
private String street;
public Address(String street){
this.street = street;
}
public String toString(){
return street;
}
}
コンテキストのブートストラップc :_0
Mar 25, 2017 1:42:35 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.[email protected]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.[email protected]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>
'c:_street'の代わりに' c:street'を試しましたか? –
が機能しました。私はその本の誤字 - 春の行動を推測する。ありがとう! –
答えとして再表示してより目立たせる –