Spring Boot @Bean 参考和总结

spring boot 学习(三) — 依赖注入 @Bean
https://blog.csdn.net/qq_34677587/article/details/68116054

@Configuration 申明这是一个配置类相当于xml配置文件,@Bean表示这是一个Spring管理的bean

[springBoot系列]–springBoot注解大全
https://www.cnblogs.com/tanwei81/p/6814022.html

@Bean:用@Bean标注方法等价于XML中配置的bean。
@Bean:相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。

StackOverflow 一个回答如下
https://stackoverflow.com/questions/37218985/spring-boot-bean-definition
BEAN

Spring的@Bean注解用于告诉方法产生一个Bean对象,然后这个Bean对象交给Spring容器管理,产生Bean对象的方法Spring只会调用一次,调用之后Spring会将这个Bean放入到自己的IOC容器中。
使用@Bean注解的好处是能够动态的获取一个Bean对象,能够根据环境不同获取不同的Bean对象,或者是将其它的组件交给Spring容器管理。
https://blog.csdn.net/yaomingyang/article/details/84347822

1.当项目中的类是自己编写的,则一般使用@controller、@service、@component等注解直接把bean交给spring管理。
2.当我们需要引入第三方库,并且也需要把第三方库中的类实例交给spring管理时,则使用@Bean、@Configuration注解。
原文:https://blog.csdn.net/czhongheng/article/details/83086174

What is a Spring Bean?
https://www.baeldung.com/spring-bean
Chapter 4. Creating and using bean definitions
https://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/creating-bean-definitions.html
Authoring @Configuration classes
https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html
Beans and Dependency Injection
https://www.tutorialspoint.com/spring_boot/spring_boot_beans_and_dependency_injection.htm
这个tuorial 讲述了@Component @Autowired @Bean @ComponentScan

Playing Around With Spring Bean Configuration
https://dzone.com/articles/playing-sround-with-spring-bean-configuration

案例
Building an Application with Spring Boot
https://spring.io/guides/gs/spring-boot/

Spring | Autowire | Dependency Injection | Spring Boot
https://youtu.be/K43qyHJXmWI
Bean就是要用到的类
@Component 告诉Spring 容器把这个对象创建为一个Spring Bean,然后就可以在任何一个地方用到它。如果要需要用多个实例,需要加@Scope(value = “prototype”)
Spring 容器将 @Component 创建为Bean之后,各个Bean是彼此独立的。由@Autowired来指明依赖关系。
object-here-is-dependent
search-for-it
@Autowired 告诉创建对象是怎样去找到其他对象。
然后,要用到Bean的时候,从ConfigurableApplicationContext中用configurableApplicationContext.getBean()获取到即可。

//***Application.java
@SpringBootApplication
public class DemobeanApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext configurableApplicationContext =
                SpringApplication.run(DemobeanApplication.class, args);

        Student student = configurableApplicationContext.getBean(Student.class);
        student.show();
//Student 加了@Scope(value="prototype")之后就会创建两个对象
//        Student student1 = configurableApplicationContext.getBean(Student.class);
//        student1.show();
    }

}

要被创建的Student类如下

//Studeng.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
//@Scope(value = "prototype") //可以在每次getBean()的时候,创建一个Student实例
public class Student {
    private String name;
    @Autowired
    private Body body;//用@Autowired指明依赖了Body类,
    //让去找容器托管的托管的body类,并连接起来

    public Student() {
        System.out.println("student is created.");// 观察打印结果,确认对象创建
    }

    public void show(){
        System.out.println("show youself");
        body.move();//没有@Autowired将会报错 java.lang.NullPointerException
    }
}

Student 类依赖的Body如下:

import org.springframework.stereotype.Component;

@Component
public class Body {
    private String leg;

    public void move(){
        System.out.println("moveing");
    }

}

以上代码均省略了Getter and Setter。
完整代码示例:https://gitee.com/xingyongzhi/demobean

Telusko 的 Sprint Boot Tutorials 系列视频:https://www.youtube.com/playlist?list=PLsyeobzWxl7oA8QOlMtQsRT_I7Rx2hoX4

Spring Core Annotation | Configuration, Bean
https://youtu.be/5zUTc-kge8I

Spring Tutorial 03 - Understanding Spring Bean Factory
https://youtu.be/xlWwMSu5I70、

相关参考
Spring Boot常用注解(一) - 声明Bean的注解
https://blog.csdn.net/lipinganq/article/details/79155072

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值