56

私は春を学ぼうとします。私はこのサイトをフォローしています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();
    }
}
4

12 に答える 12

52

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();
    }
}
于 2012-02-09T12:35:36.090 に答える
15

これが代替コードです。

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();
}
于 2011-03-08T20:44:19.610 に答える
10
BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));
于 2012-04-17T08:34:41.390 に答える
6

ClassPathXmlApplicationContextクラスを使用できます。

于 2011-03-08T10:54:41.527 に答える
6

Bean コンテキストを取得する新しい方法 (クラス キャストなし):

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

アプリ全体のコンテキストを開始するときは、使用する必要があります

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
于 2013-04-13T06:48:42.453 に答える
1

これが実装する最良の方法です

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;
于 2015-02-12T10:27:50.707 に答える
-1

次のコードを試しました

    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();
    }
}

そしてそれは動作します

于 2011-11-10T10:48:41.003 に答える