Ich versuche den Frühling zu lernen. Ich verfolge diese Seite http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml
Ich habe ein Beispiel ausprobiert. Ich benutze etwas wie hier unten, aber hier zeigt es:
Der Typ XmlBeanFactory ist veraltet
Was muss ich als Alternative dazu verwenden?
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 ist eine Unteroberfläche von BeanFactory. Sie können diesen Weg verwenden
public class SpringHelloWorldTest {
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("SpringHelloWorld.xml");
Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
myBean.sayHello();
}
}
Hier ist der Ersatzcode,
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"));
Sie können die Klasse ClassPathXmlApplicationContext verwenden.
Neue Möglichkeit, den Beans-Kontext zu erhalten (ohne Klassengießen):
BeanDefinitionRegistry beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));
Beim Starten eines anwendungsweiten Kontexts sollte verwendet werden
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
wie wäre es damit:
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("config/Beans.xml"));
Messager msg = (Messager) factory.getBean("Messager");
Alternative zu XMLBeanFactory in der Spring-Dokumentation
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");
Hier ist der beste Weg zur Umsetzung
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;
Es gibt eine Warnung " Ressourcenleck: 'Kontext' wird nie geschlossen " mit der akzeptierten Antwort.
Die in SO post Spring ApplicationContext - Ressourcenleck: 'context' wird nie geschlossen vorgeschlagene Lösung behebt das Problem.
Hoffe es hilft jemandem.
Verwenden Sie "FileSystemXmlApplicationContext" als
ApplicationContext context = new FileSystemXmlApplicationContext("SpringHelloWorld.xml");
Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
myBean.sayHello();