spring学习(二)

spring中bean的实例化
在spring中,常用的实例化有三种,分别是:构造器实例化、静态工厂实例化、实例工厂实例化。接下来我们来分别看下这三种实例化方式。

项目结构图
结构.png

1、构造器实例化
添加我们的bean文件,并添加setter方法及toString方法,在eclipse中快捷键为Alt+Shift+S;选择“getter and setter”和“toString”即可。inteilij快捷键为“Atl+Insert”,选择“setter” 和 “toString”即可。
Bean1.java

package testBean1;

public class Bean1 {
    private String name;
    private int age;
    private String job;
    /*public Bean1(){
        System.out.println("bean1实例化中...");
    }*/
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setJob(String job) {
        this.job = job;
    }
    @Override
    public String toString() {
        return "Bean1 [name=" + name + ", age=" + age + ", job=" + job + "]";
    }

}

applicationContext.xml

<?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 = "testBean1.Bean1"></bean>
    
</beans>

创建测试程序bean1Instance,查看构造器实例化是否成功。
bean1Instance.java

package testBean1;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class bean1Instance {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
        
        System.out.println(bean1);

    }

}

运行结果图:
结果.png

2、静态工厂实例化

Bean2.java

package testBean2;

public class Bean2 {
    private String name;
    private int age;
    private String job;
    /*Bean2(){
        System.out.println("bean2实例化中...");
    }*/
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    @Override
    public String toString() {
        return "Bean2 [name=" + name + ", age=" + age + ", job=" + job + "]";
    }    
}

myStaticFactory.java

package testBean2;

public class myStaticFactory {
    public static Bean2 creatBean2(){
        return new Bean2();
    }
}

创建测试程序bean2Instance,查看静态工厂实例化是否成功。
bean2Instance.java

package testBean2;

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class bean2Instance {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
            System.out.println(bean2);
    
        }
    
    }

在applicationContext.xml文件中添加如下代码:

<!-- 静态工厂实例化 -->
<bean id = "bean2" class = "testBean2.myStaticFactory" factory-method="creatBean2"></bean>

运行结果图:
结果二.png

3、实例工厂实例化

Bean3.java

package testBean3;

public class Bean3 {
    private String name;
    private int age;
    private String job;
    /*Bean3(){
        System.out.println("bean3实例化中...");
    }*/
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    @Override
    public String toString() {
        return "Bean3 [name=" + name + ", age=" + age + ", job=" + job + "]";
    }
    

}

myInstanceFactory.java

package testBean3;

public class myInstanceFactory {
    public Bean3 creatBean3(){
        return new Bean3();
    }

}

在applicationContext.xml文件中添加如下代码:

<!-- 实例工厂实例化 -->
<bean id = "myFactory" class = "testBean3.myInstanceFactory"></bean>
<bean id = "bean3" factory-bean="myFactory" factory-method="creatBean3"></bean>

创建测试程序bean3Instance,查看实例工厂实例化是否成功。
bean3Instance.java

package testBean3;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class bean3Instance {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");
        System.out.println(bean3);

    }

}

运行结果图:
结果三.png

spring的作用域
实例化方式了解之后,我们再来了解下spring中bean的作用域,在这里我只介绍两种常见的作用域:singleton 和 prototype。
注:spring默认的作用域是singleton。

首先创建两个空类,分别让他们的作用域为singleton 和 prototype。
singletonScope.java

package testScope;

public class singletonScope {

}

prototypeScope.java

package testScope;

public class prototypeScope {

}

在applicationContext.xml文件中添加如下代码:

<!-- 作用域测试 -->
<bean id = "scope1" class = "testScope.singletonScope" scope = "singleton"/>
<bean id = "scope2" class = "testScope.prototypeScope" scope = "prototype"/>

创建测试类instanceScope.java:

package testScope;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class instanceScope {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("singleton 作用域:");
        System.out.println(applicationContext.getBean("scope1"));
        System.out.println(applicationContext.getBean("scope1"));
        System.out.println("prototype 作用域:");
        System.out.println(applicationContext.getBean("scope2"));
        System.out.println(applicationContext.getBean("scope2"));

    }

}

运行结果图:
结果四.png

通过观察,我们发现scope为singleton的类,第二次获得时仍然是前一个对象;而scope为prototype的类,第二次获得的对象与前一个对象不同。所以我们需要根据我们自己的需求来选择不同的作用域。

Last modification:April 3rd, 2019 at 11:53 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment