Spring配置初探

HelloSpring

写一个HelloSpring程序:

  1. 导包,在父项目中已经完成

  2. 新建实体类

    package xyz.luck1y.pojo;
    
    public class Hello {
        private String str;
    
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    
        @Override
        public String toString() {
            return "Hello{" +
                    "str='" + str + '\'' +
                    '}';
        }
    }
    
  3. xml配置

    官方示例:https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/core.html#beans

    <?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">
    
        <!--使用Spring来创建对象,在Spring中这些都成为Bean-->
        <bean id="hello" class="xyz.luck1y.pojo.Hello">
            <property name="str" value="Spring"/>
        </bean>
    </beans>
    
    
  4. 测试:

    提供给ApplicationContext构造函数的位置路径是资源字符串,这些资源字符串使容器可以从各种外部资源(例如本地文件系统,Java CLASSPATH等)加载配置元数据。

    ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import xyz.luck1y.pojo.Hello;
    
    public class MyTest {
        public static void main(String[] args) {
            // 获取Spring的上下文对象
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            // 我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以了
            Hello hello = (Hello) context.getBean("hello");
            System.out.println(hello);
        }
    }
    

    这里一开始会给我报错:

    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hello' available

    然后我去网上冲浪了一下,发现我学到这还没有使用注解,但是

    注解如果没有指定bean的名字,默认为小写开头的类名。例如类名是ProvincialServiceImpl,则spring返回provincialServiceImpl的bean名。

    我的类名这里是大写的,并且在beans.xml文件里我的配置是这样的:

    <!--使用Spring来创建对象,在Spring中这些都成为Bean-->
    <bean id="Hello" class="xyz.luck1y.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>
    

    <bean id="Hello"这里类名大写Spring不会识别,于是我改为<bean id="hello"

    <!--使用Spring来创建对象,在Spring中这些都成为Bean-->
    <bean id="hello" class="xyz.luck1y.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>
    

    再次测试发现没有问题

  5. 结果:

思考:

  • Hello对象是谁创建的?

    hello对象是由Spring创建的

  • Hello对象的属性是怎么设置的?

    hello对象的属性是由Spring容器设置的

<!--
以前: 
	类型 变量名 = new 类型();
    Hello hello = new Hello();
现在:
	bean标签的意思: new Hello();
    id 就是对象的变量名  class 就是要new 的对象的全限定名
    property 相当于给对象中的属性设置一个值
-->

这个过程就叫做控制反转:

  • 控制:谁来控制对象的创建,传统的应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的。
  • 反转:程序本身不创建对象,而变为被动的接收对象
  • 依赖注入:就是利用set方法来进行注入的实体类必须要有set方法
  • IOC是一种编程思想,由主动的编程变成被动的接收
  • 可以通过new ClassPathXmlApplicationContext来浏览一下底层源码

到了现在,我们彻底不用再程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修,所谓的IOC,就是:对象由Spring来创建,管理,装配!

如果实体类在XML中配置过了,那么实体类就会有标志:

下面,我们改造一下前面推导IOC的程序:

  1. 配置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="mysqlImpl" class="xyz.luck1y.dao.UserDaoMySQLImpl"/>
        <bean id="userServiceImpl" class="xyz.luck1y.service.UserServiceImpl">
            <!--ref:引用Spring容器中已经创建好的对象  value:引用八大基本数据类型-->
            <property name="userDao" ref="mysqlImpl"/>
        </bean>
    </beans>
    
  2. 测试类

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import xyz.luck1y.service.UserServiceImpl;
    
    public class MyTest {
        public static void main(String[] args) {
            // 获取ApplicationContext;拿到Spring的容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            // 容器在手,天下我有,需要什么就直接get什么
            UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
            userServiceImpl.getUser();
        }
    }
    

IOC容器创建对象的方式

默认使用无参构造创建对象

  1. 新建实体类,并且在无参构造里输出一句话,表示调用了无参构造

    package xyz.luck1y.pojo;
    
    public class User {
        private String name;
    
        public User() {
            System.out.println("User的无参构造");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show(){
            System.out.println("name=" + name);
        }
    }
    
  2. 配置beans.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="user" class="xyz.luck1y.pojo.User">
            <property name="name" value="刘子"/>
        </bean>
    </beans>
    
  3. 测试:

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import xyz.luck1y.pojo.User;
    
    public class MyTest {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            User user = (User) context.getBean("user");
            user.show();
        }
    }
    
  4. 结果

    这说明,getBean方法调用了无参构造,也就是说在getBean之前,对象已经创建好了。

    其实对象的实例化在获取上下文的时候就创建好了

    接下来我们看看去掉无参构造,即添加有参构造的情况。

使用有参构造来创建对象的方法

  1. 添加有参构造方法

    package xyz.luck1y.pojo;
    
    public class User {
        private String name;
    	
        // 我们不写无参构造也会默认存在的,要写有参构造来顶掉无参构造
        public User(String name) {
            this.name = name;
        }
    
        //    public User() {
    //        System.out.println("User的无参构造");
    //    }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show(){
            System.out.println("name=" + name);
        }
    }
    
  2. 测试:

    运行前发现beans.xml已经爆红

    运行:初始化出错

那我们又该如何使用有参构造方法呢?

  1. 第一种方式:使用下标赋值

    <?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="user" class="xyz.luck1y.pojo.User">
            <!--有参构造 1.使用下标赋值-->
            <constructor-arg index="0" value="刘子"/>
        </bean>
    </beans>
    
  2. 第二种方式:通过参数类型匹配

    不建议使用:因为如果出现两个同类型的参数会出现混淆,会按照参数出现的先后顺序来赋值。

    <?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="user" class="xyz.luck1y.pojo.User">
            <!--有参构造 2.使用类型赋值-->
            <constructor-arg type="java.lang.String" value="刘子"/>
        </bean>
    </beans>
    
  3. 第三种方式:通过参数名匹配

    <?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="user" class="xyz.luck1y.pojo.User">
            <!--有参构造 3.使用参数名匹配-->
            <constructor-arg name="name"  value="刘子"/>
        </bean>
    </beans>
    

此外在配置文件加载的时候,Spring容器中管理的对象,即所有的bean都被实例化了,且每个bean在内存中只有一份实例,比如我们getBean同一个id,然后进行比较,发现是true

Spring配置

Spring标签比MyBatis的标签要少很多。

alias(别名)的配置

别名:

<alias name="user" alias="userTest"/>

name对应的是bean的id,alias是给这个id起的别名,别名区分大小写

在测试类中getBean方法通过getBean(“别名”)还是可以获取到对象的

bean的配置

<bean id="user" class="xyz.luck1y.pojo.User" name="user2 user3,user4;user5">
    <!--有参构造 3.使用参数名匹配-->
    <constructor-arg name="name"  value="刘子"/>
</bean>

id:bean的唯一标识符,也就是相当于对象名

class:bean对象所对应的全限定名(类名+包名)

name:也是别名,name可以同时取多个别名,可以通过逗号/空格/分号来分割

这里有一个高频问点:Bean的生命周期,可以自行搜索查找

Bean 的生命周期概括起来就是 4 个阶段

  1. 实例化(Instantiation)
  2. 属性赋值(Populate)
  3. 初始化(Initialization)
  4. 销毁(Destruction)

import配置

一般用于团队开发,可以将多个配置文件,导入合并为一个。

假如有几个人写的不同的配置文件

在测试时,扫描汇总后的xml、总的xml配置文件applicationContext.xml

import org.springframework.context.support.ClassPathXmlApplicationContext;
import xyz.luck1y.pojo.User;

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user2");
        user.show();
    }
}

这时就可以在总的xml配置文件中使用import标签:

<import resource="beans.xml"/>
<import resource="zhangsan.xml"/>
<import resource="lisi.xml"/>

名字重复会报错

导入之后,其他xml中的bean,总的xml中也就可以拿到了。

import 使用的时候:

  • 与主配置的id重名,使用主配置的id
  • 多个import中配置的id重名(主配置中没有),调用最后import中配置的id,即后面的覆盖前面的
  • 调用别名,哪个配置有这个别名,使用哪个配置

相同id,相同内容 —> 进行合并;

相同id,不同内容 —> 后来居上,进行覆盖。

以上配置算是初级入门,深入的部分还不止这些~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luck1y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值