IoC (Inversion if Control) - Control inversion is one of Spring's two core technologies. IoC is generally divided into two types: Dependency Injection (DI) and Dependency Lookup
Example of usage:
1. Create a new project and import Spring-related jar packages.
2. Create a new data access layer and business logic layer
Code structure:
Code example:
/** * Entity Bean * @author BC * */public class User {private Integer id;private String userName;private String password;//get set method omitted}/** * Data access layer interface* @author BC * */public interface UserDaoInterface {/** Query all user information*/public List<User> getUserList();}/** * Data access layer implementation class* @author BC * */public class UserDaoImpl implements UserDaoInterface {/**Simulate database data*/private List<User> userList;public UserDaoImpl() {userList = new ArrayList<User>();User u = new User(1, "Zhang San", "123");userList.add(u);u = new User(2, "Li Si", "456");userList.add(u);u = new User(3, "Wang Wu", "789");userList.add(u);u = new User(4, "Zhao Liu", "233");userList.add(u);}@Override public List<User> getUserList() {return userList;}}/** * Business logic layer interface* @author BC * */public interface UserBizInterface {/**Query all user information*/public List<User> getUserList();}/** * Business logic layer implementation class* @author BC * */public class UserBizImpl implements UserBizInterface {/**Use Spring injection*/private UserDaoInterface userDao;@Override public List<User> getUserList() {return userDao.getUserList();}/**Injected through the set method, so the properties that need to be injected must be set to the set method*/public void setUserDao(UserDaoInterface userDao) {this.userDao = userDao;}public UserDaoInterface getUserDao() {return userDao;}}3. Write the applicationContext.xml configuration file
Header information:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
Configuration code:
<!-- Data access layer object: UserDao --> <bean id="userDao"></bean> <!-- Business logic layer object: UserBiz --> <bean id="userBiz"> <!-- Inject data access layer properties through the set method--> <property name="userDao" ref="userDao"/> </bean>
Test code:
public class UserBizTest {private ApplicationContext ctx;@Before public void load() {//Read applicationContext.xml configuration file ctx = new ClassPathXmlApplicationContext("applicationContext.xml");}@Test public void getUserListTest() {//Create a business logic layer object UserBizInterface userDao = (UserBizInterface) ctx.getBean("userBiz");//Calling the method to get user information List<User> userList = userDao.getUserList();//Transfer the collection for (User user : userList) {System.out.println(user.getId() + "|" + user.getUserName() + "|" +user.getPassWord());}}}In the above example code, we use set method injection. There are many types of Spring injection methods, and there are many types of injected attribute types. Please refer to the details:
A brief discussion on the injection method of spring ioc and the injection of different data types
About the scope of beans
scope="singleton" default, indicates that there is only one shared Bean instance in the Spring container
scope="prototype" Get a new instance from the container each time
scope="request" Every Http request will create a new Bean instance
scope="session" Share a Bean instance with the same Http request
scope="global session" Share a Bean instance with the same global session
Summarize
The above is the entire content of this article about the simple example of spring ioc and the scope attribute analysis of beans. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!