Spring5配置组件介绍

Spring核心注解
本文介绍Spring框架中的核心注解,包括@Configuration、@ComponentScan、@Scope等,并详细解释了它们的作用与使用方法。

目录

1.@Configuration

2.@ComponentScan

 3.@Scope

4.@Lazy

5.@Conditional

6.@Import

7.给Ioc容器注册Bean的方式

8.生命周期控制 


1.@Configuration

说明:把一个类作为一个loC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。

代码演示:

Student实体类

@Data
public class Student {
    private String name;
    private Integer age;
    public Student() {

    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

 @Configuration注解使用

@Configuration
public class MyConfigura {

    //默认是方法名
    //指定value 则以value为准
    @Bean("stu")
    public Student student() {
        return new Student("ws", 18); 
    }

}

Test:

    @Test
    public void Test1() {
        ApplicationContext app = new AnnotationConfigApplicationContext(MyConfigura.class);

//        Object bean = app.getBean(Student.class);
        Object bean = app.getBean("stu");
        System.out.println(bean);
    }

2.@ComponentScan

说明:在配置类上添加@ComponentScan注解。该注解默认会扫描该类所在的包下所有的配置类,相当于之前的<context:component-scan>

默认扫描(@Controller、@Service、@Repostory、@Component)

@ComponentScan的重要属性:

value 代表扫描的包 value = "com.ws.project"代表扫描com.ws.project包下面所有的类

useDefaultFilters = true;默认是true 代表使用默认的策略 也就是扫描注解

useDefaultFilters = false代表使用别的策略

includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class)}},//代表扫描com.ws.project 下的Controller注解所在的类

includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = StudentService.class)} //代表扫描com.ws.project 下的StudentService类

//@ComponentScan.Filter(type = FilterType.CUSTOM)自己扩展策略

代码演示:

配置类:

@Configuration
@ComponentScan(value = "com.ws.project" )

public class MyConf {
}

Test:

@Test
    public void Test2() {
        ApplicationContext app = new AnnotationConfigApplicationContext(MyConf.class);

        String[] beanDefinitionNames = app.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));

    }

 3.@Scope

说明:用于指定scope作用域的(用在类上)

Scope的value属性:

  • prototype原型,多例
  • singleton单例
  • request主要应用于web模块同一次请求只创建一个实例
  • session主要应用于web模块,同一个session只创建一个对象

默认是单例

4.@Lazy

说明:表示延迟初始化(就是只有当我们调用bean的时候,这个bean才会被初始化)

@Lazy只对单例bean起作用

5.@Conditional

说明:Spring4开始提供,它的作用是按照一定的条件进行判断,满足条件给容器注册Bean。

属性是实现了Conditional接口的类

6.@Import

说明:导入外部资源

可以手动的把一个类加入到容器中

  • a.Import直接参数导入
  • b.实现Importselector自定义规则实现
  • c.实现ImportBeanDefinitionRegistrar,获得BeanDefinitionRegistry可以手动直接往Ioc容器中塞

import加入的bean是全类名

7.给Ioc容器注册Bean的方式

  • @Bean直接导入单个类
  • @ComponentScan 默认扫描(@Controller.@Service、@Repostory、@Component)
  • @Import快速给容器导入Bean的方式
  • FactoryBean把需要注册的对象封装为FactoryBean

FactoryBean负责将Bean注册到IoC的

BeanBeanFactory 是从IoC容器中获得Bean对象的

8.Bean生命周期控制 

@PostConstruct用于指定初始化方法(用在方法上)

@PreDestory用于指定销毁方法(用在方法上)

@DependsOn :定义Bean初始化及销毁时的顺序

Bean生命周期的监控实现方式

  1. 配置@Bean的参数
  2. 分别实现InitializingBean和DisposableBean接口
  3. 使用@PostConstruct和@PreDestroy注解
  4. 自己写一个类,实现BeanPostProcessor接口(这种方式可以获取所有的bean)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w7486

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值