Spring的容器類
- xmlBean配置容器類:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
- 註解Bean配置容器類():
ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.i9577.config");
ApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
- 每種容器類都有多種構造器供選擇,常用的方法如下:
Person person = (Person) applicationContext.getBean("person");//獲取對應id的bean的實體類
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);//獲取當前傳入類型的所有容器中bean的名稱
String[] beanDefinitionNames1 = applicationContext.getBeanDefinitionNames();//獲取當前容器內所有註冊bean的名稱
Environment environment = applicationContext.getEnvironment();//獲取當前運行環境的系統參數
1.組件的註冊
/**
* 給容器中註冊組件;
* 1)、包掃描+組件標註註解(@Controller/@Service/@Repository/@Component)[自己寫的類]
* 2)、@Bean[導入的第三方包裏面的組件]
* 3)、@Import[快速給容器中導入一個組件]
* 1)、@Import(要導入到容器中的組件);容器中就會自動註冊這個組件,id默認是全類名
* 2)、ImportSelector:返回需要導入的組件的全類名數組;
* 3)、ImportBeanDefinitionRegistrar:手動註冊bean到容器中
* 4)、使用Spring提供的 FactoryBean(工廠Bean);
* 1)、默認獲取到的是工廠bean調用getObject創建的對象
* 2)、要獲取工廠Bean本身,我們需要給id前面加一個&
* &colorFactoryBean
*/
@Configuration-指定配置類
配置類==配置文件
@Configuration //告訴Spring這是一個配置類
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
...
}
@Target(ElementType.TYPE)可知該類只適用於註解在類或接口上;
@Retention(RetentionPolicy.RUNTIME) 指定生命週期爲運行時,才能通過反射獲取
@ComponentScan-自動掃描組件到IOC容器
相當於原xml配置中的
<context:component-scan base-package="com.i9577"></context:component-scan>
@ComponentScans(
value = {
@ComponentScan(value="com.i9577",includeFilters = {
/* @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/
@Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})
},useDefaultFilters = false)
}
)
//@ComponentScan value:指定要掃描的包
//excludeFilters = Filter[] :指定掃描的時候按照什麼規則排除那些組件
//includeFilters = Filter[] :指定掃描的時候只需要包含哪些組件
//FilterType.ANNOTATION:按照註解
//FilterType.ASSIGNABLE_TYPE:按照給定的類型;
//FilterType.ASPECTJ:使用ASPECTJ表達式
//FilterType.REGEX:使用正則指定
//FilterType.CUSTOM:使用自定義規則
自定義過濾規則實現TypeFilter 接口,實現match方法
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
public class MyTypeFilter implements TypeFilter {
/**
* metadataReader:讀取到的當前正在掃描的類的信息
* metadataReaderFactory:可以獲取到其他任何類信息的
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
// TODO Auto-generated method stub
//獲取當前類註解的信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//獲取當前正在掃描的類的類信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//獲取當前類資源(類的路徑)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
System.out.println("--->"+className);
if(className.contains("er")){
return true;
}
return false;
}
}
@Bean-註冊實體類到IOC容器
一般寫在配置類中:
@Bean //給容器中註冊一個Bean;類型爲返回值的類型,id默認是用方法名作爲id
public Person person() {
return new Person("008", "二傻");
}
相當於原先xml如下配置:
<bean id="person" class="com.i9577.bean.Person" scope="prototype">
<property name="id" value="007"></property>
<property name="name" value="詹姆斯邦德"></property>
</bean>
@Scope-設置組件的作用域
組件在IOC容器中默認註冊都是單例(singleton),可通過@Scope調整爲多實例和其他作用域。
//默認是單實例的
/**
* ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION sesssion
* @return\
* @Scope:調整作用域
* prototype:多實例的:ioc容器啓動並不會去調用方法創建對象放在容器中。
* 每次獲取的時候纔會調用方法創建對象;
* singleton:單實例的(默認值):ioc容器啓動會調用方法創建對象放到ioc容器中。
* 以後每次獲取就是直接從容器(map.get())中拿,
* request:同一次請求創建一個實例
* session:同一個session創建一個實例
*
*/
@Scope("prototype")
@Bean("person")
public Person person(){
return new Person();
}
@Lazy-組件在IOC容器內實現懶加載
/* 懶加載:
* 單實例bean:默認在容器啓動的時候創建對象;
* 懶加載:容器啓動不創建對象。第一次使用(獲取)Bean創建對象,並初始化;
* /
@Conditional-按照條件註冊Bean
可作用在配置類上,也可作用在配置方法上
/**
* @Conditional({Condition}) : 按照一定的條件進行判斷,滿足條件給容器中註冊bean
*
* 如果系統是windows,給容器中註冊("bill")
* 如果是linux系統,給容器中註冊("linus")
*/
@Conditional(WindowsCondition.class)
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
需要創建自定義類實現Condition接口,並實現matches方法
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
//判斷是否windows系統
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String property = environment.getProperty("os.name");
if(property.contains("Windows")){
return true;
}
return false;
}
}
@Import-給容器中快速導入一個組件
- 直接導入一個或多個類
@Import({Color.class,Red.class})
- 實現ImportSelector接口的selectImports方法,並返回需要導入的組件的全類名數組;
@Import({MyImportSelector .class})
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
//自定義邏輯返回需要導入的組件
public class MyImportSelector implements ImportSelector {
//返回值,就是到導入到容器中的組件全類名
//AnnotationMetadata:當前標註@Import註解的類的所有註解信息
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// TODO Auto-generated method stub
//importingClassMetadata
//方法不要返回null值
return new String[]{"com.i9577.bean.Person","com.i9577.bean.Blue"};
}
}
- 實現ImportBeanDefinitionRegistrar的registerBeanDefinitions方法手動註冊bean到容器
@Import({MyImportBeanDefinitionRegistrar .class})
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import com.i9577.bean.RainBow;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
* AnnotationMetadata:當前類的註解信息
* BeanDefinitionRegistry:BeanDefinition註冊類;
* 把所有需要添加到容器中的bean;調用
* BeanDefinitionRegistry.registerBeanDefinition手工註冊進來
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean definition = registry.containsBeanDefinition("com.i9577.bean.Red");
boolean definition2 = registry.containsBeanDefinition("com.i9577.bean.Blue");
if(definition && definition2){
//指定Bean定義信息;(Bean的類型,Bean。。。)
RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
//註冊一個Bean,指定bean名
registry.registerBeanDefinition("rainBow", beanDefinition);
}
}
}
FactoryBean-Spring提供的 FactoryBean(工廠Bean)
1)、默認獲取到的是工廠bean調用getObject創建的對象
2)、要獲取工廠Bean本身,我們需要給id前面加一個&
如:&colorFactoryBean
@Bean
public ColorFactoryBean colorFactoryBean(){
return new ColorFactoryBean();
}
import org.springframework.beans.factory.FactoryBean;
//創建一個Spring定義的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {
//返回一個Color對象,這個對象會添加到容器中
@Override
public Color getObject() throws Exception {
// TODO Auto-generated method stub
System.out.println("ColorFactoryBean...getObject...");
return new Color();
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Color.class;
}
//是單例?
//true:這個bean是單實例,在容器中保存一份
//false:多實例,每次獲取都會創建一個新的bean;
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}