0
いくつかのSpring XML設定を明示的にGenericXmlApplicationContextオブジェクトに読み込む際に、SpELが動作するのに問題があります。GenericXmlApplicationContextを使用しているときのSpring SpEL?
ご迷惑をおかけして申し訳ございません。私は
Greeter says: [Hello Dave]を見て期待していた
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="defaultPerson" class="java.lang.String">
<constructor-arg value="World!" />
</bean>
<bean id="greeter" class="com.example.Greeter">
<property name="person" value="#{ getObject('specificPerson') != null ? getObject('specificPerson') : defaultPerson }"/>
</bean>
</beans>
そして最後に、私のJavaコード
public class Greeter {
private String person;
public String sayHello() {
return String.format("Hello %s", this.person);
}
public void setPerson(String person) {
this.person = person;
}
}
public class Main {
public static void main(String[] args) {
GenericApplicationContext parentContext = new GenericApplicationContext();
parentContext.getBeanFactory().registerSingleton("specificPerson", "Dave");
GenericXmlApplicationContext xmlContext = new GenericXmlApplicationContext();
xmlContext.setParent(parentContext);
// Load the beans
xmlContext.load("xmlContext.xml");
// GenericXmlApplicationContext lazy loads singletons by default and we need them instantiated.
xmlContext.getBeanFactory().preInstantiateSingletons();
Greeter greeter = (Greeter) xmlContext.getBean("greeter");
System.out.println(String.format("Greeter says: [%s]", greeter.sayHello()));
}
}
:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>minimal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<version.spring>3.2.6.RELEASE</version.spring>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${version.spring}</version>
</dependency>
</dependencies>
</project>
そして、ここでは、春のXML設定ファイルです: はここに私のPOMファイルです
代わりに私は参照してください:
Greeter says: [Hello #{ getObject('specificPerson') != null ? getObject('specificPerson') : defaultPerson }]
どのようなアイデアですか?あなたの助けは大変ありがとうございます。