1. @Configuration 告诉Spring这是一个配置类
2. @ComponentScan
- value:指定要扫描的包
- excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
- includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
- FilterType.ANNOTATION:按照注解
- FilterType.ASSIGNABLE_TYPE:按照给定的类型;
- FilterType.ASPECTJ:使用ASPECTJ表达式
- FilterType.REGEX:使用正则指定
- FilterType.CUSTOM:使用自定义规则
3. @Bean 给容器中注册一个Bean
4. @Scope 调整作用域
ConfigurableBeanFactory#SCOPE_PROTOTYPE prototype
ConfigurableBeanFactory#SCOPE_SINGLETON singleton
org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request
org.springframework.web.context.WebApplicationContext#SCOPE_SESSION sesssion
- prototype:多实例的:ioc容器启动并不会去调用方法创建对象放在容器中,每次获取的时候才会调用方法创建对象
- singleton:单实例的(默认值):ioc容器启动会调用方法创建对象放到ioc容器中,以后每次获取就是直接从容器(map.get())中拿
- request:同一次请求创建一个实例
- session:同一个session创建一个实例
5. @Lazy 懒加载:容器启动不创建对象,第一次使用(获取)Bean时创建对象,并初始化
6. @Conditional 按照一定的条件进行判断,满足条件给容器中注册bean,可以标记类和方法
7. @Import 快速给容器中导入一个组件,容器中会自动注册该组件,id默认是组件的全类名
给容器中注册组件:
1. 包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[自己写的类]
2. @Bean[导入的第三方包里面的组件,创建一个方法,标注@Bean后将该组件加入容器中]
3. @Import[快速给容器中导入一个组件]
- @Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
- ImportSelector:返回需要导入的组件的全类名数组;
- ImportBeanDefinitionRegistrar:手动注册bean到容器中
4. 使用Spring提供的 FactoryBean(工厂Bean);
- 默认获取到的是工厂bean调用getObject创建的对象
- 要获取工厂Bean本身,我们需要给id前面加一个&
&colorFactoryBean
应用上下文:将你需要让spring帮你管理的对象放入容器的一种对象。继承了BeanFactory后派生而来的应用上下文,它的抽象接口是ApplicationContext, 具有解析配置文本信息等功能。有了上下文对象,就能向容器注册你需要Spring管理的对象了。
应用:
1,2,3,4,5:
//配置类
@Configuration//告诉Spring这是一个配置类
public class MainConfig {
//@Scope("prototype")
@Lazy
@Bean//给容器中注册一个bean,类型为返回值的类型,id默认用方法做id
public Person person01(){
return new Person("zt",23);
}
}
public class MainTest {
public static void main(String[] args) {
// ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// Person person = (Person) context.getBean("person");
// System.out.println(person);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
Person person = (Person) context.getBean(Person.class);
System.out.println(person);
String[] beanNamesForType = context.getBeanNamesForType(Person.class);
for (String s : beanNamesForType) {
System.out.println(s);
}
}
}
测试结果:
6:
package com.ice.condition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
//判断是否linux系统
public class LinuxCondition implements Condition {
/**
* ConditionContext:判断条件能使用的上下文(环境)
* AnnotatedTypeMetadata:注释信息
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// TODO是否linux系统
//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");
//可以判断容器中的bean注册情况,也可以给容器中注册bean
boolean definition = registry.containsBeanDefinition("person");
if(property.contains("linux")){
return true;
}
return false;
}
}
package com.ice.condition;
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;
}
}
@Configuration
public class MainConfig2 {
//默认都是单实例对象
@Bean(value="person")
public Person person(){
return new Person("dq",18);
}
/**
* @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);
}
}
测试:
@Test
public void test03(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("os.name");//动态获取环境变量的值
System.out.println("os.name="+property);
for (String name : namesForType) {
System.out.println(name);
}
Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
System.out.println(persons);
}
当前系统为Windows,所以结果中没有linux