1

These days I'm learning Spring Framework. And I'm following some youtube videos. According to the video I wrote my first code in Spring as same as the tutorial. But at a point I'm getting XmlBeanFactory deprecated warning which are not shown in tutorial.

I'm having two classes on MyPackage package.

package MyPackage;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class DrawingApp {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    BeanFactory factory=new XmlBeanFactory(new FileSystemResource("Spring.xml"));
    Triangle triangle =(Triangle) factory.getBean("triangle");

    triangle.draw();
}

}

Here my XmlBeanFactory is deprecated. I checked everywhere couldn't fix it

This is my Triangle class.

package MyPackage;

public class Triangle {

public void draw(){
    System.out.println("Draw a Triangle");
}
}

And this is my xml file "spring.xml"

<?xml version="1.0" encoding="UTF-8"?>

<beans>
<bean id="triangle" class="MyPackage.Triangle"></bean>
</beans>

4 Answers4

2

As XmlBeanFact was deprecated you can use FileSystemXmlApplicationContext

public static void main(String[] args) {
    ApplicationContext factory = new FileSystemXmlApplicationContext("spring.xml");
    Triangle trianle=(Triangle)factory.getBean("triangle");
    trianle.draw();
}
CPM Chuck
  • 21
  • 2
0

As a XmlBeanFactory is deprecated you can use following code snippet.

public class SpringHelloWorldTest {
    public static void main(String[] args) {
           ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
           Triangle triangle =(Triangle) factory.getBean("triangle");
           triangle.draw();
}

}

Note : make sure to add "spring.xml" in build path.

0

As XmlBeanFactory was deprecated you can use ClassPathXmlApplicationContext

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        Triangle trianle=(Triangle)context.getBean("triangle");
        trianle.draw();

    }
glee8e
  • 6,180
  • 4
  • 31
  • 51
0

Since the XmlBeanFactory is deprecated, as an alternate you can use ClassPathXmlApplicationContext. ApplicationContext is a sub-interface of BeanFactory. Also, make sure that spring.xml is in the classpath (Under src folder).

    ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
    // ApplicationContext appContext = new FileSystemXmlApplicationContext("spring.xml");
    Triangle triangle = (Triangle) appContext.getBean("triangle");
    triangle.draw();

For more explanation, you can check https://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory