스프링 로딩 방법
실행 파일 방법의 경우 스프링 구성을로드하는 일반적인 방법은 다음과 같습니다.
classPathXmlApplicationContext
public static void main (String [] args) {classPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext ( "ClassPath : Spring-Context.xml"); Demoservice demoservice = (Demoservice) xmlapplicationcontext.getbean ( "demoservice"); 문자열 text = demoservice.hello (); System.out.println (텍스트); }<beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns : context = "http://wwww.spramframett" xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context-3.2.xsd "default-autowire ="byname "default-lazy-init ="false "> <!-주석에서 Bean을 구성합니다-> <context : pannotation-config/>>-conte-canting-> conte-scan을 구성하십시오. Base-Package = "com.jin.lesson.context"/> </beans>
봄 3.0부터 시작하여 주석을 사용하여 스프링 구성을 등록하십시오.
public static void main (String [] args) {AnnotationConfigApplicationContext ConnotationConfigApplicationContext = new AnnotationConfigApplicationContext (); // 패키지를 스캔하도록 지시하십시오. 일반적으로 응용 프로그램 클래스 annotationConfigApplicationContext.scan (main.class.getPackage (). getName ()); // 컨텍스트를 새로 고침하고 해당 Bean을 사용하여 성공적으로 gontationconfigApplicationContext.Refresh (); // 이름으로 해당 데모 서비스를 가져옵니다. Demoservice Demoservice = (Demoservice) AnnotationConfigApplicationContext.getBean ( "Demoservice"); 문자열 text = demoservice.hello (); System.out.println (텍스트); }Demoservice는 정의 된 서비스의 이름입니다. XML 구성 방법은 1에서와 같이 주석을 사용하여 스캔할지 여부를 설정할 수도 있습니다.
<컨텍스트 : 주석 -Config/>
Demoservice는 다음과 같이 매우 간단합니다
@Service (value = "demoservice") public class demoservice {public string hello () {return "Hello World"; }}웹 응용 프로그램의 초기화
web.xml 구성 메소드
초기 등록에 Spring의 자체 서블릿을 사용하십시오
<Servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.spramework.web.servlet.dispatcherServlet </servlet-class> <init-param> <apar-name> contextConfiglocation </param-name> <param-value> classpath : spring/initc-conte-xml </load-on-startup> 1 </load-on-startup> <sync-supported> true </async-supported> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Spring+structs와 같은 청취자를 사용한 등록은 이러한 방식으로 컨텍스트 내용을 초기화하는 것입니다.
<Leater> <Leater-Class> org.springframework.web.context.contextloaderListener </Learger-Class> </Listener> <Leater> <Leater-class> org.spramframework.web.context.request.requestContextListener </Lister-Class> </lurtener>
주석을 달 수있는 방법
또한 서블릿을 사용하여 초기화 매개 변수를 구성합니다. 그러나 이번에는 주석 기반 Class AnnotationConfigBebApplicationContext를 사용해야하며 서블릿을 등록해야합니다.
@override public void onstartup (servletcontext servletcontext) servletexception {servletregistration.dynamic dispatcher = servletcontext.addservlet ( "dispatcher", dispatcherervlet.class); Dispatcher.SetInitParameter ( "ContextConfigLocation", getClass (). getName ()); Dispatcher.SetInitParameter ( "ContextClass", AnnotationConfigWebApplicationContext.class.getName ()); Dispatcher.AddMapping ( "/*"); Dispatcher.SetLoadOnStartup (1); }위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.