スプリングコメント-自動組立
8110 ワード
Springは依存注入(DI)を利用してIOC容器中の各コンポーネントに対する依存関係の割り当てを完了した.
一、@Autowiredデフォルトの優先順位は、タイプ別にコンテナに対応するコンポーネントを探すことです. 同じタイプのコンポーネントが複数見つかったら、属性の名称をコンポーネントのidとしてコンテナから検索します. 自動組立デフォルトでは属性の値を必ず付けなければなりません.エラーが発生しません.@Autowired(required=false)が使えます. 表示の位置
コンストラクタ、パラメータ、方法、属性.方法位置に表示されています.beanを構築するときはパラメータに@Autowiredを書かなくてもいいです.パラメータの値は自動的に容器から取得します.setXXXは@Autowiredを追加する必要があります. は、コンストラクタに表示されています.コンポーネントが一つしかない場合、このコンストラクタのある@Autowiredは省略できます.パラメータ位置のコンポーネントは自動的に容器から を取得します.パラメータ位置 に表示されます.
@Qualfier
@Qualfier(「xxDao」)属性名ではなく、組み立てが必要なコンポーネントのIDを指定します.
@Primary
Springを自動組立させる場合、デフォルトでは優先のbeanを使用します.@Qualfierを使って組み立てるべきbeanの名前を指定してもいいです.
二、@Resourceデフォルトではコンポーネント名に従って組み立てられていますが、name属性によって を指定することもできます.@Primary機能、reqiured=false はサポートされていません.
三、@Injectはjavax.injectのカバンを導入する必要があります.Autowiredの機能とほぼ同じです.required=falseの機能がない 四、xxAware Spring容器の下の部分を使用することを実現する.
実現原理は、beanを作成した後、一連のバックプロセッサ(XBeanPostProcessor)がフロント処理を行い、条件を満たすとインターフェース規定の方法を呼び出して関連するコンポーネントを注入することである.
現在の環境に応じて、動的なアクティブ化と一連のコンポーネントの機能を切り替えます.に環境マークが付加されたbeanは、この環境が活性化されたときのみ容器に登録されます.デフォルトはdefault環境 です.設定クラスに書いてありますが、指定された環境の場合だけ、配置クラス全体のすべての構成が有効になります. 環境マークが表示されているbeanは、どの環境においてもローディングされている(構成クラスが有効である場合) .
締め括りをつける
@Autowired:Spring定義の;@Resource、@Injectはいずれもjava仕様です.これらはAutowiredAnnotationBenPostProcessor解析によって自動組立機能を完成します.
一、@Autowired
コンストラクタ、パラメータ、方法、属性.
@Autowired
public void setCar(Car car) {
this.car = car;
}
@Bean
public Color color(Car car){
Color color = new Color();
color.setCar(car);
return color;
}
public Boss(Car car){
this.car = car;
System.out.println("Boss... ");
}
public Boss(@AutoWired Car car){
this.car = car;
System.out.println("Boss... ");
}
@Qualfier
@Qualfier(「xxDao」)属性名ではなく、組み立てが必要なコンポーネントのIDを指定します.
@Primary
Springを自動組立させる場合、デフォルトでは優先のbeanを使用します.@Qualfierを使って組み立てるべきbeanの名前を指定してもいいです.
二、@Resource
三、@Inject
実現原理は、beanを作成した後、一連のバックプロセッサ(XBeanPostProcessor)がフロント処理を行い、条件を満たすとインターフェース規定の方法を呼び出して関連するコンポーネントを注入することである.
@Component
public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
System.out.println(" ioc:"+applicationContext);
this.applicationContext = applicationContext;
}
@Override
public void setBeanName(String name) {
// TODO Auto-generated method stub
System.out.println(" bean :"+name);
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
// TODO Auto-generated method stub
String resolveStringValue = resolver.resolveStringValue(" ${os.name} #{20*18}");
System.out.println(" :"+resolveStringValue);
}
五、@Profile現在の環境に応じて、動的なアクティブ化と一連のコンポーネントの機能を切り替えます.
//1、 : -Dspring.profiles.active=test
//2、
@Test
public void test01(){
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext();
//1、 applicationContext
//2、
applicationContext.getEnvironment().setActiveProfiles("dev");
//3、
applicationContext.register(MainConfigOfProfile.class);
//4、
applicationContext.refresh();
String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String string : namesForType) {
System.out.println(string);
}
Yellow bean = applicationContext.getBean(Yellow.class);
System.out.println(bean);
applicationContext.close();
}
締め括りをつける
@Autowired:Spring定義の;@Resource、@Injectはいずれもjava仕様です.これらはAutowiredAnnotationBenPostProcessor解析によって自動組立機能を完成します.