Vis*_*h.M 50 java spring xmlbeans deprecated
我试着学春天.我关注此网站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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 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();
}
Run Code Online (Sandbox Code Playgroud)
BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));
Run Code Online (Sandbox Code Playgroud)
获取bean上下文的新方法(没有类转换):
BeanDefinitionRegistry beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));
Run Code Online (Sandbox Code Playgroud)
当开始一个应用程序范围的上下文时,应该使用
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54727 次 |
| 最近记录: |