Example Three entry --- Spring, Spring IOC inversion control relies on the first day of injection []

Note: This example is assigned by the initialization bean implantation applicationContext.xml. which is:

applicationContext.xml ---> bean-> Test Value

Inversion of Control ( Inversion of Control ) , also called Dependency InJection ( DI ) .

The basic concept of control inversion:

It does not directly create objects, but to create a description of their way. Use in engineering Bean when, by the Spring container is created Bean instance. Not directly connected with the object and the service code, but which is described in the configuration file which requires a service component.

 

A dependency injection:

Spring injection dependency injection, also known as [ DI ] where the purpose is to bean property assignment.

1, by Setter method (generally attribute assignment, i.e. basic types of assignment example).

(1)  , write JavaBean .

package test3.ioc;

 

public class User {

private String uname,ubirth;

private int id;

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}

public  String getUbirth () {

return ubirth;

}

public void setUbirth(String ubirth) {

this.ubirth = ubirth;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

}

 

(2), the initial value of the property in the injection profile.

 <! - configured via setter injection property of the initial value ->

 <bean id="user" class="test3.ioc.User">

   <property name="uname" value="zhangsan"></property>

   <property name="ubirth" value="2019-01-10"></property>

   <property name="id" value="123"></property>

 </bean>

(3)  test :

package test3;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import test3.ioc.User;

 

public class Test {

public static void main(String[] args) {

@SuppressWarnings("resource")

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

User user = (User)factory.getBean("user");

// obtain the initial value

System.out.println(user.getId());

}

}

 

Summary: By the above method, may extend generally injected Reflection object constructor injection, injection of the set of array type.

 

Guess you like

Origin www.cnblogs.com/ciscolee/p/10931254.html