Spring core: Bean factory assembly 1

Source: Internet
Author: User

This article mainly includes Bean Factory knowledge in spring. It mainly includes the following topics:

1. Bean factory at the beginning of getstart

2. Bean basic definition

3. Create a bean using the constructor

4. Create a bean using the static factory Method

5. Create a bean using a non-static factory Method

6. Bean identifier

7. When to use Singleton

8. Set bean attributes and collaborators

9. Bean constructor selection decision

10. Automatic Assembly

11. Dependency check

12. Use the value element to set attributes

13. Use null to set a null value.

14. Use collection to set the set

15. Define internal beans

16. Pre-check error using idref Element

17. Use ref to set Dependencies

18. shorthand for value and ref

19. Method Injection

20. Bean lifecycle Interface

21. Let Bean know its identity

22. Parent bean and child Bean

23. Custom Bean Factory

The content above is quite large and may be explained in several parts. This articleArticleIncluding:

1. Bean factory at the beginning of getstart

2. Bean basic definition

1. Bean factory at the beginning of getstart

The most basic beanfactory interface in spring is org. springframework. Beans. Factory. beanfactory.CodeAs follows:

Public interface beanfactory {<br/> string factory_bean_prefix = "&"; <br/> Object getbean (string name) throws beansexception; <br/> <t> T getbean (string name, class <t> requiredtype) throws beansexception; <br/> <t> T getbean (class <t> requiredtype) throws beansexception; <br/> Object getbean (string name, object... ARGs) throws beansexception; <br /> Boolean containsbean (string name); <br/> Boolean issingleton (string name) throws nosuchbeandefinitionexception; <br/> Boolean isprototype (string name) throws nosuchbeandefinitionexception; <br/> Boolean istypematch (string name, class targettype) throws nosuchbeandefinitionexception; <br/> class <?> GetType (string name) throws nosuchbeandefinitionexception; <br/> string [] getaliases (string name); <br/>}

There are three common methods in this interface: getbean, issingleton, and getalians. beanfactory can be instantiated in the actual project as follows:

/** <Br/> * load the spring runtime environment <br/> */<br/> applicationcontext context = <br/> New classpathxmlapplicationcontext ("applicationcontext. xml ");

In most cases, the Customer Code does not need to directly instantiate applicationcontextd. The Spring framework is automatically executed behind the scenes and automatically loads applicationcontext when the web starts the service.

2. Bean basic definition

The basic definition of bean is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans <br/> xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: P = "http://www.springframework.org/schema/p" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <br/> </beans>

In myeclipse 8.5, you can add a bean as follows:

The following dialog box is displayed:

In the following articles, we will gradually explain how to fill in the options in the preceding dialog box.

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.