简体   繁体   English

使用GenericXmlApplicationContext时使用Spring SpEL?

[英]Spring SpEL when using a GenericXmlApplicationContext?

I'm having a problem getting SpEL to work when loading some Spring XML configuration explicitly into a GenericXmlApplicationContext object. 在将某些Spring XML配置显式加载到GenericXmlApplicationContext对象中时,让SpEL工作时遇到问题。

Any help you can give would be greatly appreciated. 您能提供的任何帮助将不胜感激。 Here's my POM file: 这是我的POM文件:

<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>

And here's Spring XML config file: 这是Spring 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"
    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>

And finally, my Java code 最后,我的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()));
    }
}

I was hoping to see 我希望看到

Greeter says: [Hello Dave]

but instead I see: 但是我看到了:

Greeter says: [Hello #{ getObject('specificPerson') != null ? getObject('specificPerson') : defaultPerson }]

Any idea why? 知道为什么吗? Your help is greatly appreciated - thanks! 非常感谢您的帮助-谢谢!

Turns out I needed to add calls to refresh() . 原来我需要添加对refresh()调用。

Adding a call to parentContext.refresh() and xmlContext.refresh() after object construction fixed the issue. 在对象构造完成后,添加对parentContext.refresh()xmlContext.refresh()的调用。 Hope someone else finds this to be helpful! 希望其他人发现这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM