What in the world are Spring beans?

前端 未结 12 1446
谎友^
谎友^ 2020-11-28 00:19

I am yet to find a high-level definition of Spring beans that I can understand. I see them referenced often in Grails documentation and books, but I think that understanding

相关标签:
12条回答
  • 2020-11-28 00:33

    The XML configuration of Spring is composed of Beans and Beans are basically classes. They're just POJOs that we use inside of our ApplicationContext. Defining Beans can be thought of as replacing the keyword new. So wherever you are using the keyword new in your application something like:

    MyRepository myRepository =new MyRepository ();
    

    Where you're using that keyword new that's somewhere you can look at removing that configuration and placing it into an XML file. So we will code like this:

    <bean name="myRepository " 
          class="com.demo.repository.MyRepository " />
    

    Now we can simply use Setter Injection/ Constructor Injection. I'm using Setter Injection.

    public class MyServiceImpl implements MyService {
        private MyRepository myRepository;
        public void setMyRepository(MyRepository myRepository)
            {
        this.myRepository = myRepository ;
            }
    public List<Customer> findAll() {
            return myRepository.findAll();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 00:35

    In Spring, those objects that form the backbone of your application and that are managed by the Spring IoC container are referred to as beans. A bean is simply an object that is instantiated, assembled and otherwise managed by a Spring IoC container;

    0 讨论(0)
  • 2020-11-28 00:36

    Well you understood it partially. You have to tailor the beans according to your need and inform Spring container to manage it when required, by using a methodology populalrly known as IoC (Inversion of Control) coined by Martin Fowler, also known as Dependency Injection (DI).

    You wire the beans in a way, so that you do not have to take care of the instantiating or evaluate any dependency on the bean. This is popularly known as Hollywood Principle.

    Google is the best tool to explore more on this in addition to the links you would get flooded with here in this question. :)

    0 讨论(0)
  • 2020-11-28 00:36

    A Bean is a POJO(Plain Old Java Object), which is managed by the spring container.

    Spring containers create only one instance of the bean by default. 
This bean it is cached in memory so all requests for the bean will return a shared reference to the same bean.

    The @Bean annotation returns an object that spring registers as a bean in application context.
The logic inside the method is responsible for creating the instance.

    When do we use @Bean annotation?

    When automatic configuration is not an option. For example when we want to wire components from a third party library, because the source code is not available so we cannot annotate the classes with @Component.

    A Real time scenario could be that someone wants to connect to Amazon S3 bucket. Because the source is not available he would have to create a @bean.

    @Bean
    public AmazonS3 awsS3Client() {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsKeyId, accessKey);
        return AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(region))
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
    }
    

    Source for the code above -> https://www.devglan.com/spring-mvc/aws-s3-java

    Because I mentioned @Component Annotation above.

    @Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and class path scanning.

    Component annotation registers the class as a single bean.

    0 讨论(0)
  • 2020-11-28 00:41

    For Spring, all objects are beans! The fundamental step in the Spring Framework is to define your objects as beans. Beans are nothing but object instances that would be created by the spring framework by looking at their class definitions. These definitions basically form the configuration metadata. The framework then creates a plan for which objects need to be instantiated, which dependencies need to be set and injected, the scope of the newly created instance, etc., based on this configuration metadata.

    The metadata can be supplied in a simple XML file, just like in the first chapter. Alternatively, one could provide the metadata as Annotation or Java Configuration.

    Book: Just Spring

    0 讨论(0)
  • 2020-11-28 00:42

    The objects that form the backbone of your application and that are managed by the Spring IoC* container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions.

    More to learn about beans and scope from SpringSource:

    When you create a bean definition what you are actually creating is a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, just like a class, you can potentially have many object instances created from a single recipe.

    You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition, but also the scope of the objects created from a particular bean definition. This approach is very powerful and gives you the flexibility to choose the scope of the objects you create through configuration instead of having to 'bake in' the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes

    *IoC: Inversion of Control

    0 讨论(0)
提交回复
热议问题