Spring BeanDefintion的注册方式


一、扫包方式

  1. 扫描指定package下所有class文件中带有注解@Component、@Service、@Repository、@Controller、@Configuration、@ManagedBean、@Named,其中前五个由Spring提供,第六个由JSR-250提供,第七个由JSR-330提供

示例:

@Component
public class Bean1{

}

@Service
public class Bean2{

}

@Repository
public class Bean3{

}

@Controller
public class Bean4{

}

@Configuration
public class Bean5{

}

@ManagedBean
public class Bean6{

}

@Named
public class Bean7{

}


  1. Bean的名字:默认情况bean名字为类名首字母小写,如bean1、bean2、bean3…
  2. BeanDefintion的实现:ScannedGenericBeanDefinition,默认情况会立刻将带有1中注解的class生成ScannedGenericBeanDefinition加载到IOC容器,但是如果class上存在条件注解则不会在此刻加入
  3. 如果一个class上存在的是@Configuration注解或者存在@Component、@ComponentScan、@Import、@ImportResource注解或存在@Bean注解的方法时,则后续会将此class作为一个配置类继续解析

二、Import方式

触发条件:当解析的bean上存在Import注解
示例:

@Import(Config.class)
public class SimpleStart {

}
  1. 当导入的class实现了ImportSelector接口则将selectImports返回className作为配置类继续解析
  2. 当导入的class实现了ImportBeanDefinitionRegistrar接口则将ImportBeanDefinitionRegistrar加入到配置类,后续在ConfigurationClassBeanDefinitionReader中统一加载成BeanDefintion(对应实现:GenericBeanDefinition
  3. 将导入的class作为配置类解析,后续在ConfigurationClassBeanDefinitionReader中统一加载成BeanDefintion(对应实现:AnnotatedGenericBeanDefinition
  4. Bean的名字:默认情况bean名字为类的全限定名,如cn.hiboot.framework.research.spring.Config

三、ImportResource方式

触发条件:当解析的bean上存在ImportResource注解
示例:

@ImportResource("classpath:spring-config.xml")
public class Config {
 
}

spring-config.xm

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
	    http://www.springframework.org/schema/tx
	    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
		
	<context:property-placeholder location="classpath:db.properties" />

	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
		<property name="driverClassName" value="${db.driver}" />
		<property name="jdbcUrl" value="${db.url}" />
		<property name="username" value="${db.user}" />
		<property name="password" value="${db.password}" />
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"/>

</beans>
  1. 将ImportResource中的资源添加到配置类中,后续在ConfigurationClassBeanDefinitionReader中统一加载成BeanDefintion(对应实现:GenericBeanDefinition

四、Bean方法方式

触发条件:当解析的bean上存在@Bean注解的方法时

示例:

public class Config {
    @Bean
    ExampleBean exampleBean(){
        return new ExampleBean();
    }
}
  1. 将所有标有@Bean注解的方法加到配置类中,后续在ConfigurationClassBeanDefinitionReader中统一加载成BeanDefintion(对应实现:ConfigurationClassBeanDefinition
  2. static修饰的@Bean注解的方法与非static的区别

如果是static修饰的,则不需要在ExampleBean 例化时去实例化Config ,反之需要。

下面的代码就需要static修饰,否则会发生循环引用

public class Config {
  Config(ExampleBean exampleBean){

   }
   @Bean
   static ExampleBean exampleBean(){
       return new ExampleBean();
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kse_music

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

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

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

打赏作者

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

抵扣说明:

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

余额充值