Vernacular to explain the Spring of @bean comment

1, Spring annotation classification

Broadly Spring annotation can be divided into two categories:

Annotations are used to register for a class Bean

If the IOC container is an empty room, first of all this empty room and consequently did not, we have to eat a big meal, we have to come from external handling food and utensils. Here the ingredients of a different or the same utensils to move into an empty house operation of each annotation is equivalent to similar effect registration of Bean. Notes the role of registered Bean is put to the IOC container (registered) thing!
Bean annotation for registration: for example @Component, @Repository, @ Controller, @Service , @Configration these annotations is used to register Bean, into the IOC container, a decoupling convenient to manage to spring, and secondly also secondary use, what is the secondary use it? Secondary use here can be understood as: before you start into the air from outside the house carrying food and utensils when handling disposable pork, lamb, scoop, chopsticks four things, this time you will begin to eat a big meal, first eat when you must use chopsticks or scoop, not to mention you grasping, as long as you need, you will find, this time find that you have chopsticks or scoop into the house, you do not have to go outside chopsticks into the house, meaning that the IOC container that already exist, as long as you can take it with, without having to go register! The operating room has to take something to talk about is the following for use Bean comments!

A type annotation is intended for use Bean

Bean annotation for use: for example @Autowired, @Resource notes, these notes is to house the things they used to use, if you bring it, the premise is certainly room (IOC) in there, otherwise it will error, such as you have to do a beef platter of raw materials needed five cows do the job, you are now only four cows pot, this time you know that he lived in the house to five cows, this time directly to the house of the cow directly into the pot, to complete the assembly of beef platter. These notes are what need is what you want, as long as there is a container aboard the vessel to take! These notes have their own differences, such as @Autowired used in the chopsticks, which you might just want to use wooden chopsticks, perhaps just want to use iron, @ Autowired role in the chopsticks on what attributes What chopsticks, and @ Resource Angus beef if used in the above, specify the name is Angus beef beef.

2, @ Bean annotated overview

This article is mainly about @Bean annotation, this annotation belongs annotation is used to register the Bean.

Section taken from the following passage in Spring Why should @Bean comment?

Spring's @Bean annotation is used to tell the method to produce a Bean object, and the object is to Spring Bean management. This method produces Spring Bean object only called once, then this will be the Spring Bean object in its own IOC container. @Bean clearly indicates a way, what method do? Method of producing a bean, and to the Spring container management; we will understand why this is @Bean comment on the method, because it is very clear to tell annotated method, you give me generate a Bean, then to the Spring container, and the rest of you do not control. Remember, @ Bean will be placed on the method is to let the method to generate a Bean, and then to the Spring container.

The following can make accountDao method produces a AccountDao object, then the object is to AccountDao Spring Management

 class A{
        @Bean
        public AccountDao accountDao(){
            return new AccountDao();
        }
    }

In fact, the role of @ Bean annotations and xml configuration bean tag is the same.

3. Why should @Bean comment?

I do not know if you have not thought about for registered notes there are so Bean's more, why should appear @Bean comment?

The reason is simple: similar @Component, @Repository, @ Controller, these limitations @Service registered Bean notes, only for a limited role in their own writing class if the jar package is a third-party library to join the IOC containers, these Notes to physically very weak, yes, @ Bean annotation can do it! Of course, in addition to @Bean annotations can do as well as third party can also @Import class instance library management to the spring, and @Import faster and more convenient, but not in the notes @Import Benpian range, there is no longer Overview .

Another benefit is the ability to use dynamic annotation @Bean get a Bean object, can get different Bean different objects depending on the environment.

4, @ Bean annotated summary

1, Spring annotation is used to tell the @Bean method, resulting in a Bean object, and the object is to Spring Bean management. This method produces Spring Bean object only called once, then this will be the Spring Bean object in its own IOC container.

2, @ Component, @Repository, @ Controller, @Service write these notes is limited to their own classes, while the third party can comment @Bean class instance library is added to the spring IOC container and management.

3. Another benefit is the ability to annotate @ Bean obtain a dynamic objects Bean, Bean can be different to get different objects depending on the environment.

4 ,, remember, @ Bean will be placed on the method is to let the method to generate a Bean, and then to the Spring container, and the rest of you do not control.

Of course, this is not talked about specific details @Bean annotation, simply describes a rough, if other requirements are free to Google ....

end.....

If this article there is a little bit of help to you, then please point a chant praise, thank you ~

Finally, if there is insufficient or is not correct, please correct me criticism, grateful! If you have questions please leave a message, the absolute first time to reply!

I welcome you to focus on the public number, there are some java learning materials and a large wave of java e-books, such as Zhou Zhiming teacher depth java virtual machine, java programming ideas, the core technology volume, Westward design patterns, java concurrent programming combat ... .. is a java Bible, do not say fast car on Tomcat, ye who go! The main thing is to explore technology, yearning technology, the pursuit of technology, said good pots Friends is coming Oh ...

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yichunguo/p/12110761.html