The understanding of bean and @bean in spring

Source: Internet
Author: User

Beans are ubiquitous in spring and SPRINGMVC, it's important to put this concept into context, and here's what I think:

First, what is a bean?

1, Java object-oriented, the object has methods and properties, then you need an object instance to invoke methods and properties (i.e. instantiation);

2. All classes that have methods or attributes need to be instantiated to use these methods and attributes in order to be figurative;

3. Rule: all subclasses and classes with methods or attributes are added to the annotations of the registered bean to spring IOC ;

4, the bean is understood as the agent or spokesperson of the class (in fact, it is implemented by reflection, proxy), so that it can represent the class has the possession of the thing

5, we are on the Weibo @ over xxx, the other party will first see this message, and give you feedback, then in spring, you identify an @ symbol, then spring will come to see, and get a bean from here or give a bean

Second, the annotations are divided into two categories:

1, one kind is uses the bean, namely is already in the XML file to configure the bean to use, completes the property, the method assembly, for example @autowired, @Resource, May through Bytype (@Autowired), ByName (@Resource) way to get the bean;

2, a class is registered Bean, @Component, @Repository, @ Controller, @Service, @Configration These annotations are the objects you want to instantiate into a bean, placed in the IOC container, when you need to use , it will @autowired with the above, @Resource together, the object, properties, methods to assemble the perfect.

Iii. What is @Bean?

1. What is the principle? First look at the source of some of the content:

1234567891011121314 Indicates that a method produces a bean to be managed bythe Spring container.   <p>The names and semantics of the attributes to thisannotation are intentionally similar to those of the {@code <bean/>} element inthe Spring XML schema. For example: <pre class="code">     @Bean     publicMyBean myBean() {         // instantiate and configure MyBean obj         returnobj;    }</pre>

It means that @bean clearly indicates a method, what method--a method of generating a bean, and handing it over to the spring container management; From here we understand why @bean is on the annotation of the method, because it tells the annotated method very clearly, you give me a bean, And then hand it over to the spring container, and you leave the rest.

2, remember, @Bean put on the method, is to produce a bean, then you are not confused, because already in your definition of the class added @configration and other registered bean annotations, why also use @bean it? I do not know this, let me give an example, let's discuss it together:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 packagecom.edu.fruit;  //定义一个接口    publicinterface Fruit<T>{        //没有方法}/**定义两个子类*/packagecom.edu.fruit;     @Configuration     publicclassApple implementsFruit<Integer>{//将Apple类约束为Integer类型}packagecom.edu.fruit;     @Configuration     publicclassGinSeng implementsFruit<String>{//将GinSeng 类约束为String类型}/**业务逻辑类*/packagecom.edu.service;       @Configuration       publicclassFruitService {          @Autowired          privateApple apple;          @Autowired          privateGinSeng ginseng;    //定义一个产生Bean的方法       @Bean(name="getApple")       publicFruit<?> getApple(){       System.out.println(apple.getClass().getName().hashCode);         System.out.println(ginseng.getClass().getName().hashCode);       returnnew Apple();}}/**测试类*/@RunWith(BlockJUnit4ClassRunner.class)publicclassConfig {    publicConfig(){        super("classpath:spring-fruit.xml");    }    @Test    publicvoidtest(){        super.getBean("getApple");//这个Bean从哪来,从上面的@Bean下面的方法中来,返回                                                          的是一个Apple类实例对象            }}

The above example also confirms what I have summed up above:

1. All subclasses and classes with attributes and methods are registered beans to spring and administered by them;

2, @Bean used in the method, tell the spring container, you can get a Bean from the following method

The understanding of bean and @bean in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.