봄에 콩을 얻는 방법에는 여러 가지가 있으며 다시 요약하겠습니다.
첫 번째 유형 : 초기화 중에 ApplicationContext 객체를 저장합니다
ApplicationContext ac = 새 FileSystemXmlApplicationContext ( "ApplicationContext.xml"); ac.getBean ( "Beanid");
참고 :이 방법은 Spring Framework를 사용하는 독립형 응용 프로그램에 적합하며 프로그램은 구성 파일을 통해 스프링을 수동으로 초기화해야합니다.
두 번째 유형 : Spring이 제공 한 도구 클래스를 통해 ApplicationContext 개체 가져 오기
import org.springframework.web.context.support.webapplicationContextUtils; ApplicationContext AC1 = webApplicationContextUtils.getRequiredWebApplicationContext (ServletContext Sc); ApplicationContext AC2 = WebApplicationContextUtils.getWebapplicationContext sc); ac1.getbean ( "beanid"); ac2.getbean ( "beanid");
설명 :
1.이 두 가지 방법은 스프링 프레임 워크를 사용하는 B/S 시스템에 적합하며 ServletContext 객체를 통해 ApplicationContext 객체를 얻은 다음 필요한 클래스 인스턴스를 얻습니다.
2. 첫 번째 메소드는 획득이 실패하면 예외가 발생하고 두 번째 메소드는 NULL을 반환합니다.
세 번째 유형 : 초록 클래스 ApplicationObjectSupport에서 상속
참고 : Abstract Class ApplicationObjectSupport에서 제공하는 getApplicationContext () 메소드는 ApplicationContext 인스턴스를 쉽게 얻은 다음 스프링 컨테이너에서 Bean을 얻을 수 있습니다. Spring이 초기화되면 ApplicationContext 객체는 초록 클래스의 setApplicationContext (ApplicationContext Context) 메소드를 통해 주입됩니다.
네 번째 유형 : 추상 클래스 WebApplicationObjectSupport에서 상속
참고 : 위의 메소드와 유사하게 getWebApplicationContext ()를 호출하여 WebApplicationContext 인스턴스를 가져옵니다.
다섯 번째 유형 : 인터페이스 ApplicationContextAware 구현
설명 :이 인터페이스의 setApplicationContext (ApplicationContext Context) 메소드를 구현하고 ApplicationContext 객체를 저장하십시오. 스프링이 초기화되면이 방법을 통해 ApplicationContext 객체가 주입됩니다.
Spring은 Spring의 ApplicationContext 객체를 얻기 위해 보통 클래스에서 해당 클래스 또는 인터페이스의 상속 또는 구현을 구현하는 마지막 세 가지 방법을 제공하지만, 사용할 때는 이러한 추상 클래스 또는 인터페이스를 상속하거나 구현하는 데주의를 기울여야합니다. 또는 획득 된 ApplicationContext 객체는 null이됩니다.
다음은 인터페이스 ApplicationContextAware를 구현하여 스프링 컨테이너에서 Bean을 얻는 방법을 보여줍니다.
먼저 ApplicationContextAware 인터페이스를 구현하는 클래스를 사용자 정의하고 내부에서 메소드를 구현하십시오.
package com.ghj.tool;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class SpringConfigTool implements ApplicationContextAware {// extends ApplicationObjectSupport{ private static ApplicationContext ac = null; 개인 정적 SpringConfigTool SpringConfigTool = NULL; public synchronized static springconfigtool init () {if (springconfigtool == null) {springConfigTool = new SpringConfigTool (); } 반환 SpringConfigTool; } public void setApplicationContext (ApplicationContext ApplicationContext)는 beansexception {ac = ApplicationContext; } public synchronized static 객체 getBean (String beanname) {return ac.getBean (beanname); }} 둘째, ApplicationContext.xml 파일에서 구성하십시오.
다음과 같이 코드를 복사하십시오. <bean id = "SpringConfigTool"/>
마지막으로, 다음 코드를 통해 스프링 컨테이너에 해당 콩을 가져올 수 있습니다.
코드 사본은 다음과 같습니다. SpringConfigTool.getBean ( "Beanid");
서버가 스프링 컨테이너의 초기화를 시작하면 다음 방법을 통해 스프링 컨테이너를 얻을 수 없습니다.
import org.springframework.web.context.contextloader; import org.springframework.web.context.webapplicationContext; WebApplicationContext WAC = ContextLoader.GetCurrentWebApplicationContext (); wac.getbean (beanid);
위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.