The way to create Bean object in Spring

Ready to work

Create a maven project, create a package and write a UserService interface and interface implementation class UserServiceImpl
Insert picture description here
UserService interface

/*
* 保存用户接口
* */
public interface UserService {
    
    
    /*
    * 保存用户
    * */
    void saveUser();
}

UserService interface implementation class

/*
* 保存用户实现类
* */
public class UserServiceImpl implements UserService {
    
    

    public void saveUser() {
    
    
        System.out.println("saveUser方法执行了");
    }
}

The jar package introduced by the pom file is the same as the previous article, you can refer to the previous chapter.

One, the default construction method is created

  Spring creates Bean by default by default construction method. If there is no construction method, it will not be created. We only need to configure the following in the configuration file, which is the same as the configuration of the springIoc entry case in the previous chapter.
bean.xml configuration file:

<?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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    第一种方式:默认构造方法
    配置好bean标签的id和class属性后,没有配置其他的属性和标签时,采用的就是
    默认构造方法创建实例,如果没有默认构造函数无法创建实例。
    -->
    <bean id="userService" class="com.zy.service.impl.UserServiceImpl"></bean>
</beans>

Test category:

public class TestDemo01 {
    
    
    public static void main(String[] args) {
    
    
        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //根据bean标签的id获取实例对象
        UserService userService = (UserService) context.getBean("userService");
        //调用方法
        userService.saveUser();
    }
}

operation result:
Insert picture description here

Second, the instance factory is created

  To create through the instance factory, we first write a factory class InstanceFactory under the factory package, and write a createUserService() method with a return value of UserServiceImpl

public class InstanceFactory {
    
    
    /*
    * 我们模拟一个实例工厂
    * */
    public UserService createUserService(){
    
    
        //通过返回值 new UserServiceImpl
        return  new UserServiceImpl();
    }
}

bean.xml configuration:

<?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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
     第二种方式:通过实例工厂
       首先通过bean标签实例化工厂类,在通过factory-bean属性引用实例化的工厂
       factory-method属性配置工厂类中的方法
    -->
    <bean id="instanceFactory" class="com.zy.factory.InstanceFactory"></bean>
    <bean id="userService" factory-bean="instanceFactory" factory-method="createUserService"></bean>
</beans>

The test class is the same as the test class of the first default construction method above.

Three, static factory creation

  To create it through a static factory, we first write a factory class called staticFactory under the factory package, write a createUserService() method with a return value of UserServiceImpl and add the static keyword modification.

public class staticFactory {
    
    
    /*
    * 我们模拟一个实例工厂
    * */
    public static UserService createUserService(){
    
    
        //通过返回值 new UserServiceImpl
        return  new UserServiceImpl();
    }
}

bean.xml configuration:

<?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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
     第三种方式:通过静态工厂
    -->
    <bean id="userService" class="com.zy.staticFactory.staticFactory" factory-method="createUserService"></bean>
</beans>

The test class is the same as the test class of the first default construction method above.
Come on, boy

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/110942647