Spring:IoC 用法(七、<beans>用法)

XML 配置详解

1、<beans> 元素

1)、<description> 元素

作用:描述配置文件的作用

用法:

<?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">

    <description>描述作用:当前这个XML配置无作用,不能重复出现,必须在第一个元素位置</description>

</beans>

2)、<import> 元素

作用:引入另一个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">
    
    <!-- resource 属性:引用另一个XML配置 -->
    <import resource="spring.xml"/>

</beans>

3)、<beans> 元素

作用:<beans>与<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">

    <beans>
        <description>beans 与 beans 嵌套使用</description>
    </beans>
    
</beans>

4)、<bean> 元素

作用:定义实例对象(重点)

用法:

下一节介绍

5)、<alias> 元素

作用:为已经定义的 <bean> 配置别名

用法:

定义Controller控制层

public class TestController {

    public String testController() {
        return "成功注入!!!";
    }
}
配置文件
<?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="testController" class="test.define.controller.TestController"></bean>
    <alias name="testController" alias="c1"/>
    <alias name="testController" alias="c2"/>
</beans>
测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/spring.xml")
@ActiveProfiles("pro")
public class Test {

    @Autowired
    @Qualifier("c1")
    private TestController c1;

    @Autowired
    @Qualifier("c2")
    private TestController c2;

    @org.junit.Test
    public void testXMLConfig() {
        String data;
        data = c1.testController();
        System.out.println("c1: " + data);

        data = c2.testController();
        System.out.println("c2: " + data);
    }
}
结果
c1: 成功注入!!!
c2: 成功注入!!!


2、<beans> 属性

1)、profile 属性

作用:区分不同环境下的类加载

测试:

定义Controller控制层

public class TestController {

    @Autowired
    private TestService testService;

    public String testController() {
        return testService.testService();
    }
}
定义Service服务层接口

public interface TestService {
    
    String testService();
}
定义Service服务层接口实现
public class ProServiceImpl implements TestService {

    public String testService() {
        return "this is ProServiceImpl";
    }
}
public class TestServiceImpl implements TestService {

    public String testService() {
        return "this is TestServiceImpl";
    }
}
配置文件

<?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 class="test.define.controller.TestController"></bean>

    <!-- 环境一 -->
    <beans profile="test">
        <bean id="testService" class="test.define.service.impl.TestServiceImpl"></bean>
    </beans>

    <!-- 环境二 -->
    <beans profile="pro">
        <bean id="testService" class="test.define.service.impl.ProServiceImpl"></bean>
    </beans>
</beans>
测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/spring.xml")
@ActiveProfiles("pro")
public class Test {

    @Autowired
    private TestController controller;

    @org.junit.Test
    public void testXMLConfig() {
        String data = controller.testController();
        System.out.println(data);
    }
}

结果

this is ProServiceImpl
2)、default-lazy-init 属性

作用:容器级别的延迟加载,定义整个 <beans> 范围内的 <bean> 的延迟加载方式(<bean>在使用时加载)

测试:无(通过启动时间或日志的方式判断)

<?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"
       default-lazy-init="true">
    
</beans>

3)、default-autowire 属性

作用:用法同 <bean> 的 autowire,但是属于容器级别(此处不说明)

测试:略

4)、default-autowire-candidates 属性

作用:用法同 <bean> 的 autowire-candidates,但是属于容器级别(此处不说明)

测试:略

5)、default-init-method、default-destroy-method  属性

作用:容器级别的初始化方法(适用于 <beans> 中所有的 <bean>)

          容器级别的销毁方法(适用于 <beans> 中所有的 <bean>)

测试:

初始化测试类

public interface TestService {

    String testService();
}

public class TestServiceImpl implements TestService {

    private int i = 1;

    public String testService() {
        return i++ + " this is TestServiceImpl";
    }

    // 初始化方法
    public void init(){
        System.out.println("this is TestServiceImpl init");
    }

    // 销毁方法
    public void destroy(){
        System.out.println("this is TestServiceImpl destroy");
    }
}

public class ProServiceImpl implements TestService {

    private int i = 1;

    public String testService() {
        return i++ + " this is ProServiceImpl";
    }

    // 初始化方法
    public void init(){
        System.out.println("this is ProServiceImpl init");
    }

    // 销毁方法
    public void destroy(){
        System.out.println("this is ProServiceImpl destroy");
    }
}

配置文件

<?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"
       default-init-method="init"
       default-destroy-method="destroy">
    <!-- 初始化方法、和销毁方法:方法不强制存在 -->

    <!-- 测试类:存在 init 方法、destroy 方法 -->
    <bean class="test.define.service.impl.TestServiceImpl"/>
    <!-- 测试类:存在 init 方法、destroy 方法 -->
    <bean class="test.define.service.impl.ProServiceImpl"/>
</beans>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/spring.xml")
public class Test {

    @org.junit.Test
    public void testXMLConfig() {
        System.out.println("\n测试:容器级别的初始化方法!");
    }
}

结果

this is TestServiceImpl init
this is ProServiceImpl init
测试:容器级别的初始化方法!
this is ProServiceImpl destroy
this is TestServiceImpl destroy



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值