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

本文详细介绍了Spring框架中XML配置的基本元素及其属性,包括&lt;description&gt;、&lt;import&gt;、&lt;beans&gt;、&lt;bean&gt;、&lt;alias&gt;等元素的作用和用法,并探讨了&lt;beans&gt;元素的多种属性如profile、default-lazy-init、default-autowire等的功能及应用场景。

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:学习使用@Component、@Repository、@Service、@Controller、@Autowired等注解完成bean的注入与依赖。完成教材P31页,例3-8的案例代码的编写与调试运行。案例中需要实现Controller层依赖注入Service层,在Service层依赖注入Dao层。 步骤如下: (1) 创建空项目ex3, (2) 在ex2中使用Maven工具创建module: m1-ioc-anno, 使用:maven-archetype-quickstart脚本创建。 (3) 在pom.xml文件中引入Spring框架依赖,并同步pom.xml文件导入依赖包。 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.2.7</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency> (4) 根据教材内容,在指定位置创建TestDao接口及其子类TestDaoImpl,完成相关代码 (5) 根据教材内容,在指定位置创建TestService接口及其子类TestServiceImpl,完成相关代码 (6) 根据教材内容,在指定位置创建TestControll类,完成相关代码 (7) 在配置文件中添加扫描包的配置 (8) 创建测试类TestAnnootation,调试运行程序。 请给出代码及运行结果截图。 回答如下问题: (1) 在TestServiceImpl类中将 @Service("testSeviceImpl") ,注解修改为:@Service("test123"),会不会影响程序的执行?为什么? (2) 在TestDaoImpl类中将注解@Repository("testDaoImpl"),修改为@Repository("test666")会不会影响程序的执行?为什么?
最新发布
09-26
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值