Spring Essays (06)

Source: Internet
Author: User

1. Setter injection of simple type and compound type

Di:dependency Injection (Dependency injection)

The literal analysis:

IoC: Refers to the creation of objects, reversed to the spring container

DI: Refers to the process by which the spring container creates an object, and the object dependency property is injected through the configuration

They are, in fact, different angles of the same concept. Di, in contrast to the IOC, explicitly describes the "injected object relies on the IOC container configuration dependent object".

  

1) How to inject the value:

    Mode 1: Attribute injection, through setter method;

Mode 2: Constructor injection through the constructor;

2) injected in the way:

Manual assembly:

Based on XML:

Based on annotations:

Automatic assembly: unsafe, not recommended

3) What types of values are injected:

① Simple value types: basic type +string+url, etc.

② composite type (object type):

③ collection type (understood, used within the framework)

Class employee{    private String name;    Private private private // omit Getter, setter method}       

<!--① Simple value type: basic type +string+url etc.--
<BeanId="EmployeeClass="Employee Fully qualified name "><PropertyName= "Name"Value= "John"/><PropertyName= "Age"Value= "20"/><PropertyName= "Salary"Value= "5000"/><PropertyName= "url"Value= "Http://www.baidu.com"/></Bean>

<!--② Composite Type--
<BeanId="EmployeeDAO "Class="EmployeeDAO fully qualified name "/><BeanId="EmployeeService "Class="EmployeeService Fully qualified name "><PropertyName="EmployeeDAO "Ref="EmployeeDAO " /> </Bean>

Class employeeservice{    private EmployeeDAO EmployeeDAO;         void Setemployeedao (EmployeeDAO employeedao) {EmployeeDAO =void Save () { Employeedao.save ();
     System.out.println ("Service.save ()")}}

Class employeeedao{    void Save () {System.out.println ("Dao.save ()")}}   

Sometimes, if you want only one bean to reference another bean, similar to an anonymous inner class

<bean id=" EmployeeService " Class=" EmployeeDAO fully qualified name "> < property name= " EmployeeDAO "> < Bean class= "EmployeeDAO fully qualified name" /> </property></bean>              

Spring Essays (06)

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.