7.3 Java EE - Bean instantiation

1. Instantiation of the construction method

The following is a case to demonstrate how the Spring container instantiates a Bean through the construction method. 

(1) Create a Maven project named chapter07 in IDEA, and then configure the four basic Spring packages and Spring dependency packages to be used in the pom.xml file of the project.

<dependencies>
        <!-- spring-core的依赖包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- spring-beans的依赖包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- spring-context的依赖包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- spring-expression的依赖包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- commons-logging的依赖包 -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
    </dependencies>

(2) Create a package named com.mac, and create the Bean1 class in this package. 

package com.mac;
public class Bean1 {
    public Bean1(){
        System.out.println("这是Bean1");
    }
}

(3) Create a new applicationBean1.xml as the configuration file of the Bean1 class, define a Bean whose id is bean1 in the configuration file, and specify its corresponding implementation class as Bean1 through the class attribute. 

<?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="bean1" class="com.mac.Bean1"></bean>
</beans>

(4) Create a test class Bean1Test, initialize the Spring container by loading the applicationBean1.xml configuration file in the main() method, and then generate an instance bean1 of the Bean1 class through the Spring container to test whether the construction method can instantiate Bean1.

public class Bean1Test {
    public static void main(String[] args){
        // 加载applicationBean1.xml配置
        ApplicationContext applicationContext=new 
                                             ClassPathXmlApplicationContext("applicationBean1.xml");
        // 通过容器获取配置中bean1的实例
        Bean1 bean=(Bean1) applicationContext.getBean("bean1");
        System.out.print(bean);		}
}

(5) Start the Bean1Test class in IDEA, and the console will output the results.

 

2. Static factory instantiation 

The following demonstrates how to instantiate a Bean using a static factory through a case.

(1) Create the Bean2 class, which is the same as the Bean1 class, and only defines a construction method without adding any additional methods.

package com.mac;
public class Bean2 {
    public Bean2(){
        System.out.println("这是Bean2");
    }
}

 (2) Create a MyBean2Factory class, and define a static method createBean() in this class to create Bean instances. The createBean() method returns the Bean2 instance.

package com.mac;
public class MyBean2Factory {
	//使用MyBean2Factory类的工厂创建Bean2实例
    	public static Bean2 createBean(){
      	return new Bean2();
    	}
}

(3) Create a new applicationBean2.xml file as the configuration file of the MyBean2Factory class. 

<?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-4.3.xsd">
<bean id="bean2"
      class="com.mac.MyBean2Factory"
      factory-method="createBean"/>
</beans>

(4) Create a test class Bean2Test to test whether the Bean can be instantiated using the static factory method. 

public class Bean2Test {
    public static void main(String[] args) {
        // ApplicationContext在加载配置文件时,对Bean进行实例化
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationBean2.xml");
        System.out.println(applicationContext.getBean("bean2"));
    }
}

 (5) Start the Bean2Test class in IDEA, and the console will output the results.

 

 

Guess you like

Origin blog.csdn.net/W_Fe5/article/details/131667832
Recommended