春ラーニング(XVI) - エンジニアリング初期設定に春のアプローチ

第一种:实现ApplicationListener <ContextRefreshedEvent> 接口

パッケージBDC。ベース; 

輸入java.util.HashMapを; 
輸入はjava.util.List; 
輸入java.util.Map; 

輸入javax.annotation.Resource。

輸入org.springframework.context.ApplicationListener。
org.springframework.contextインポートします。イベント.ContextRefreshedEvent; 

輸入bdc.ws.dao.CommonDao; 
輸入bdc.ws.entity.RsaKey。
// @Component使用这个注解或者采用豆注入<ビーンID = "afterSpringBegin"クラス= "bdc.base.AfterSpringBegin"> </ビーン> 
パブリック クラスAfterSpringBeginが実装
    ApplicationListenerを <ContextRefreshedEvent> {

  @Resource 
  プライベートCommonDao commonDaoを。
  公共の 静的な最終地図<文字列、RsaKey> KEY_RSA = 新しい HashMapの<文字列、RsaKey> (); 

  @Override 
  公共 ボイド onApplicationEvent(ContextRefreshedEventのイベント){ 

    場合イベント。.getApplicationContext()のgetParent()== nullの){
       しようと{ 
        一覧 <RsaKey> selectAllRsaKey = commonDao.selectAllRsaKey();
        以下のためにint型 i = 0 ; iがselectAllRsaKey.size()<; I ++は){
          KEY_RSA.put(selectAllRsaKey。取得(I).getUserOrg()、selectAllRsaKey。取得(I)); 
        } 
      } キャッチ(例外e){ 
        e.printStackTrace(); 
      } 
    } 
  } 

} 



第二种使用@PostConstruct注解
@Component 
パブリック クラスTuneNetInterface {
   プライベート 静的クライアントのクライアント。
  プライベート 静的なQNameにOPNAME。
  @value(" $ {WebUrl} " プライベート文字列WebUrl。
  @value(" $ {操作} " 文字列操作。プライベート

  @PostConstruct 
  公共 ボイドのinit(){
  // 以下为具体逻辑 
    JaxWsDynamicClientFactory工場= JaxWsDynamicClientFactory 
        .newInstance(BusFactory.newInstance()createBus()); 
    クライアント = factory.createClient(WebUrl)。
    エンドポイントエンドポイント = client.getEndpoint()。
    OPNAME = 新規のQName(endpoint.getService()のgetName()のgetNamespaceURI()、。。
        操作)
    BindingInfo bindingInfo = endpoint.getEndpointInfo()getBinding()。
    もし(bindingInfo.getOperation(OPNAME)== NULL ){
      (BindingOperationInfo operationInfo:bindingInfo.getOperations()){
         場合(operation.equals(operationInfo.getName()getLocalPart())){ 
          OPNAME = operationInfo.getName()。
          破ります; 
        } 
      } 
    } 

  } 

  パブリック 静的オブジェクト[]要求(ストリング含有量)は例外{スロー
     リターンclient.invoke(OPNAME、コンテンツ)。
  } 

} 


 第三种:使用@Configuration注解

パッケージcom.dxz.demo.configuration。

輸入org.springframework.context.annotation.Bean。
輸入org.springframework.context.annotation.ComponentScan;
輸入org.springframework.context.annotation.Configuration。
org.springframework.context.annotation.Scopeインポート; 

@Configuration 
// TestBeanに注釈を追加自動スキャン、basePackagesパッケージパス 
@ComponentScan(basePackagesは= " com.dxz.demo.configuration " パブリック クラスTestConfiguration {
     パブリックTestConfiguration(){ 
        システム。OUT .println(" TestConfigurationコンテナが最初に育っている... " ); 
    } 

    / * Beanを登録// @Beanノート、および初期化や破壊方法を指定することができます
    。// @Bean(名前= "testNean" 、にinitMethodは= "スタート" 、およびdestroyMethodを= "クリーンアップ")
    @Bean  
    @Scope( "プロトタイプ")
    公共TestBean testBean(){ 
        )(新しい新しいTestBeanを返します。
    } * / 
} 

 


Spring3から。0 、@は、構成設定クラス、別のXML構成ファイル、@Beanをアノテートされた1つまたは複数の方法で注釈内部クラスを定義するために使用され、これらの方法になりますAnnotationConfigApplicationContextを走査又はAnnotationConfigWebApplicationContextクラス、およびBean定義、初期化Springコンテナを構築するために使用されます。

注意:@Configuration注釈の設定クラスには、次の要件があります。

@Configurationは、最終的なタイプではないかもしれない。
@Configurationは匿名クラスではありません。
ネストされた構成は、静的なクラスです。
、@Configurationロードスプリングで
1.1 、コンフィギュレーション@春の構成とは、春のコンテナ起動
 1.2を、コンフィギュレーションの開始コンテナ@ + 豆登録@Bean
 1.3、コンフィギュレーションの開始コンテナ@ + @Component登録ビーン
 1.4 登録AppContextをクラスAnnotationConfigApplicationContextを使用して、2つのメソッドを
 1.5、Webアプリケーション(web.xmlの構成AnnotationConfigApplicationContext)を構成し

、第二の構成の複数のクラスの組み合わせ
2.1 、@ConfigurationのXML構成ファイル内のスプリングの導入
 2.2 他のノートを導入は、@Configurationに配置
 2.3 、@設定ネストされた(埋め込まれましたコンフィギュレーション・スリーブは、静的クラス)である
EnableXXXアノテーション@ 3、
プロフィール論理グループ構成@ 4、
外部変数用い、5 

、@的な設定ばね荷重方法
1.1 、@構成および開始Spring構成スプリングコンテナ
むしろ、@ConfigurationクラスにマークをこのようなXML構成ファイル内のバネとして <豆> 、関数として:容器に配置されたスプリング(アプリケーション・コンテキスト)を

コードコピー
パッケージcom.dxz.demo.configurationと、

インポートorg.springframework.context.annotation.Configuration; 

@Configuration 
パブリック クラスTestConfiguration {
     公共TestConfiguration(){ 
        システム。OUT .println(" TestConfigurationコンテナは初期化を開始... " ); 
    } 
} 
コードのコピー
に対応する:

コードのコピー
 <XMLバージョン= " 1.0 "エンコード= " UTF-8。"?> 
<豆のxmlns = " HTTP://www.springframework。 ORG /スキーマ/ビーンズ"のxmlns:XSI = " http://www.w3.org/2001/XMLSchema-instance " 
    のxmlns:コンテキスト = " http://www.springframework.org/schema/context のxmlns:JDBC = " http://www.springframework.org/schema/jdbc ""   
    のxmlns:JEE =http://www.springframework.org/schema/jee "のxmlns:TX = " http://www.springframework.org/schema/tx " 
    のxmlns:UTIL = " http://www.springframework.org/schema/ UTIL "のxmlns:タスク= " http://www.springframework.org/schema/task "のxsi:schemaLocationの= "
         のhttp:// www.springframework.org/schema/beans http://www.springframework.org/schema /beans/spring-beans-4.0.xsd 
        ます。http:// www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http:// www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd 
        ます。http:// www.springframework.org/schema/jee のhttp:/ /www.springframework.org/schema/jee/spring-jee-4.0.xsd 
        ます。http:// www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0 .xsd 
        ます。http:// www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 
        ます。http:// www.springframework.org/schema/task HTTP ://www.springframework.org/schema/task/spring-task-4.0.xsd falseに"怠惰デフォルト-INIT ="「>

 
</豆は> 
コードをコピー 
主な試験方法:

コピーコード
パッケージcom.dxz.demo.configuration; 

インポートorg.springframework.context.ApplicationContext; 
インポートorg.springframework.context.annotation.AnnotationConfigApplicationContext ; 

パブリック クラスいるTestMain {
     公共 静的 ボイドメイン(文字列[]引数){ 

        // Springコンテナ積載@Configuration注釈付き及びClassPathXmlApplicationContext AnnotationConfigApplicationContextで置換 
        ApplicationContextのコンテキスト= 新しい新しい AnnotationConfigApplicationContext(TestConfiguration。クラス); 

        // ロードスプリングコンテキスト場合。 xmlファイル:
         // ApplicationContextのコンテキスト=新しい新しい
         // ClassPathXmlApplicationContext(「春のcontext.xml」); 
    } 
} 
コードをコピーは
メインメソッドの実行結果から分かるように、ばねコンテナが起動した:

SpringBoot初期化項目
1 、インターフェイスCommandLineRunner重量を実装するカスタムクラスを作成しますrun()メソッドを記述します。springboot開始後は、デフォルトですべて実現CommandLineRunnerクラスをスキャンし、そのrun()メソッドを実行します。

@Component 
(@Orderを2// 順序を決定する値を開始サイズ順によって
パブリック クラス実装CommandLineRunner {AskForLeave 

    @Override 
    公共 ボイドRUN(文字列...引数)が例外{スロー
        ; askForLeaveを()
    } 

    公共 ボイド)askForLeave( {
        。システムOUTの .println(プロジェクトは、メソッドの実装を開始しました); 
    } 
} 

2 、インターフェースApplicationRunner、オーバーライドrun()メソッドを実装するカスタムクラスを作成します。

@Component 
@Order(3 公共の クラスこんにちは{実装ApplicationRunner 
    @Override 
    公共 ボイドRUNが(ApplicationArgumentsの引数)例外{スロー

        こんにちは(); 
    } 

    パブリック 無効のHello(){ 
        システム。OUTの .println(" プロジェクトは、再びこの時間を起動使用される:継承ApplicationRunner " ); 
    } 
} 

二つの間の差に。
実際には、あなたがより詳細なパラメータを取得したい場合、彼らはApplicationRunnerインターフェイスを使用するように選択することができ、違いはありません。ApplicationArguments:引数は型があります。

抜粋します。https://www.cnblogs.com/gaomanito/p/9604569.html

 

おすすめ

転載: www.cnblogs.com/gllegolas/p/11765010.html