Four ways to instantiate bean s in Spring

This article mainly introduces four ways to instantiate beans (injection way) or four ways to instantiate dependent objects. The above program, to create a bean object, uses what method and constructor method (Spring can create a class object when the constructor is privatized)

There are four common creation methods:

1) setter method

2) Constructor

3) Static factory

4) Example factory

1, Using setter

public interface IUserDao {
                void addUser();
                void delUser();
                void updateUser();
            }
            
            public class UserDaoImpl implements IUserDao {
                public void addUser() {
                    System.out.println("addUser Method called");
                }        
                public void delUser() {
                    System.out.println("delUser Method called");
                }        
                public void updateUser() {
                    System.out.println("updateUser Method called");
                }
            }
            
            public class UserAction {
                    private IUserDao dao; //dao Is a dependent object,Must from springg Manage,To generate get set Method
                            public void execute(){
                            dao.addUser();
                            dao.updateUser();
                            dao.delUser();
                    }
                }
//configuration file
<bean name="userAction_name" class="cat.action.UserAction" >
<property name="dao" ref="userDao_name" />  //It refers to the following name
</bean>    
<bean name="userDao_name" class="cat.dao.UserDaoImpl" />
 //test
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
UserAction action=(UserAction)ctx.getBean("userAction_name");
action.execute(); 

2, Constructor

public class UserAction {
       //public UserAction(){} Can keep a parameterless constructor
                
       //These are several dependent objects,Do not generate get set Method
       private UserInfo user;
       private String school;
       private IUserDao dao;     
            
       //hope Spring Inject dependent objects by constructor
       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" class="cat.beans.UserInfo" >
      <property name="id" value="1" />
      <property name="userName" value="Zhou Zhou" />
      <property name="password" value="123" />
      <property name="note" value="This is the note" />
</bean>
                    
<bean name="userAction_name" class="cat.action.UserAction" >
      <constructor-arg ref="userDao_name" />
      <constructor-arg ref="userInfo_name" />
      <constructor-arg value="harbin normal university " />
</bean>
            
/*
You can also specify the index and type attributes, and neither index nor type can be specified
<bean name="userAction_name" class="cat.action.UserAction" >
<constructor-arg index="0" ref="userDao_name" type="cat.dao.IUserDao" />  If it is an interface, you cannot specify the type of 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" class="cat.dao.UserDaoImpl" />
//test
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
UserAction action=(UserAction)ctx.getBean("userAction_name");
action.execute(); 

3, Static factory mode

//factory,Used to generate dao Implementation class of
public class UserDaoFactory {
public static IUserDao createUserDaoInstance(){
       return new UserDaoOracleImpl();
       }
}

           
public class UserAction {
       private IUserDao dao;//Use factory mode to note value,Also generate 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" class="cat.action.UserAction" >
<property name="dao"  ref="userDao_name" />
</bean>
              
<bean name="userDao_name" class="cat.dao.UserDaoFactory" factory-method="createUserDaoInstance" />
//test
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
UserAction action=(UserAction)ctx.getBean("userAction_name");
action.execute(); 

4, Example factory

//factory =>
public class UserDaoFactory {
//This method is not static
public  IUserDao createUserDaoInstance(){
        return new UserDaoOracleImpl();
        }
}
//configuration file 
<bean name="userAction_name" class="cat.action.UserAction" >
<property name="dao"  ref="userDao_name" />
</bean>
              
<bean  name="userDaoFactory_name" class="cat.dao.UserDaoFactory" />
<bean name="userDao_name" factory-bean="userDaoFactory_name" factory-method="createUserDaoInstance" />

Tags: Java xml Spring

Posted on Fri, 01 May 2020 02:37:29 -0700 by misc00500