Spring学习(四)Spring配置文件常用内容

本文介绍了Spring配置文件中的别名概念,如何为Bean对象设置别名,以及Bean的配置细节,包括id、class、name和scope属性。此外,还探讨了如何通过import导入其他配置文件,实现资源的合并。
摘要由CSDN通过智能技术生成

Spring学习(四)Spring配置文件常用内容

spring配置

首先我们的基础文件和上一节是一样的:

//实体类 src/main/java/com/hj/pojo/User.java
public class User {
    private String name;
    public User(String name){
        this.name=name;
        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);
    }
}

resources/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="com.hj.pojo.User">
        <!--构造函数参数名称 name="参数名称"-->
        <constructor-arg name="name" value="Spring"/>
    </bean>
</beans>

别名

别名就是<alias name=“bean的id(对象名)” alias=“别名”/>

<!--别名,如果添加了别名,也可以使用这个别名来获取这个对象-->
<!--<alias name="bean里的id(对象名)" alias="新取的别名"/>-->
<alias name="user" alias="newUser"/>

测试类

import com.hj.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User newUser = (User) context.getBean("newUser");//这里使用别名
        newUser.show();
    }
}

Bean的配置

是创建对象的注册信息。

<!--
id:bean的唯一标识符,也就是相当于对象名
class:bean对象所对应的类型,全限定名:包名+类名
name:也是别名,可以同时取多个别名
scope:作用域
-->
<bean id="user2" class="com.hj.pojo.User" name="newUser2,u2" scope="singleton">
    <constructor-arg name="name" value="second"/>
</bean>

测试类:

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

import导入

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

在你最终的一个配置文件里导入其他配置文件。

<import resource="beans.xml"/>

我们新增beans2.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="from2User" class="com.hj.pojo.User">
        <constructor-arg name="name" value="来自2的对象"/>
    </bean>
</beans>

然后新建一个applicationContext.xml文件,将beans.xml和beans2.xml导入在这个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">

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

</beans>

测试,我们读取applicationContext.xml文件,然后看看能不能取得对应的beans.xml。

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 依然可以取到beans.xml里注册的对象
        User newUser = (User) context.getBean("user");
        newUser.show();
        // 也可以取得beans2.xml里注册的对象
        User user2 = (User) context.getBean("from2User");
        user2.show();
    }
}

发现是可以的。

本章节代码

本章节代码 中的spring-04中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值