SPR3: Spring配置文件

3.1 alias标签

3.1.1 简介

​ 类似于MyBatis中给实体类起别名,在Spring中,我们可以给bean起别名。但是,在实际使用中的作用并不大,因为bean标签提供了 name 属性用于起别名,并且更高级。

3.1.2 例子

Spring配置文件:

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

    <bean id="hello" class="com.yun.pojo.Hello">
        <property name="str" value="你好啊,Spring"/>
    </bean>
    <bean id="hello2" class="com.yun.pojo.world">
       <constructor-arg name="str" value="这是name方式"/>
        <constructor-arg name="test" value="这也是"/>
    </bean>
    
    <alias name="hello2" alias="world"/>

</beans>


测试调用:

package com.yun;

import com.yun.pojo.Hello;
import com.yun.pojo.world;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        world w = (world) applicationContext.getBean("world");
        System.out.println(w);
    }
}

总结: 就是在Spring配置文件中,起别名。然后在使用 getBean() 语句时,即可使用别名。

3.2 bean标签

3.2.1 简介

​ bean标签的作用是,向Spring注册实体类,放别容器在后期进行调用。主要使用的属性有 id、name、classid 则是名字,name 则是别名,class 则是类地址。

3.2.2 例子

Spring配置文件:

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

    <bean id="hello" name="word world1,world2;world3" class="com.yun.pojo.Hello">
    </bean>
</beans>

提示: name 的别名可以有多个,可以使用空格、逗号、分号等进行分开。

测试接口:

package com.yun;

import com.yun.pojo.Hello;
import com.yun.pojo.world;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        Hello hello = (Hello) applicationContext.getBean("world3");
        System.out.println(hello);
    }
}

总结: 与alias标签的作用一样,但是更高级。

3.3 import标签

3.3.1 简介

​ import标签的主要作用是在Spring配置文件中,导入其他xml文件。当一个项目进行团队开发时,可能会存在多个Spring配置文件,这时候,便可以在一个xml中,使用import标签,将其他的配置文件导入其中。并且Spring会自动选择其中的bean。而Spring配置文件的官方名字,是 applicationContext.xml

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

    <bean id="hello" class="com.yun.pojo.Hello">
        <constructor-arg name="str" value="里层bean"/>
    </bean>
</beans>

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

    <bean id="hello" class="com.yun.pojo.Hello">
        <constructor-arg name="str" value="外层bean"/>
    </bean>

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

测试接口:

package com.yun;

import com.yun.pojo.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        Hello hello = (Hello) applicationContext.getBean("hello");
        System.out.println(hello);
    }
}

总结: 本案例中存在两个配置文件,最后将一个文件导入到另一个文件中。并且,两个配置文件中的bean名字与实体类都是一致的。只是在为构造器进行参数传递时,所传递的值不同而已。当使用import语句,导致两个名字相同的bean存在时,Spring会自动选择最新的,也就是 配置文件中最下面的


写在最后,bean标签的属性不仅仅只有这几个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值