专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Spring(配置详解、创建对象的方式、scope属性、生命周期属性、配置文件的分模块)

1、配置文件详解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.domain.Student"></bean>
</beans>

(1):使用该容器描述需要Spring管理的对象

(2):给被管理的对象去一个名字,方便获取对象,该名字可以重复,可以使用特殊字符。

(3):与name属性的作用相同,但是属性的值不能重复,不能使用特殊字符。

(4):被管理对象的完整类名。

2、Spring创建对象的方式

(1)空参构造:

创建一个Student对象,里面含有空参的构造方法:

public class Student {
    private String snum;
    private String sname;
    private String sex;
    public Student(){
        System.out.println("我是Student类的空参构造方法!");
    }

    public String getSnum() {
        return snum;
    }

    public void setSnum(String snum) {
        this.snum = snum;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{" +
                "snum='" + snum + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.domain.Student"></bean>
</beans>

创建测试类:

public class Test {
    public void test1(){
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        Student student=(Student)applicationContext.getBean("student");
        student.setSname("zhai");
        student.setSnum("202012");
        student.setSex("nv");
        System.out.println(student);
    }
    public static void main(String[] args){
        Test test=new Test();
        test.test1();
    }
}

该类创建了Spring容器,并且从容器中获取到了Student对象。

80_1.png

从测试结果可以看出,该方法是利用空参构造的方式来创建对象的。

(2)静态工厂方式创建:

与空参构造的方式不同,静态工厂的创建方式需要自己创建一个类,在该类中产生一个对象并返回。而空参构造的方式是Spring容器采用看参构造的方式创建的,不需要自己创建方法来产生对象。

import pers.zhb.domain.Student;
public class StudentFactory {
    public static Student creatStudent(){
        System.out.println("采用静态工厂的方式创建Student对象!");
        return new Student();
    }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.hello.StudentFactory" factory-method="creatStudent"></bean>
</beans>

指明了产生对象的类和该类的方法。

80_2.png

(3)实例工厂方式创建:

创建一个类,该类中的方法产生Student对象,不过,该方法不是静态的。

public class StudentFactory {
    public  Student creatStudent(){
        System.out.println("采用实例工厂的方式创建Student对象!");
        return new Student();
    }
}

配置文件:

 <bean name="student"
 factory-bean="stuFactory"
 factory-method="creatStudent"
 >
 </bean>
<!--创建StudentFactory对象-->
 <bean name="stuFactory"
      class="pers.zhb.hello.StudentFactory"
 >
<bean name="stuFactory" class="pers.zhb.hello.StudentFactory">产生StudentFactory对象,此时配置文件调用工厂类,产生工厂类对象。上面的代码调用该工厂类对象并
调用产生Student对象的方法产生Student对象。

3、Spring配置文件scope属性

(1)singleton(单例对象,默认值)

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student" scope="singleton"></bean>
</beans>

测试类:

public class Test {
    public void test1(){
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        Student student=(Student)applicationContext.getBean("student");
        Student student1=(Student)applicationContext.getBean("student");
        System.out.println(student==student1);
    }
    public static void main(String[] args){
        Test test=new Test();
        test.test1();
    }
}

在测试类的方法中,创建了两个对象,返回值为true,也就是说为单例对象。

(2)prototype:多例对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student" scope="prototype"></bean>
</beans>

每次调用的时候才会创建对象,且每次创建的时候都不是同一个对象。

(3)request:web环境下,对象的生命周期与requst一样

(4)session:web环境下,对象的生命周期与session一样

4、生命周期属性

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student" destroy-method="destory" init-method="init"></bean>
</beans>

init方法是生命周期的初始化方法,Spring会在对象创建之后立即调用,destory方法在Spring容器关闭并销毁所有对象前调用。

测试类:

public class Test {
    public void test1(){
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        Student student=(Student)applicationContext.getBean("student");
        ((ClassPathXmlApplicationContext) applicationContext).close();
    }
    public static void main(String[] args){
        Test test=new Test();
        test.test1();
    }
}

80_3.png

5、分模块

 <import resource="路径"></import>

分模块即引入其他配置文件,这样的话所有的配置文件不用写在一个配置文件中,只需要引入即可。

6、Bean的种类

(1)普通Bean

之前操作的都是普通Bean,spring直接创建实例并返回

(2)FactoryBean:是一个特殊的Bean,具有工厂生成对象的能力,只能生成特定的对象,bean必须使用FactoryBean接口,此接口提供方法getObject()用于获得特定的bean

7、BeanFactory和FactoryBean对比
BeanFactory:工厂,用于生成任意bean

FactoryBean:特殊Bean,用于生成另一个特定的Bean

文章永久链接:https://tech.souyunku.com/23315

未经允许不得转载:搜云库技术团队 » Spring(配置详解、创建对象的方式、scope属性、生命周期属性、配置文件的分模块)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们