The role of the 1.spring IOC is to instantiate an object in a way that loads a configuration file instead of the new object in the code.
2.IOC full Name: Inversion of control, Chinese meaning: controlling inversion
3. There are two ways of dependency injection:
- Set value injection by adding a set method and adding a property tag in the bean of the configuration file (property referencing another bean)
- Construct injection-"by constructing the method and adding the Constructor-arg tag in the bean of the configuration file
Example Project structure:
The following are the file codes:
Injectdao.java Interface
Package Com.dingli.dao;public interface Injectdao {public void Save (String args);
Injectdaoimpl.java Implementation Class
Package Com.dingli.dao;public class Injectdaoimpl implements injectdao{@Overridepublic void Save (String args) { SYSTEM.OUT.PRINTLN ("Save data," +args);}}
Injectservice.java interface
Package Com.dingli.service;public interface Injectservice {public void Save (String args);
Injectserviceimpl.java Implementation Class
Package Com.dingli.service;import Com.dingli.dao.injectdao;public class Injectserviceimpl implements injectservice{ Private Injectdao injectdao;public Injectserviceimpl (Injectdao injectdao) {This.injectdao = Injectdao;} @Overridepublic void Save (String args) {System.out.println ("Service received data," +args); args + = "." +this.hashcode (); Injectdao.save (args);} public void Setinjectdao (Injectdao injectdao) {This.injectdao = Injectdao;}}
Basetest.java JUnit Test base class
Package Test;import Org.apache.commons.lang.stringutils;import Org.junit.after;import org.junit.Before;import Org.springframework.beans.beansexception;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Basetest {private Classpathxmlapplicationcontext context;private string Springxmlpath;public basetest () {}public BaseTest (string Springxmlpath) {This.springxmlpath = Springxmlpath;} @Beforepublic void before () {if (Stringutils.isempty (Springxmlpath)) {Springxmlpath = "classpath*:spring-*.xml";} try {context = new Classpathxmlapplicationcontext (Springxmlpath.split ("[, \\s]+")); Context.start ();} catch ( Beansexception e) {e.printstacktrace ();}} @Afterpublic void After () {Context.destroy ();} @SuppressWarnings ("Unchecked") protected <t extends object> T getbean (String beanid) {try {return (T) Context.getbean (Beanid);} catch (Beansexception e) {e.printstacktrace (); return null;}} Protected <t extends object> T getbean (class<t> clazz) {try {return CoNtext.getbean (clazz);} catch (Beansexception e) {e.printstacktrace (); return null;}}}
Testdemo.java JUnit Test Example
Package Test;import Org.junit.test;import Org.junit.runner.runwith;import Org.junit.runners.BlockJUnit4ClassRunner ; Import Com.dingli.service.InjectService; @RunWith (blockjunit4classrunner.class) public class Testdemo extends Basetest{public Testdemo () {Super ("Classpath*:spring-ioc.xml");} @Test //test setpoint injected//public void Testsetter () {//injectservice service = Super.getbean ("Injectservice");// Service.save ("This is the data to be saved");/} @Test//test construct injected public void Testcons () {Injectservice service = Super.getbean (" Injectservice "); Service.save (" This is the data to be saved ");}}
Spring-ioc.xml Spring 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://ww W.springframework.org/schema/beans/spring-beans.xsd "> <!--<bean id=" Injectservice "class=" Com.dingli.service.InjectServiceImpl "> <property name=" Injectdao "ref=" Injectdao "> </property> </ Bean>--<bean id= "Injectservice" class= "Com.dingli.service.InjectServiceImpl" > <constru Ctor-arg name= "Injectdao" ref= "Injectdao" ></constructor-arg> </bean> <bean id= "Injectdao" cl ass= "Com.dingli.dao.InjectDaoImpl" ></bean> </beans>
The source of the study content, the school network spring Introduction
This code can be downloaded by clicking here
Spring Starter-Learning notes