Springs XmlBean Factory는 더 이상 사용되지 않습니다.
저는 봄을 배우려고 노력합니다.저는 이 사이트 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();
}
}
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"));
(클래스 캐스팅 없이) 콩 컨텍스트를 얻는 새로운 방법:
BeanDefinitionRegistry beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));
애플리케이션 전체 컨텍스트를 시작할 때 사용해야 합니다.
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
ClassPathXmlApplicationContext 클래스를 사용할 수 있습니다.
이거 어때:
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("config/Beans.xml"));
Messager msg = (Messager) factory.getBean("Messager");
가장 좋은 구현 방법은 다음과 같습니다.
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;
Spring 문서에서 확인할 수 있는 XML Bean Factory의 대안
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");
"FileSystemXmlApplicationContext"를 다음과 같이 사용합니다.
ApplicationContext context = new FileSystemXmlApplicationContext("SpringHelloWorld.xml");
Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
myBean.sayHello();
승인된 답변과 함께 "Resource leak: 'context'가 닫히지 않음" 경고가 표시됩니다.
SO Post Spring Application Context - Resource Leak: 'context is never closed'에서 제안된 해결책은 이 문제를 해결합니다.
누군가에게 도움이 되길 바랍니다.
객체의 런타임 초기화와 관련하여 ApplicationContext와 XmlBeanFactory 간에 큰 차이가 있다는 것을 알게 되었습니다.
응용프로그램 컨텍스트는 개체 생성자를 즉시 호출하여 개체를 한 번에 만듭니다.
XmlBeanFactory는 beanFactory.getBean(문자열 이름)이 호출된 경우에만 개체를 만듭니다.
이것이 문제 도메인에 중요한 경우 다음 코드를 XmlBeanFactory를 피하는 오리진 소스에 해당하는 코드로 시도하십시오.
final Resource resource = new ClassPathResource("SpringHelloWorld.xml");
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
final XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
xmlBeanDefinitionReader.loadBeanDefinitions(resource);
Spring3HelloWorld myBean = beanFactory.getBean("Spring3HelloWorldBean", Spring3HelloWorld.class);
myBean.sayHello();
명백한 선장이 완전한 예를 만족시킬 수 있도록 하기 위해 다음과 같이 비교합니다.
먼저 Person.java 모델이 있습니다.
package com.stackoverflow.questions_5231371;
// ./spring-tutorial/src/main/java/com/stackoverflow/questions_5231371/Person.java
public class Person {
int id;
String name;
public Person() {
System.out.println("calling Employee constructor");
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
그런 다음 personbeans.xml의 개체 데이터를 입력합니다.
<?xml version="1.0" encoding="UTF-8"?>
<!-- ./spring-tutorial/src/main/java/personbeans.xmlxml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="p1" class="com.stackoverflow.questions_5231371.Person">
<property name="id" value="1" />
<property name="name" value="Sheldon" />
</bean>
<bean id="p2" class="com.stackoverflow.questions_5231371.Person">
<property name="id" value="2" />
<property name="name" value="Penny" />
</bean>
<bean id="p3" class="com.stackoverflow.questions_5231371.Person">
<property name="id" value="3" />
<property name="name" value="Leonard" />
</bean>
</beans>
그리고 사물을 부르는 주요한 방법.
package com.stackoverflow.questions_5231371;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
// ./spring-tutorial/src/main/java/com/stackoverflow/questions_5231371/Main.java
public class Main {
public static void main(final String[] args) {
// Spring #1 IoC application context
System.out.println("--- ApplicationContext");
final ApplicationContext context = new ClassPathXmlApplicationContext("personbeans.xml");
System.out.println("context created ---");
System.out.println(context.getBean("p1"));
System.out.println(context.getBean("p2"));
System.out.println(context.getBean("p3"));
// Spring #2 IoC bean factory
System.out.println("--- DefaultListableBeanFactory");
final Resource resource = new ClassPathResource("personbeans.xml");
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
final XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
xmlBeanDefinitionReader.loadBeanDefinitions(resource);
System.out.println("bean definition loaded ---");
System.out.println(beanFactory.getBean("p1"));
System.out.println(beanFactory.getBean("p2"));
System.out.println(beanFactory.getBean("p3"));
}
}
인터스팅 부분은 생성자가 콘솔 출력에서 "직원 생성자 호출" 출력으로 로드될 때의 비교입니다.
--- ApplicationContext
calling Employee constructor
calling Employee constructor
calling Employee constructor
context created ---
Person [id=1, name=Sheldon]
Person [id=2, name=Penny]
Person [id=3, name=Leonard]
--- DefaultListableBeanFactory
bean definition loaded ---
calling Employee constructor
Person [id=1, name=Sheldon]
calling Employee constructor
Person [id=2, name=Penny]
calling Employee constructor
Person [id=3, name=Leonard]
나는 다음 코드를 시도했습니다.
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();
}
}
그리고 그것은 동작한다.
언급URL : https://stackoverflow.com/questions/5231371/springs-xmlbeanfactory-is-deprecated
'programing' 카테고리의 다른 글
C 프로그래밍의 고정점 연산 (0) | 2023.08.22 |
---|---|
PDO로 연결 시간 제한 설정 (0) | 2023.08.22 |
MySQL Current_TIMESTAMP가 기본값으로 표시됨 (0) | 2023.08.22 |
PHP 사전 설정은 허용되는 숫자만 대체합니다. (0) | 2023.08.22 |
오라클에서 행이 있는지 확인하는 가장 빠른 쿼리? (0) | 2023.08.22 |