1. The concept of the IoC container
An IOC container is a container that has a dependency injection function, which is responsible for instantiating, locating, configuring, and establishing dependencies between objects in the application. The application does not need to be directly in the code to new related objects, but is assembled by the IoC container. Beanfactory is the actual representative of the IoC container in spring.
The Spring IoC container instantiates and assembles the various objects in use by reading the configuration file and configuring the metadata. Configuration metadata is typically used based on an XML configuration file, and spring is fully decoupled from the configuration file, and can be configured in other ways, such as annotations, Java file-based, property file-based configurations.
2. The Bean concept
Those objects managed by the IoC container, called Bean,bean, are objects that are initialized, assembled, and managed by the Spring container, except that there is no difference between beans and other objects in the application.
3. Let's build a project to demonstrate the basic use of spring IoC
(Environment JDK 1.7.0_79,eclipse and Spring 4.2.2 Release, note: This version of Spring only supports JDK 1.6+
(: http://repo.spring.io/release/org/springframework/spring/))
Use Eclipse to create a new Java project, named Spring_ioc, and import Spring's jar package.
User.java
public class User {private int userid;private string username;//username private string sex;//sex private date birthday;//date of birth private string address;//Address private string detail;//details private Float score;//score//getter () and Setter () ...}
Userservice.java
Public interface UserService {public void AddUser (user user);
Userserviceimpl.java
public class Userserviceimpl implements UserService {private Userdao userdao;public Userdao Getuserdao () {return userdao; }public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} @Overridepublic void AddUser (user user) {this.userDAO.saveUser (user);}}
Userdao.java
Public interface Userdao {public void Saveuser (user user);
Userdaoimpl.java
public class Userdaoimpl implements Userdao {@Overridepublic void Saveuser (user user) {System.out.println ("Save user to Da Tabase ");}}
Userservicetest.java (Junit test case)
public class Userservicetest {@Beforepublic void SetUp () throws Exception {} @Testpublic void Testadduser () throws Exceptio n {applicationcontext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Userserviceimpl service = (Userserviceimpl) ctx.getbean ("UserService"); User user = new user (); User.setusername ("Olive"); Service.adduser (user);}}
Beans.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id=" Userdao "class=" Com.dao.impl.UserDAOImpl "/> <bean id=" UserService "class=" Com.service.impl.UserServiceImpl "> <prope Rty name= "Userdao" ref= "Userdao"/> </bean></beans>
4. Injection method
Ok,demo built up, here are some of the IoC container injection methods: interface injection, construction method, setter method injection ( the first two used less ).
(1) Construction method injection:
First, an explicit construction method is constructed in the dependent object (Userserviceimpl), and the construction method contains the dependent object (Userdaoimpl). Configure the Userdaoimpl and Userdaoimpl in the configuration file (Beans.xml) as follows:
To add a construction method to Userserviceimpl:
Constructorpublic Userserviceimpl (Userdao Userdao) {super (); This.userdao = Userdao;}
To modify a configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id= "Userdao" class= "Com.dao.impl.UserDAOImpl" /> <bean id= " UserService " class=" Com.service.impl.UserServiceImpl "> <!-- <property name= "Userdao" ref= "Userdao" /> --> <!-- constructor -- > <constructor-arg> <ref bEan= "Userdao"/> </constructor-arg> </bean></beans>
(2) Setter method injection:
Requires that the setter () method of the dependent object (Userdaoimpl) be provided in the dependent object ( Userserviceimpl ), then in the configuration file <bean><property/ The ></bean> property is configured with dependent objects.
public class Userserviceimpl implements UserService {private Userdao userdao;public Userdao Getuserdao () {return userdao; }public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} Constructor//public Userserviceimpl (Userdao Userdao) {//super ();//this.userdao = userdao;//} @Overridepublic void AddUser (user user) {this.userDAO.saveUser (user);}}
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id= "Userdao" class= "Com.dao.impl.UserDAOImpl" /> <bean id= " UserService " class=" Com.service.impl.UserServiceImpl "> <property name= "Userdao" ref= "Userdao" /> <!-- constructor --> <!-- <constructor-arg> <ref bean= "Userdao"/> </constructor-arg> --> </bean></beans>
(3) interface injection: because the author himself is not too clear, first temporarily empty, and later to find out after the supplement
Brief analysis of Spring IoC-injection method