后端开发速查:必备的Spring IOC 容器底层注解使用【完整版】

📌个人主页个人主页
​🧀 推荐专栏后端开发成神之路
📝作者简介:一个在读研中创业、工作中学习,能搞全栈、也搞算法、目前在搞大数据的大厂打工人。真诚地想分享自己的内容,希望能帮到你,一起学习、一起进步!
⭐️关注我,持续分享干货!用实打实的经验来帮你少走弯路⭐️


Spring IOC 容器底层注解使用

在前文我们已经对于Spring架构中常用的模块已经有了整体的认知,详情可参考:
Spring架构的基础模块大全

本文我们来详细介绍IOC容器中常用的注解的配置和使用,强烈建议收藏,以后再遇到某个注解的参数配置不清晰时,完全可省去百度的多余时间,直接查看本文,快速解答你的疑惑!

一、Bean的定义

1.1 xml配置文件的形式

①:基于xml的形式定义Bean的信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
//定义一个Bean的信息
<bean id="car" class="com.taoren.compent.Car"></bean>
</beans>

去容器中读取Bean

public static void main( String[] args )
{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBean("person"));
}

1.2 配置类的形式

②:基于读取配置类的形式定义Bean信息

@Configuration
public class MainConfig {
@Bean
public Person person(){
return new Person();
}
}

注意: 通过@Bean的形式是使用的话, bean的默认名称是方法名

  • 若@Bean(value=“bean的名称”),那么bean的名称是指定的去容器中读取Bean的信息(传入配置类)
public static void main( String[] args )
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println(ctx.getBean("person"));
}

二、@CompentScan注解进行包扫描

@Configuration
@ComponentScan(basePackages = {"com.TaoRen.testcompentscan"})
public class MainConfig {
}

2.1 排除用法 excludeFilters

@Configuration
@ComponentScan(basePackages = {"com.taoren.testcompentscan"},excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class}),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = {TaoRenService.class})
})
public class MainConfig {
}

2.2 包含用法 includeFilters

注意,若使用包含的用法,需要把useDefaultFilters属性设置为false(true表示扫描全部的)

@Configuration
@ComponentScan(basePackages = {"com.taoren.testcompentscan"},includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, Service.class})
},useDefaultFilters = false)
public class MainConfig {
}

2.3 @ComponentScan.Filter type的类型

  • a)注解形式的FilterType.ANNOTATION
@Controller 
@Service
@Repository
@Component
  • b)指定类型的 FilterType.ASSIGNABLE_TYPE
@ComponentScan.Filter(type =FilterType.ASSIGNABLE_TYPE,value = {TaoRenService.class})
  • c)aspectj类型的 FilterType.ASPECTJ(不常用)
  • d)正则表达式的 FilterType.REGEX(不常用)
  • e)自定义的 FilterType.CUSTOM:
public enum FilterType {
//注解形式 比如@Controller @Service @Repository @Compent
ANNOTATION,
//指定的类型
ASSIGNABLE_TYPE,
//aspectJ形式的
ASPECTJ,
//正则表达式的
REGEX,
//自定义的
CUSTOM
}

FilterType.CUSTOM 自定义类型如何使用

public class TaoRenFilterType implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, Metadata
ReaderFactory metadataReaderFactory) throws IOException {
//获取当前类的注解源信息
AnnotationMetadata annotationMetadata = metadataReader.getA>nnotationMetadata();
//获取当前类的class的源信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取当前类的资源信息
Resource resource = metadataReader.getResource();
if(classMetadata.getClassName().contains("dao")) {
return true;
}
return false;
}
}
@ComponentScan(basePackages = {"com.TaoRen.testcompentsca>n"},includeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM,value = TaoRenFilterType.class)
},useDefaultFilters = false)
public class MainConfig {
}

三、配置Bean的作用域对象

3.1 @Scope的默认使用

在不指定@Scope的情况下,所有的bean都是单实例的bean,而且是饿汉加载(容器启动实例就创建好了)

@Bean
public Person person() {
return new Person();
}

3.2 指定@Scope为 prototype 表示为多实例

该模式还是懒汉模式加载(IOC容器启动的时候,并不会创建对象,而是在第一次使用的时候才会创建)

@Bean
@Scope(value = "prototype")
public Person person() {
return new Person();
}

3.3 @Scope指定的作用域方法取值

  • a) singleton 单实例的(默认)
  • b) prototype 多实例的
  • c) request 同一次请求
  • d) session 同一个会话级别

3.4 Bean的懒加载@Lazy

主要针对单实例的bean 容器启动的时候,不创建对象,在第一次使用的时候才会创建该对象

@Bean
@Lazy
public Person person() {
return new Person();
}

四、 @Conditional进行条件判断等.

场景,有二个组件TaoRenAspect 和TaoRenLog ,我的TaoRenLog组件是依赖于TaoRenAspect的组件

应用:自己创建一个TaoRenCondition的类 实现Condition接口
public class TaoRenCondition implements Condition {
/**
*
* @param context
* @param metadata
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//判断容器中是否有TaoRenAspect的组件
if(context.getBeanFactory().containsBean("TaoRenAspect")) {
return true;
}
return false;
}
}
public class MainConfig {
@Bean
public TaoRenAspect taorenAspect() {
return new TaoRengAspect();
}
//当切 容器中有taorenAspect的组件,那么taorenLog才会被实例化.
@Bean
@Conditional(value = TaoRenCondition.class)
public TaoRenLog taorenLog() {
return new TaoRenLog();
}
}

五、往IOC 容器中添加组件的方式

5.1 通过@CompentScan +@Controller @Service @Respository @component

适用场景: 针对我们自己写的组件可以通过该方式来进行加载到容器中。

  • ②:通过@Bean的方式来导入组件(适用于导入第三方组件的类)
  • ③:通过@Import来导入组件 (导入组件的id为全类名路径
@Configuration
@Import(value = {Person.class, Car.class})
public class MainConfig {
}

5.2 通过@Import 的ImportSeletor类实现组件的导入 (导入组件的id为全类名路径)

public class TaoRenImportSelector implements ImportSelector {
/可以获取导入类的注解信息
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.TaoRen.testimport.compent.Dog"};
}
}
@Configuration
@Import(value = {Person.class, Car.class, TaoRenImportSelector.class})
public class MainConfig {
}

5.3 通过@Import的 ImportBeanDefinitionRegister导入组件 (可以指定bean的名称)

public class TaoRenBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//创建一个bean定义对象
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Cat.class);
//把bean定义对象导入到容器中
registry.registerBeanDefinition("cat",rootBeanDefinition);
}
}
@Configuration
//@Import(value = {Person.class, Car.class})
//@Import(value = {Person.class, Car.class, TaoRenImportSelector.class})
@Import(value = {Person.class, Car.class, TaoRenImportSelector.class, TaoRenBeanDefinitionRegister.class})
public class MainConfig {
}

5.4 通过实现FacotryBean接口来实现注册组件

public class CarFactoryBean implements FactoryBean<Car> {
/返回bean的对象
@Override
public Car getObject() throws Exception {
return new Car();
}
/返回bean的类型
@Override
public Class<?> getObjectType() {
return Car.class;
}
/是否为单利
@Override
public boolean isSingleton() {
return true;
}
}

六、Bean的初始化方法和销毁方法.

  • ①:什么是bean的生命周期?

bean的创建----->初始化----->销毁方法

由容器管理Bean的生命周期,我们可以通过自己指定bean的初始化方法和bean的销毁方法

@Configuration
public class MainConfig {
//指定了bean的生命周期的初始化方法和销毁方法.
@Bean(initMethod = "init",destroyMethod = "destroy")
public Car car() {
return new Car();
}
}

针对单实例bean的话,容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法针对多实例bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受IOC容器的管理.

  • ②:通过 InitializingBean和DisposableBean 的二个接口实现bean的初始化以及销毁方法
@Component
public class Person implements InitializingBean,DisposableBean {
public Person() {
System.out.println("Person的构造方法");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean的destroy()方法 ");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean的 afterPropertiesSet方法");
}
}
  • ③:通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法
@Component
public class Book {
public Book() {
System.out.println("book 的构造方法");
}
@PostConstruct
public void init() {
System.out.println("book 的PostConstruct标志的方法");
}
@PreDestroy
public void destory() {
System.out.println("book 的PreDestory标注的方法");
}
}
  • ④:通过Spring的BeanPostProcessor的 bean的后置处理器会拦截所有bean创建过程

postProcessBeforeInitialization 在init方法之前调用

**postProcessAfterInitialization 在init方法之后调用

@Component
public class TaoRenBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("TaoRenBeanPostProcessor...postProcessBeforeInitialization:"+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("TaoRenBeanPostProcessor...postProcessAfterInitialization:"+beanName);
return bean;
}
}

BeanPostProcessor的执行时机

populateBean(beanName, mbd, instanceWrapper)
initializeBean{
applyBeanPostProcessorsBeforeInitialization()
invokeInitMethods{
isInitializingBean.afterPropertiesSet
自定义的init方法
}
applyBeanPostProcessorsAfterInitialization()方法
}

七、通过@Value +@PropertySource来给组件赋值

public class Person {
//通过普通的方式
@Value("司马")
private String firstName;
//spel方式来赋值
@Value("#{28-8}")
private Integer age;
通过读取外部配置文件的值
@Value("${person.lastName}")
private String lastName;
}
@Configuration
@PropertySource(value = {"classpath:person.properties"}) //指定外部文件的位置
public class MainConfig {
@Bean
public Person person() {
return new Person();
}
}

八、自动装配 @AutoWired的使用

自动注入:

//一个Dao
@Repository
public class TaoRenDao {
}
@Service
public class TaoRenService {
@Autowired
private TaoRenDao TaoRenDao;

结论:

  • a:自动装配首先时按照类型进行装配,若在IOC容器中发现了多个相同类型的组件,那么就按照 属性名称来进行装配
    @Autowired
    private TaoRenDao TaoRenDao;
    比如,我容器中有二个TaoRenDao类型的组件 一个叫TaoRenDao 一个叫TaoRenDao2
    那么我们通过@AutoWired 来修饰的属性名称时TaoRenDao,那么拿就加载容器的TaoRenDao组件,若属性名称为
    tulignDao2 那么他就加载的时TaoRenDao2组件
  • b:假设我们需要指定特定的组件来进行装配,我们可以通过使用@Qualifier(“TaoRenDao”)来指定装配的组件
    或者在配置类上的@Bean加上@Primary注解
@Autowired
@Qualifier("TaoRenDao")
private TaoRenDao TaoRenDao2;
  • c:假设我们容器中即没有TaoRenDao 和TaoRenDao2,那么在装配的时候就会抛出异常
    No qualifying bean of type ‘com.TaoRen.testautowired.TaoRenDao’ available
    若我们想不抛异常 ,我们需要指定 required为false的时候可以了
@Autowired(required = false)
@Qualifier("TaoRenDao")
private TaoRenDao TaoRenDao2;
  • d:@Resource(JSR250规范)
    功能和@AutoWired的功能差不多一样,但是不支持@Primary 和@Qualifier的支持
  • e:@InJect(JSR330规范)
    需要导入jar包依赖功能和支持@Primary功能 ,但是没有Require=false的功能
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

f:使用autowired 可以标注在方法上标注在set方法上

//@Autowired
public void setTaoRenLog(TaoRenLog TaoRenLog) {
this.TaoRenLog = TaoRenLog;
}

标注在构造方法上

@Autowired
public TaoRenAspect(TaoRenLog TaoRenLog) {
this.TaoRenLog = TaoRenLog;
}

标注在配置类上的入参中(可以不写)

@Bean
public TaoRenAspect TaoRenAspect(@Autowired TaoRenLog TaoRenLog) {
TaoRenAspect TaoRenAspect = new TaoRenAspect(TaoRenLog);
return TaoRenAspect;
}

九、自定义组件

我们自己的组件时,需要使用spring ioc的底层组件的时候,比如 ApplicationContext等.我们可以通过实现XXXAware接口来实现

@Component
public class TaoRenCompent implements ApplicationContextAware,BeanNameAware {
private ApplicationContext applicationContext;
@Override
public void setBeanName(String name) {
System.out.println("current bean name is :【"+name+"】");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

十、@Profile注解指定环境

过@Profile注解来根据环境来激活标识不同的Bean@Profile标识在类上,那么只有当前环境匹配,整个配置类才会生效@Profile标识在Bean上 ,那么只有当前环境的Bean才会被激活没有标志为@Profile的bean 不管在什么环境都可以被激活.

@Configuration
@PropertySource(value = {"classpath:ds.properties"})
public class MainConfig implements EmbeddedValueResolverAware {
@Value("${ds.username}")
private String userName;
@Value("${ds.password}")
private String password;
private String jdbcUrl;
private String classDriver;
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.jdbcUrl = resolver.resolveStringValue("${ds.jdbcUrl}");
this.classDriver = resolver.resolveStringValue("${ds.classDriver}");
}
//标识为测试环境才会被装配
@Bean
@Profile(value = "test")
public DataSource testDs() {
return buliderDataSource(new DruidDataSource());
}
//标识开发环境才会被激活
@Bean
@Profile(value = "dev")
public DataSource devDs() {
return buliderDataSource(new DruidDataSource());
}
//标识生产环境才会被激活
@Bean
@Profile(value = "prod")
public DataSource prodDs() {
return buliderDataSource(new DruidDataSource());
}
private DataSource buliderDataSource(DruidDataSource dataSource) {
dataSource.setUsername(userName);
dataSource.setPassword(password);
dataSource.setDriverClassName(classDriver);
dataSource.setUrl(jdbcUrl);
return dataSource;
}
}

激活切换环境的方法

  • 方法一:通过运行时jvm参数来切换 -Dspring.profiles.active=test|dev|prod
  • 方法二:通过代码的方式来激活
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("test","dev");
ctx.register(MainConfig.class);
ctx.refresh();
printBeanName(ctx);
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陶人超有料

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

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

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

打赏作者

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

抵扣说明:

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

余额充值