Spring注記の@Lazy注記
1402 ワード
1,@Lazy注記は何ですか
@Lazy注記は、beanが遅延ロードを必要とするかどうかを識別するために使用されます.ソースコードは次のとおりです.
パラメータは1つしかありません.デフォルトはtrueです.つまり、この注釈を付けるとロードが遅延します.
2,@Lazy注記の使い方
注釈を付けない前にメインコンテナが起動するとbeanがインスタンス化され、以下のようになります.
@Lazy注記を追加すると、最初の呼び出し時に次のようにロードされる必要があります.
@Lazy注記注記の役割は主にspringIOCコンテナの起動のロード時間を減らすことです
@Lazy注記は、beanが遅延ロードを必要とするかどうかを識別するために使用されます.ソースコードは次のとおりです.
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
/**
* Whether lazy initialization should occur.
*/
boolean value() default true;
}
パラメータは1つしかありません.デフォルトはtrueです.つまり、この注釈を付けるとロードが遅延します.
2,@Lazy注記の使い方
注釈を付けない前にメインコンテナが起動するとbeanがインスタンス化され、以下のようになります.
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
user
@Lazy注記を追加すると、最初の呼び出し時に次のようにロードされる必要があります.
/**
* bean
* @return
*/
@Scope
@Lazy
@Bean(value="user0",name="user0",initMethod="initUser",destroyMethod="destroyUser")
public User getUser(){
System.out.println(" user ");
return new User(" ",26);
}
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
User bean2 = applicationContext2.getBean(User.class);
user
1 === User [userName= , age=26]
@Lazy注記注記の役割は主にspringIOCコンテナの起動のロード時間を減らすことです