スプリングローディング方法
実行可能ファイル方法の場合、スプリング構成をロードする一般的な方法は
classpathxmlapplicationContext
public static void main(string [] args){classpathxmlapplicationContext xmlapplicationContext = new ClassPathxMLApplicationContext( "classpath:spring-context.xml"); Demoservice demoservice =(demoservice)xmlapplicationContext.getBean( "demoservice"); string 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://www.springframework.org/schema/context" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/spring/ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "default-autowire =" byname "defayour" <コンテキスト:annotation-config/> <! - スキャンされるパッケージを構成 - > <コンテキスト:component-scansaber-package = "com.jin.lesson.context"/> </beans>
スプリング3.0から始めて、アノテーションを使用してスプリング構成を登録します
public static void main(string [] args){annotationConfigApplicationContext AnnotationConfigApplicationContext = new AnnotationConfigApplicationContext(); //スキャンするパッケージを指示します。通常、アプリケーションクラスAnnotationConfigApplicationContext.scan(main.class.getPackage()。getName()); //コンテキストを更新し、対応するBeanを使用してAnnotationConfigApplicationContext.Refresh()を正常に登録します。 //対応するDEMOSERVICEをNAME DEMOSERVICE DEMOSERVICE =(DEMOSERVICE)ANNOTATIONCONFIGAPPLICATIONCONTEXT.GETBEAN( "Demoservice"); string text = demoservice.hello(); System.out.println(テキスト); }Demoserviceは、定義されたサービスの名前です。 XML構成メソッドは、1のような注釈を使用してスキャンするかどうかを設定することもできます
<コンテキスト:annotation-config/>
Demoserviceは、次のように非常にシンプルです
@service(value = "demoservice")public class demoservice {public string hello(){return "hello world"; }}Webアプリケーションの初期化
web.xml構成方法
最初の登録には、Spring独自のサーブレットを使用します
<Servlet> <Servlet-Name> SpringMVC </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <init-name> contextconfiglocation </param-name> <param-value> classpath:spring/spring-context.xtext.xml < <load-on-startup> 1 </load-on-startup> <async-supported> true </async-supported> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Spring+structsなどのリスナーを使用した登録は、この方法でコンテキストコンテンツを初期化することです
<リスナー> <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> </listener> <リスナー> <リスナークラス> org.springframework.web.context.request.request.requestcontextlistener
注釈の方法
また、サーブレットを使用して、初期化パラメーターを構成します。ただし、今回は、注釈ベースのクラスAnnotationConfigWebApplicationContextを使用する必要があり、サーブレットを登録する必要があります。
@Override public void onstartup(servletcontext servletcontext)throws servletexception {servletregistration.dynamic dispatcher = servletcontext.addservlet( "dispatcher"、dispatcherservlet.class); Dispatcher.setInitParameter( "ContextConfiglocation"、getClass()。getName()); Dispatcher.setInitParameter( "ContextClass"、AnnotationConfigWebApplicationContext.class.getName()); Dispatcher.AddMapping( "/*"); Dispatcher.setloadOnStartup(1); }上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。