3、春ELと豆スコープ

1、Beanのスコープ
  スコープSpringコンテナを作成することは@Scope(「XXXX」)リアクションによって達成、Beanインスタンスである方法を説明
  Beanのコンテナ春の一例に過ぎず、春はすべてのコンテナインスタンスを共有し、デフォルトの設定です:シングルトン
    コメントにデフォルトをそれは、マルチサーバデプロイメント、ロードバランシングが存在、各サーバーで同じ豆が存在ある場合@Service豆や他のデフォルトは、単一例です。
  プロトタイプ:各呼び出しBeanの新しいインスタンスを作成するために
    getBeanにより取得したプロトタイプモデル、注射または他の豆を、のインスタンスを作成します
  要求を:すべてのHTTP要求に新しいBeanインスタンスを作成するために、
  新しいBeanインスタンスを作成するために、各HTTPセッション:セッションを
2、春を呼び出しにELや資源
  式言語の
  使用リソース注入ELを達成することができ、初期化を理解することは簡単であるリソース

1  インポートorg.apache.commons.io.IOUtils。
2  インポートorg.springframework.beans.factory.annotation.Autowired。
3  インポートorg.springframework.beans.factory.annotation.Value。
4  インポートorg.springframework.context.annotation.Bean。
5  インポートorg.springframework.context.annotation.ComponentScan。
6  インポートorg.springframework.context.annotation.Configuration。
7  インポートorg.springframework.context.annotation.PropertySource。
8  インポートorg.springframework.context.support.PropertySourcesPlaceholderConfigurer。
9  インポートorg.springframework.core.env.Environment;
10  インポートorg.springframework.core.io.Resource。
11  
12  @Configuration
 13 @ComponentScan( "com.ning.ch2.el. *" 14 @PropertySource( "クラスパス:COM /寧/ CH2 / EL / el.properties" 15  パブリック クラスElConfig {
 16      // 注入普通字符
17      @value(「これは春ブーツである」18      プライベート通常の文字列。
19      
20      // 注入操作系统属性
21      @value( "#{systemProperties [ 'os.name']}" 22      民間OSNAME文字列、
 23は     
24      // 式の噴射動作結果
25      @value( "#{T(java.lang.Mathに).random()} 100.0 *" 26は、     プライベート ダブル乱数;
 27の     
28      // 他のビーンの注入特性
 29      // プライベートプロパティ名ゲッターメソッドが必要開始小文字funServiceのクラス名
30      @value(「funService.name番号を{}」31が     プライベートストリングfromFunServiceName;
 32      
33である     // 注入プロファイル
 34      //は、ファイルの場所を指定されています、または豆の複雑な構成PropertySourcesPlaceholderConfigurer @PropertySource注釈を必要としない
35      ( ":COM /寧/ CH2 / EL / el.properties CLASSPATH" @valueを36      専用リソースTESTFILE;
 37 [      
38は、     // 噴射URLのコンテンツ
39      @value( "https://www.baidu.com/" 40      専用リソースtestUrl;
 41である     
42れる     // @value(「HTTPS://www.cnblogs。 COM / shipengzhi /記事/ 2099693.html ")
 43がある     // リソースbolgUrlプライベート;
 44は     
45      // 注射属性ファイル
 46      // ファイルの必要性の@PropertySourceの場所と設定PropertySourcesPlaceholderConfigurerビーン
47      @value("} $ {book.name " 48      プライベート文字列bookName;
 49      
50      //注入的配置文件可以从環境获取
51      @Autowired
 52      プライベート環境環境。
53      
54      @Bean
 55      パブリック 静的PropertySourcesPlaceholderConfigurer propertyConfigure(){
 56          リターン 新しいPropertySourcesPlaceholderConfigurer()。
57      }
 58      
59      公共 ボイドoutputResource(){
 60          のSystem.out.println( "開始..." )。
61          のSystem.out.println(正常);
62          のSystem.out.println(OSNAME)。
63          のSystem.out.println(乱数)。
64          のSystem.out.println(fromFunServiceName)。
65          試み{
 66              のSystem.out.println(IOUtils.toString(testFile.getInputStream()))。
67              のSystem.out.println(IOUtils.toString(testUrl.getInputStream()));
68              // するSystem.out.println(IOUtils.toString(bolgUrl.getInputStream())); 
69          } キャッチ(IOExceptionを電子){
 70              e.printStackTrace();
71          }
 72          
73          のSystem.out.println(bookName)。
74          // 需要@PropertySource指定配置文件位置
75         System.out.println(environment.getProperty( "book.anthor" ));
76          のSystem.out.println( "終わり..." );
77      }
 78 }

 

おすすめ

転載: www.cnblogs.com/6xiong/p/12005574.html