First, easy to get started case
Introductory Case: IoC
1. Project creation and structure
2. Interfaces and Implementation classes
User.java interface
Package COM.JD.IOC; /* */Publicinterface User {void addUser ();}
Userimpl.java Implementation Class
Package Com.jd.ioc.impl;import Com.jd.ioc.User; /* */Publicclass Userimpl implements User { @Overridepublic void AddUser () { System. out. println ("add user! " ); }}
XML configuration file
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/beanshttp//www.springframework.org/schema/beans/spring-beans.xsd "><!--Configure Service<bean>Configure the object ID that you want to create: used to later obtain an instance from the spring containerclass: Required to create the fully qualified class name of the instance-<bean id="Userserviceid" class="Com.jd.ioc.impl.UserImpl"></bean></beans>
Test class
Usertest.java
Package Com.jd.test;import Com.jd.ioc.user;import org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author Weihu * @date 2018/8/8/008 22:33 * @desc IOC test class*/ Public classusertest {@Test Public voidTestUser () {//1. Load the configuration fileString xmlpath="Com/jd/xml/beans.xml"; ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext (Xmlpath); //get Bean object by IDUser user = (user) Applicationcontext.getbean ("Userserviceid"); User.adduser (); }}
1.Spring Framework Getting Started case