Springboot笔记<2>IOC容器与组件注入

IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。应用程序无需直接在代码中new相关的对象,应用程序由IOC容器进行组装。

查看ioc容器中的组件

public static void main(String[] args) {
    //1、返回IOC容器
    ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

    //2、查看容器里面的组件
    String[] names = run.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }
}
/**
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springbootReviewApplication
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration 
...
...
**/

组件添加

Bean四种注入方式

  1. @ComponentScan包扫描+组件标注注解@Component(@Controller@Service@Repository)
  2. @Configuration+@Bean
    使用场景:导入的第三方包里面的组件,将其他jar包中的类(类没有Component等注解),加载到容器中。
  3. @Import快速给容器中导入一个组件
    1)@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
    2)ImportSelector:返回需要导入的组件的全类名数组;
    3)ImportBeanDefinitionRegistrar:手动注册bean到容器中
  4. 使用Spring提供的 FactoryBean(工厂Bean)
    1)默认获取到的是工厂bean调用getObject创建的对象
    2)要获取工厂Bean本身,我们需要给id前面加一个&&xxxFactoryBean 注意类名是X,这里就是小写的x?

@Configuration 和 @Bean

@Configuration(proxyBeanMethods = false) 告诉SpringBoot这是一个配置类、配置文件。@Configuration 用于定义配置类,可替换 xml 配置文件,被注解的类内部包含有一个或多个被 @Bean 注解的方法,这些方法将会被AnnotationConfigApplicationContextAnnotationConfigWebApplicationContext 类进行扫描。

@Bean 是一个方法级别上的注解,主要用在 @Configuration 注解的类里,也可以用在 @Component 注解的类里。添加的bean的id为方法名。标注在方法上,返回某个实例的方法,这个实例就会交给Spring容器管理。

proxyBeanMethods

  • FULL模式: @Configuration(proxyBeanMethods = true) ,保证每个@Bean方法被调用多少次返回的组件都是单实例的。配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用 FULL 模式。
  • LITE模式: @Configuration(proxyBeanMethods = false) ,每个@Bean方法被调用多少次返回的组件都是新创建的,配置类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断。
  • Spring Boot 在2.2.0版本(依赖于Spring 5.2.0)起就把它的所有的自动配置类默认proxyBeanMethods = false,提高Spring启动速度。
@Configuration(proxyBeanMethods = false)
public class ConfigDemo1 {
    @Bean
    public Student getStudent() {
        Student student = new Student();
        student.setAge(18);
        student.setName("xiaoming");
        return student;
    }
}
@SpringBootApplication
public class SpringbootReviewApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootReviewApplication.class, args);
        ConfigDemo1 configDemo1 = run.getBean(ConfigDemo1.class);
        Student student1 = configDemo1.getStudent();
        Student student2 = configDemo1.getStudent();
        //返回false,如果proxyBeanMethods = true,则返回true
        System.out.println(student1 == student2);
    }
}

@Bean,@Component,@Service,@Repository 和 @Controller注解的区别

1.@Bean

标注在方法上,返回某个实例的方法,这个实例就会交给Spring容器管理。

2.@Component

作用于类上,相当于一个基类,跟 @Bean 一样,可以托管到Spring容器进行管理。表示一个方法实例化、配置或者初始化一个Spring IoC容器管理的新对象

3.@Service, @Controller , @Repository

作用于类上,={@Component + 一些特定的功能}。这些注解在部分功能上是一样的,但有一些不同:

@Controller注解类:SpringMVC的理念,进行前端请求的处理,转发,重定向。包括调用Service层的方法。

@Service注解类:处理业务逻辑

@Repository注解类:作为DAO对象(数据访问对象,Data Access Objects),这些类可以直接对数据库进行操作。

@Controller、@Service、@Repository都属于@Component

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
   @AliasFor(annotation = Component.class)
   String value() default "";
}

使用bean

以上都是注册bean,那怎么使用bean呢?

@Autowired 属于Spring的注解             org.springframework.beans.factory.annotation.Autowired

@Resource  不属于Spring的注解,JDK1.6支持的注解   javax.annotation.Resource

@Autowired
根据类的类型进行装配
@Resource
根据类的名字进行装配,也可以设定根据类的名字

未经作者同意请勿转载

本文来自博客园作者:aixueforever,原文链接:https://www.cnblogs.com/aslanvon/p/15715084.html

原文地址:https://www.cnblogs.com/aslanvon/p/15715084.html