springboot相关注解

springboot注解

@Configuration

@Configuration :Spring的配置标签,标记改类是Spring的配置类
Spring的配置类 相当于是:applicationContext.xml

 @Configuration
public class ApplicationConfig {
   
}
ComponentScan&ComponentScans自动扫描

ComponentScan是ioc组件自动扫描,相当于是 context:component-scan base-package=*

默认扫描当前包,及其子包 ;

ComponentScan.lazyInit :懒初始化

ComponentScan.excludeFilters :排除

@Component
public class MyBean {
   
}

@Configuration
//@ComponentScans
@ComponentScan("包名")
public class ApplicationConfig {
   
}
@Bean

Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理
bean的id: 方法名 myBean
bean的name:可以通过 @Bean标签的name属性指定
生命周期方法:initMethod , destroyMethod
单利多利: @Scope(“prototype”)

@Configuration
public class ApplicationConfig {
   
    @Bean
    public MyBean myBean(){
   
        return new MyBean();
    }

}

 @Bean(name="" , initMethod ="" ,destroyMethod="" )
 //@Scope("prototype")
 //@Lazy
 public MyBean myBean(){
   
 	return new MyBean();
 }

依赖注入
1.手动定义bean的方式
直接通过调方法的方式注入bean,或者通过参数注入bean

2.自动扫描定义bean的方式
使用 @Autowired 注入

3.通过工厂定义bean
3.1.定义FactoryBean

public class MyBeanFactoryBean implements FactoryBean<MyBean> {
   
    public MyBean getObject() throws Exception {
   
        return new MyBean();
    }

    public Class<?> getObjectType() {
   <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值