Spring Multi-Configurationファイルの利点は何ですか?
構成ファイルを目的と関数に応じて分割すると、構成トランザクション管理やデータソース、構成豆などの変更が少ない個別の構成など、構成ファイルの読みやメンテナンスが向上します。
いくつかの方法Springは構成ファイルを読み取ります:
1.Spring自体によって提供されるApplicationContextメソッドを使用して読む
Javaプログラムでは、ApplicationContextの2つの実装クラスClassPathxMLApplicationContextとFilesystemXMLApplicationContextを使用して複数の構成ファイルを読み取ることができ、コンストラクターは一連の構成ファイルを受信できます。
例:ApplicationContext ctx = new ClassPathxMLApplicationContext(configlocations); FilesystemxmlapplicationContextを使用してApplicationContextを作成する方法に似ています。唯一の違いは、2つの検索構成ファイルのパスが異なることです。ClassPathxMLApplicationContextClassPathパスを介して構成ファイルを検索します。
方法1:初期化中にApplicationContextオブジェクトを保存します
コード:
ApplicationContext AC = new FilesystemXMLApplicationContext( "ApplicationContext.xml"); ac.getbean( "beanid");
説明:
この方法は、Spring Frameworkを使用したスタンドアロンアプリケーションに適しています。この方法では、プログラムが構成ファイルを介してスプリングを手動で初期化する必要があります。
方法2:Springが提供するツールクラスを介してApplicationContextオブジェクトを取得します
コード:
Import org.springframework.web.context.support.webapplicationContextutils; ApplicationContext AC1 = webApplicationContextutils.getRequiredWebApplicationContext(ServletContext SC)ApplicationContext AC2 = webApplicationContextutils.getWebApplicationContext(ServletContext(ServletContext SC)AC1.GetBean( "Beanid"); ac2.getbean( "beanid");
説明:
この方法は、Spring Frameworkを使用してB/Sシステムに適しており、ServletContextオブジェクトを介してApplicationContextオブジェクトを取得し、必要なクラスインスタンスを取得します。
上記の2つのツールメソッドの違いは、取得が失敗したときに前者が例外をスローし、後者がnullを返すことです。
方法3:抽象クラスApplicationObjectSupportから継承
説明:
Abstract class ApplicationObjectSupportは、getApplicationContext()メソッドを提供します。これにより、ApplicationContextを簡単に取得できます。 Springが初期化されると、ApplicationContextオブジェクトは、抽象クラスのSetApplicationContext(ApplicationContextコンテキスト)メソッドを介して注入されます。
方法4:抽象クラスWebApplicationObjectSupportから継承
説明:
上記の方法と同様に、getWebApplicationContext()を呼び出してwebApplicationContextを取得します
方法5:インターフェイスApplicationContextAwareを実装します
説明:
このインターフェイスのSetApplicationContext(ApplicationContext Context)メソッドを実装し、ApplicationContextオブジェクトを保存します。 Springが初期化されると、ApplicationContextオブジェクトがこのメソッドを介して注入されます。
上記の方法はさまざまな状況に適しています。特定の状況に応じて、対応する方法を選択してください。
2。Webプロジェクトの起動を使用するときの読み込み
Web.xmlで自動的にロードされる構成ファイル:
<context-param> <param-name> contextconfiglocation </param-name> <param-value> /web-inf/spring/spring-core.xml </param-value> </context-param> <Servlet> <Servlet-Name> SpringMVC </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-name> contextconfiglocation </param-name> <param-value>/<param-value> inf/spring/spring-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-name> springmvc </servlet-name> <url-pattern>/</</url-pattern> </servet-mapping>
複数の数値がある場合、 *を使用してそれらを交換できます。
<Servlet> <Servlet-Name> app </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <context-param> <param-name> contextconfiglocation </param-name> <param-value> /web-inf/applicationcontext*.xml,/web-inf/user_spring.xml </param-value> </context-param> <load-on-startup> 1 </load-on-startup> </servet>
3.他の構成ファイルをXML構成ファイルにインポートする
/web-inf/applicationcontext.xmlにロードするためにアプリケーションサービスを構成します。インポートを使用して、ApplicationContext.xmlで他の構成ファイルをインポートできます。
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:p = "http://www.springframework.org/schema/p" xmlns:mvc = "http://www.springframework.org/schema/mvc" " xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-/spring-context-/spring-contex http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/spring-pring-f.f.f.f.f.f.f.f.f.f.f.f.f.f.f. Resource = "Spring-Servlet.xml"/> <リソースをインポート= "Spring-security.xml"/> <リソースをインポート= "spring-hibernate.xml"/> <Import resource = "spring-redis.xml"/> <import resource = "spring-redis.xml"/> <beans>