1, Writing process
First create a web project using IDEA
1. Download the latest Spring development package (jar package)
2. Copy the jar package to the project project (under lib)
3. Write the core configuration file, for example (applicationContext.xml)
4. Read the Spring configuration file in the program, obtain the Bean through the Spring framework, and complete the corresponding operations
2, Import jar package
3, Write target class
1. First, create the interface and its implementation class, such as UserDao and UserDaoImpl
2. Get the instance in the UserDao implementation class
2.1 in the previous development, we just need to directly new an object. Namely: UserDao userDao = new UserDaoImpl();
2.2 after learning spring, spring will create an instance of the object – > that is, when an instance object is required after IoC inversion of control, it can be obtained from the Spring Factory (container), and the fully qualified name (path name) of the implementation class needs to be configured into the xml file
UserDao.java
package com.chaz.dao; public interface UserDao { void save(); }
UserDaoImpl.java
package com.chaz.dao.impl; import com.chaz.dao.UserDao; /** * Original coding method */ public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("The original coding method..."); } }
4, Create and write Spring configuration file: XXX.xml
Location: any, can be leveled with src (need to be created additionally), or under src
Name: arbitrary, generally applicationContext.xml
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- IOC Introduction to configuration <bean> Configure the objects to be created id : Unique, non repeatable, for subsequent Spring The name: It's the only one, but it's not usually used class : The fully qualified class name of the instance to be created --> <bean id="userDao" class="com.chaz.dao.impl.UserDaoImpl2"></bean> </beans>
Five. Test
SpringTest.java
package com.chaz.test; import com.chaz.dao.UserDao; import com.chaz.dao.impl.UserDaoImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; /** * Test class 1 * IOC Inversion of control */ public class SpringTest { /** * Traditional writing */ @Test public void test01(){ UserDao userDao = new UserDaoImpl(); userDao.save(); } /** * Spring Frame writing */ @Test public void test02(){ //Get container ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get content from container UserDao bean = (UserDao) applicationContext.getBean("userDao"); bean.save(); } }
The concept of IoC (Inverse of Control) is to manually create the control right of UserDaoImpl object in the program, which is managed by Spring framework. In short, the control to create the UserServiceImpl object is reversed to the Spring framework.