Getting spring (Inversion of Control and the Dependency Injection)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

 

We must first understand what is the inversion of control and dependency injection.

Contrast concept:

                                        Inversion of Control                       DI (dependency injection) dependency injection

meaning:

   The principle in this way converted into new objects, spring way to create objects through reflection.

spring to create a finished object into a container, who needs who gave injected into it.

Simply put: the right to create objects and managed objects to the spring

Meaning: is to set the value of the property to the object.

Property is set to the value of the original object: set value method to set the target to the object constructor initializes the time setting.

Creating objects

 

Set the object attribute value

 

Inversion of Control:

 

1. Create an object in several ways?

 Constructor
 static factory
 instance factory

2.spring create objects range

     Problem: every acquisition target time, spring is to create a new object or give us always return the same object?

              Singleton: only one object in memory, each time the address is acquired as the value of the object.

             Many cases of: Memory Each object is a new object, their address values ​​are different.

   Answer: The object created by default under the spring are a single case (the object each time returned are the same).

3.spring create an object life cycle

When an object is created to create spring, when to run, when to be destroyed.?

                  1. Create and configuration file loading time

                  2. Run, when to use the object

                  3. Destruction, spring, when the container is closed

Dependency Injection:

1. Fill the ways dependent?

attribute set provided
configured injection mode
p c namespace namespace injection and injection
complex data type injection

Common notes:

 

Notes name effect

@Component 

@Component("abc")

The default id is the class name first letter lowercase

Manually specified <bean id = "abc" class = "xxxx">

@Controller Determining a presentation layer / control layer using the Controller
@Service Business logic using Service
@Repository Using the Repository persistence layer dao

 @Autowired

Automatic annotation

@Qualifier("id")   1 :( bean id injection manner according to the specified values)
@Value("#{son1}") Injection method 2:
 @Resource(name="son1") Injection method 3: jdk injection

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/longyanchen/article/details/94409961