Springs XmlBeanFactory 已弃用

发布于 2024-10-20 14:14:35 字数 646 浏览 9 评论 0原文

我尝试学习Spring。我正在关注这个网站 http://www.roseindia.net/spring/spring3/spring -3-hello-world.shtml

我尝试了其中的一个例子。我正在使用如下所示的一些内容,但这里显示:

XmlBeanFactory 类型已弃用

我必须使用什么来替代它?

public class SpringHelloWorldTest {
    public static void main(String[] args) {

        XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));

        Spring3HelloWorld myBean = (Spring3HelloWorld)beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

I try to learn Spring. I am following this site http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml

I tried one example in that. I am using some what like below, but here it shows:

The type XmlBeanFactory is deprecated

What do I have to use as an alternative to this?

public class SpringHelloWorldTest {
    public static void main(String[] args) {

        XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));

        Spring3HelloWorld myBean = (Spring3HelloWorld)beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

拥醉 2024-10-27 14:14:35

ApplicationContext是BeanFactory的子接口,可以这样使用

public class SpringHelloWorldTest {
    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("SpringHelloWorld.xml");
        Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

ApplicationContext is a sub-interface of BeanFactory.You can use this way

public class SpringHelloWorldTest {
    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("SpringHelloWorld.xml");
        Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}
半边脸i 2024-10-27 14:14:35

这是替代代码,

public static void main(String[] args){
    ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"SpringHelloWorld.xml"});
    BeanFactory factory=context;
    Spring3HelloWorld myBean=(Spring3HelloWorld)factory.getBean("Spring3HelloWorldBean");
    myBean.sayHello();
}

Here is the substitute code,

public static void main(String[] args){
    ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"SpringHelloWorld.xml"});
    BeanFactory factory=context;
    Spring3HelloWorld myBean=(Spring3HelloWorld)factory.getBean("Spring3HelloWorldBean");
    myBean.sayHello();
}
要走就滚别墨迹 2024-10-27 14:14:35
BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));
BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));
梦纸 2024-10-27 14:14:35

获取 beans 上下文的新方法(无需类转换):

BeanDefinitionRegistry beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));

当启动应用程序范围的上下文时,应该使用

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

New way to get beans context (without class casting):

BeanDefinitionRegistry beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));

When starting an apppication-wide context one should use

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
漫漫岁月 2024-10-27 14:14:35

这个怎么样:

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("config/Beans.xml"));
Messager msg = (Messager) factory.getBean("Messager");

how about this:

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("config/Beans.xml"));
Messager msg = (Messager) factory.getBean("Messager");
两相知 2024-10-27 14:14:35

这是最好的实施方式

Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
       new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;

Here is the best way to implement

Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
       new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;
错爱 2024-10-27 14:14:35

Spring 文档中可找到 XMLBeanFactory 的替代方案

GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new 
ClassPathResource("applicationContext.xml"));
PropertiesBeanDefinitionReader propReader = new 
PropertiesBeanDefinitionReader(ctx);
propReader.loadBeanDefinitions(new 
ClassPathResource("otherBeans.properties"));
ctx.refresh();

MyBean myBean = (MyBean) ctx.getBean("myBean");

Alternative to XMLBeanFactory found on Spring documentation

GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new 
ClassPathResource("applicationContext.xml"));
PropertiesBeanDefinitionReader propReader = new 
PropertiesBeanDefinitionReader(ctx);
propReader.loadBeanDefinitions(new 
ClassPathResource("otherBeans.properties"));
ctx.refresh();

MyBean myBean = (MyBean) ctx.getBean("myBean");
红ご颜醉 2024-10-27 14:14:35

使用“FileSystemXmlApplicationContext”作为

ApplicationContext  context =  new FileSystemXmlApplicationContext("SpringHelloWorld.xml");

Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");

myBean.sayHello();

Use "FileSystemXmlApplicationContext" as

ApplicationContext  context =  new FileSystemXmlApplicationContext("SpringHelloWorld.xml");

Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");

myBean.sayHello();
夜夜流光相皎洁 2024-10-27 14:14:35

接受的答案中有一条警告“资源泄漏:'上下文'永远不会关闭”。

SO 帖子 Spring ApplicationContext - Resource Leak: 'context' is never close 中建议的解决方案修复了该问题。

希望它能帮助某人。

There is a warning "Resource leak: 'context' is never closed" with the accepted answer.

The solution suggested in the SO post Spring ApplicationContext - Resource leak: 'context' is never closed fixes the issue.

Hope it helps someone.

痴骨ら 2024-10-27 14:14:35

我认识到 ApplicationContext 和 XmlBeanFactory 在对象的运行时初始化方面存在巨大差异。

应用程序上下文立即调用对象构造函数,从而立即创建对象。

仅当调用 beanFactory.getBean(String name) 时,XmlBeanFactory 才会创建对象。

如果这对于问题域很重要,请尝试使用以下代码作为避免使用 XmlBeanFactory 的原始源的等效代码。

final Resource resource = new ClassPathResource("SpringHelloWorld.xml");
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
final XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
xmlBeanDefinitionReader.loadBeanDefinitions(resource);
Spring3HelloWorld myBean = beanFactory.getBean("Spring3HelloWorldBean", Spring3HelloWorld.class);
myBean.sayHello();

为了让 Captain Obvious 满意,下面是一个完整的比较示例。

首先是模型 Person.java。

package com.stackoverflow.questions_5231371;

// ./spring-tutorial/src/main/java/com/stackoverflow/questions_5231371/Person.java
public class Person {

    int id;
    String name;

    public Person() {
        System.out.println("calling Employee constructor");
    }

    public int getId() {
        return id;
    }

    public void setId(final int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
}

然后是personbeans.xml中的对象数据。

<?xml version="1.0" encoding="UTF-8"?>
<!-- ./spring-tutorial/src/main/java/personbeans.xmlxml  -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="p1" class="com.stackoverflow.questions_5231371.Person">
        <property name="id" value="1" />
        <property name="name" value="Sheldon" />    
    </bean>

    <bean id="p2" class="com.stackoverflow.questions_5231371.Person">
        <property name="id" value="2" />
        <property name="name" value="Penny" />  
    </bean>

    <bean id="p3" class="com.stackoverflow.questions_5231371.Person">
        <property name="id" value="3" />
        <property name="name" value="Leonard" />    
    </bean>

</beans>

以及调用对象的主要方法。

package com.stackoverflow.questions_5231371;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

// ./spring-tutorial/src/main/java/com/stackoverflow/questions_5231371/Main.java
public class Main {

    public static void main(final String[] args) {
        // Spring #1 IoC application context
        System.out.println("--- ApplicationContext");
        final ApplicationContext context = new ClassPathXmlApplicationContext("personbeans.xml");
        System.out.println("context created ---");
        System.out.println(context.getBean("p1"));
        System.out.println(context.getBean("p2"));
        System.out.println(context.getBean("p3"));

        // Spring #2 IoC bean factory
        System.out.println("--- DefaultListableBeanFactory");
        final Resource resource = new ClassPathResource("personbeans.xml");
        final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        final XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        xmlBeanDefinitionReader.loadBeanDefinitions(resource);
        System.out.println("bean definition loaded ---");
        System.out.println(beanFactory.getBean("p1"));
        System.out.println(beanFactory.getBean("p2"));
        System.out.println(beanFactory.getBean("p3"));
    }

}

最有趣的部分是加载构造函数时与控制台输出中的输出“calling Employee constructor”的比较。

--- ApplicationContext
calling Employee constructor
calling Employee constructor
calling Employee constructor
context created ---
Person [id=1, name=Sheldon]
Person [id=2, name=Penny]
Person [id=3, name=Leonard]
--- DefaultListableBeanFactory
bean definition loaded ---
calling Employee constructor
Person [id=1, name=Sheldon]
calling Employee constructor
Person [id=2, name=Penny]
calling Employee constructor
Person [id=3, name=Leonard]

I recognized a huge difference between the ApplicationContext and the XmlBeanFactory regarding to the runtime initialization of the objects.

The applicationcontext calls the object constructors immediatly thus creating the objects at once.

The XmlBeanFactory creates an object only if the beanFactory.getBean(String name) is called.

If this is important for the problem domain give the following code a try as an equivalent to the origin source avoiding XmlBeanFactory.

final Resource resource = new ClassPathResource("SpringHelloWorld.xml");
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
final XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
xmlBeanDefinitionReader.loadBeanDefinitions(resource);
Spring3HelloWorld myBean = beanFactory.getBean("Spring3HelloWorldBean", Spring3HelloWorld.class);
myBean.sayHello();

To make Captain Obvious satisfied a full example with comparasion follows.

First there is the model Person.java.

package com.stackoverflow.questions_5231371;

// ./spring-tutorial/src/main/java/com/stackoverflow/questions_5231371/Person.java
public class Person {

    int id;
    String name;

    public Person() {
        System.out.println("calling Employee constructor");
    }

    public int getId() {
        return id;
    }

    public void setId(final int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
}

Then the object data in the personbeans.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!-- ./spring-tutorial/src/main/java/personbeans.xmlxml  -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="p1" class="com.stackoverflow.questions_5231371.Person">
        <property name="id" value="1" />
        <property name="name" value="Sheldon" />    
    </bean>

    <bean id="p2" class="com.stackoverflow.questions_5231371.Person">
        <property name="id" value="2" />
        <property name="name" value="Penny" />  
    </bean>

    <bean id="p3" class="com.stackoverflow.questions_5231371.Person">
        <property name="id" value="3" />
        <property name="name" value="Leonard" />    
    </bean>

</beans>

And the main method who calls the objects.

package com.stackoverflow.questions_5231371;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

// ./spring-tutorial/src/main/java/com/stackoverflow/questions_5231371/Main.java
public class Main {

    public static void main(final String[] args) {
        // Spring #1 IoC application context
        System.out.println("--- ApplicationContext");
        final ApplicationContext context = new ClassPathXmlApplicationContext("personbeans.xml");
        System.out.println("context created ---");
        System.out.println(context.getBean("p1"));
        System.out.println(context.getBean("p2"));
        System.out.println(context.getBean("p3"));

        // Spring #2 IoC bean factory
        System.out.println("--- DefaultListableBeanFactory");
        final Resource resource = new ClassPathResource("personbeans.xml");
        final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        final XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        xmlBeanDefinitionReader.loadBeanDefinitions(resource);
        System.out.println("bean definition loaded ---");
        System.out.println(beanFactory.getBean("p1"));
        System.out.println(beanFactory.getBean("p2"));
        System.out.println(beanFactory.getBean("p3"));
    }

}

The interssting part is the comparasion when the constructor is loaded with the output "calling Employee constructor" in the console output.

--- ApplicationContext
calling Employee constructor
calling Employee constructor
calling Employee constructor
context created ---
Person [id=1, name=Sheldon]
Person [id=2, name=Penny]
Person [id=3, name=Leonard]
--- DefaultListableBeanFactory
bean definition loaded ---
calling Employee constructor
Person [id=1, name=Sheldon]
calling Employee constructor
Person [id=2, name=Penny]
calling Employee constructor
Person [id=3, name=Leonard]
游魂 2024-10-27 14:14:35

我尝试了以下代码

    public class Spring3HelloWorldTest {
    public static void main(String[] args) {        
        DefaultListableBeanFactory  beanFactory = new DefaultListableBeanFactory ((BeanFactory) new ClassPathResource("SpringHelloWorld.xml"));     
        Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

并且它有效

I tried the following code

    public class Spring3HelloWorldTest {
    public static void main(String[] args) {        
        DefaultListableBeanFactory  beanFactory = new DefaultListableBeanFactory ((BeanFactory) new ClassPathResource("SpringHelloWorld.xml"));     
        Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

and it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文