Preface
Before introducing the instantiation of beans, we first need to introduce what is a bean and how to configure beans.
If Spring is regarded as a large factory, then the bean in the Spring container is the product of that factory. To use Spring factory to produce and manage beans, you need to specify in the configuration file which beans are needed and how to assemble them together.
Spring container supports two formats of configuration files, namely Properties file format and xml file format. In actual development, the most commonly used amount is the xml file format. Therefore, in the following explanation, we will explain the configuration method of xml file format. The root element of an XML configuration file is <beans>, which can contain multiple child elements <bean>, each child element defines a bean and describes how the bean should be assembled into a Spring container. The properties in the <bean> element are as follows:
In a configuration file, usually an ordinary bean only needs to define two attributes: id and class. The way to define a bean is as follows:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- Configure the specified object to spring and let spring create its instance--> <bean id="userDao"/> <bean name="userDao1, userDao2"/></beans>
In the above code, two beans are defined using the id and name attributes, and the corresponding implementation class is specified using the class element. If the id and name are not specified, Spring will use the class value as id.
Four ways to instantiate beans in Spring
This article mainly introduces four ways to instantiate beans (injection methods) or four ways to rely on object instantiation. The above program uses the method of creating bean objects, and uses the constructor method (Spring can create class objects under the privatization of constructors)
There are four common creation methods:
1) Setter method
2) Constructor
3) Static factory
4) Example factory
1. Use setter method
public interface IUserDao { void addUser(); void delUser(); void updateUser(); } public class UserDaoImpl implements IUserDao { public void addUser() { System.out.println("addUser method was called"); } public void delUser() { System.out.println("delUser method was called"); } public void updateUser() { System.out.println("updateUser method was called"); } public class UserAction { private IUserDao dao; //dao is a dependency object that needs to be managed by springg and generates the get set method public void execute(){ dao.addUser(); dao.updateUser(); dao.delUser(); } }//Configuration file <bean name="userAction_name" ><property name="dao" ref="userDao_name" /> //The name is referenced below</bean> <bean name="userDao_name" />
//Test ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");UserAction action=(UserAction)ctx.getBean("userAction_name");action.execute();2. Constructor function
public class UserAction { //public UserAction(){} You can keep a constructor without parameters// These are several dependencies, and there is no need to generate a get set method private UserInfo user; private String school; private IUserDao dao; //I hope Spring injects the dependency object public UserAction(IUserDao dao,UserInfo user,String school){ this.dao=dao; this.school=school; this.user=user; } public void execute(){ dao.addUser(); dao.updateUser(); dao.delUser(); System.out.println(user); System.out.println(school);}//Configuration file<bean name="userInfo_name" > <property name="id" value="1" /> <property name="userName" value="weekly" /> <property name="password" value="123" /> <property name="note" value="This is a note" /></bean> <bean name="userAction_name" > <constructor-arg ref="userDao_name" /> <constructor-arg ref="userInfo_name" /> <constructor-arg value="Harbin Normal University" /></bean> /*You can also specify index and type properties, neither index nor type can be specified <bean name="userAction_name" ><constructor-arg index="0" ref="userDao_name" type="cat.dao.IUserDao" /> If it is an interface, it cannot be specified for the type of the implementation class.<constructor-arg index="1" ref="userInfo_name" type="cat.beans.UserInfo" /><constructor-arg index="2" value="Harbin Normal University" /></bean>*/ <bean name="userDao_name" />
//Test ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");UserAction action=(UserAction)ctx.getBean("userAction_name");action.execute();3. Static factory method
//Factory, the implementation class used to generate dao public class UserDaoFactory {public static IUserDao createUserDaoInstance(){ return new UserDaoOracleImpl(); }} public class UserAction { private IUserDao dao;//Use factory method to pay attention to the value, and also generate the set method public void execute(){ dao.addUser(); dao.updateUser(); dao.delUser();}public void setDao(IUserDao dao) { this.dao = dao; } }//Configuration file<bean name="userAction_name" ><property name="dao" ref="userDao_name" /></bean><bean name="userDao_name" factory-method="createUserDaoInstance" />
//Test ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");UserAction action=(UserAction)ctx.getBean("userAction_name");action.execute();IV. Example factory
//Factory=>public class UserDaoFactory {//This method is not a static public IUserDao createUserDaoInstance(){ return new UserDaoOracleImpl(); }}//Configuration file<bean name="userAction_name" ><property name="dao" ref="userDao_name" /></bean><bean name="userDaoFactory_name" /><bean name="userDao_name" factory-bean="userDaoFactory_name" factory-method="createUserDaoInstance" />
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.