복붙노트

[SPRING] 스프링 XmlBeanFactory가 사용되지 않습니다.

SPRING

스프링 XmlBeanFactory가 사용되지 않습니다.

나는 봄을 배우려고 노력한다. 나는이 사이트를 따라 가고있다. http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml

나는 그 한 가지 예를 시도했다. 나는 아래에 어떤 것을 사용하고 있지만, 여기에 그것을 보여준다 :

이것에 대한 대안으로 무엇을 사용해야합니까?

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

해결법

  1. ==============================

    1.

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

    2.다음은 대체 코드입니다.

    다음은 대체 코드입니다.

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

    3.

    BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
    reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));
    
  4. ==============================

    4.ClassPathXmlApplicationContext 클래스를 사용할 수 있습니다.

    ClassPathXmlApplicationContext 클래스를 사용할 수 있습니다.

  5. ==============================

    5.bean 컨텍스트를 가져 오는 새로운 방법 (클래스 캐스팅없이) :

    bean 컨텍스트를 가져 오는 새로운 방법 (클래스 캐스팅없이) :

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

    응용 프로그램 전체 컨텍스트를 시작할 때 사용해야합니다.

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

    6.여기에 구현하는 가장 좋은 방법은

    여기에 구현하는 가장 좋은 방법은

    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;
    
  7. ==============================

    7.이것은 어떤가요:

    이것은 어떤가요:

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

    8.Spring 문서에있는 XMLBeanFactory의 대안

    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");
    
  9. ==============================

    9."FileSystemXmlApplicationContext"를 다음과 같이 사용하십시오.

    "FileSystemXmlApplicationContext"를 다음과 같이 사용하십시오.

    ApplicationContext  context =  new FileSystemXmlApplicationContext("SpringHelloWorld.xml");
    
    Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
    
    myBean.sayHello();
    
  10. ==============================

    10.나는 다음 코드를 시도했다.

    나는 다음 코드를 시도했다.

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

    작동하고

  11. from https://stackoverflow.com/questions/5231371/springs-xmlbeanfactory-is-deprecated by cc-by-sa and MIT license