Spring XmlBeanFactory已弃用

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

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

XmlBeanFactorytypes已被弃用

我必须使用什么来替代这个?

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

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

这里是替代代码,

 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(); } 
 BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry); reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE")); 

您可以使用ClassPathXmlApplicationContext类。

获取bean上下文的新方法(不需要类转换):

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

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

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

这是实施的最佳方式

 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; 

这个怎么样:

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

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"); 

我试了下面的代码

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

它的工作