4.Spring-IOC-コンポーネントの作用域、怠惰負荷、条件判断
シリーズの紙面.Spring-IOC-登録コンポーネント .spring-IOC-依存注入 .Spring-IOC-コンポーネントスキャン規則 .Spring-IOC-コンポーネントの作用領域、怠惰負荷、条件判断 .Spring-IOC-コンポーネントのライフサイクル .Spring-AOP-基本使用 スコープ
パラメータ
文字スタイル
説明
SCOPE_SINGLETON
シングルトン
単例の(標準値)ioc容器起動はメソッドを呼び出してオブジェクトをioc容器に入れてから取得するたびにコンテナ(map.get()から直接取ります。
SCOPE_PROTタイプ
プロトタイプ
複数のインスタンスのiocコンテナを起動しても、メソッドを呼び出すことはできません。オブジェクトをコンテナに入れて取得するたびに、メソッドを呼び出してオブジェクトを作成します。
SCOPE_REQUST
request
同一の要求のインスタンスを作成します。
SCOPE_SESSION
セッション
同じセッションの作成例
xml中-任意選択
怠惰なロードは単一のインスタンスに対してのみ有効で、複数のインスタンスの呼び出し時にのみ作成されます。
@Conditional注解は一定の条件で判断し、条件を満たして容器にbeanを登録する
異なるシステムによって、異なるPersonを与える。
クラスに作用して、windowsだけがbeanをロードすることができます。
テストについて
jarパッケージを実行できる場合は、以下の環境変数を指定します。
パラメータ
文字スタイル
説明
SCOPE_SINGLETON
シングルトン
単例の(標準値)ioc容器起動はメソッドを呼び出してオブジェクトをioc容器に入れてから取得するたびにコンテナ(map.get()から直接取ります。
SCOPE_PROTタイプ
プロトタイプ
複数のインスタンスのiocコンテナを起動しても、メソッドを呼び出すことはできません。オブジェクトをコンテナに入れて取得するたびに、メソッドを呼び出してオブジェクトを作成します。
SCOPE_REQUST
request
同一の要求のインスタンスを作成します。
SCOPE_SESSION
セッション
同じセッションの作成例
xml中-任意選択
<bean id="person" class="com.atguigu.bean.Person" scope="prototype" >
<property name="age" value="18">property>
<property name="name" value="zhangsan">property>
bean>
コードの中-任意選択@Configuration
public class MainConfig {
@Bean
@Scope("prototype")
public Person person(){
System.out.println(" Person");
return new Person(" ", 18);
}
}
使用public class MainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println("ioc ....");
Object bean = applicationContext.getBean("person");
Object bean2 = applicationContext.getBean("person");
System.out.println(bean == bean2);
}
}
出力ioc ....
Person
Person
false
ローディング怠惰なロードは単一のインスタンスに対してのみ有効で、複数のインスタンスの呼び出し時にのみ作成されます。
@Lazy
@Bean
public Person person(){
System.out.println(" Person..");
return new Person(" ", 18);
}
使用AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println("ioc ....");
Object bean = applicationContext.getBean("person");
Object bean2 = applicationContext.getBean("person");
System.out.println(bean == bean2);
出力ioc ....
Person..
true
Coditional@Conditional注解は一定の条件で判断し、条件を満たして容器にbeanを登録する
異なるシステムによって、異なるPersonを与える。
/**
* linux
*/
public class LinuxCondition implements Condition {
/**
* ConditionContext: ( )
* AnnotatedTypeMetadata:
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//1、 ioc beanfactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//2、
ClassLoader classLoader = context.getClassLoader();
//3、
Environment environment = context.getEnvironment();
//4、 bean
BeanDefinitionRegistry registry = context.getRegistry();
String property = environment.getProperty("os.name");
//5. bean , bean
boolean definition = registry.containsBeanDefinition("person");
//
return property.contains("linux");
}
}
/**
* windows
*/
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String property = environment.getProperty("os.name");
return property.contains("Windows");
}
}
システムによって違います。AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
String[] names = applicationContext.getBeanDefinitionNames();
Stream.of(names).forEach(System.out::println);
今はmacです。帰ります。person2
クラスに作用するクラスに作用して、windowsだけがbeanをロードすることができます。
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig {
@Bean
public Person person(){
System.out.println(" Person");
return new Person(" ", 18);
}
}
表示AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
String[] names = applicationContext.getBeanDefinitionNames();
Stream.of(names).forEach(System.out::println);
戻る:私はmacなので、ロードのpersonに戻りませんでした。テストについて
jarパッケージを実行できる場合は、以下の環境変数を指定します。
java -jar test.jar -Dos.name=window