Spring entry-Study Notes
1. spring IOC is used to instantiate an object by loading the configuration file instead of the new object in the code.
2. IOC Full name: Inversion of Control
3. There are two ways to inject dependencies:
Set Value injection-> inject constructor injection by adding the set Method and adding the property tag (property references another bean) to the bean in the configuration file-> through constructor, add the constructor-arg label to the bean in the configuration file to inject the example project structure:
The code for each file is as follows:
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 ("data received by the service->" + 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 getBean(String beanId) {try {return (T)context.getBean(beanId);} catch (BeansException e) {e.printStackTrace();return null;}}protected
T getBean(Class
clazz) {try {return context.getBean(clazz);} catch (BeansException e) {e.printStackTrace();return null;}}}
TestDemo. java Junit test instance
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 Set Value injection // public void testSetter () {// InjectService service = super. getBean ("injectService"); // service. save ("this is the data to be saved"); //} @ Test // Test Structure injection public void testCons () {InjectService service = super. getBean ("injectService"); service. save ("this is the data to be saved ");}}
Spring-ioc.xml spring configuration file
The source of the learning content, MOOC spring entry
You can click here to download the code for this article.