Spring 如何在xml中配置Bean?

本文目的

  配置Spring IOC 容器中的Bean。


问题描述

  Spring提供了一个容器来管理Bean,本文用xml来看下如何配置Bean,让容器来管理。


现在以生成一个随机序列值,它有前缀,后缀,作为演示。

详细代码

xml中的配置-方案一:
ide用的是IDEA讲xml配置文件放在resources中conf文件夹取名为:spring-ioc.xml

<?xml version="1.0" encoding="GBK"?>
<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-3.0.xsd">

    <bean name="sequenceGenerator" class="com.swagger.template.ioc.SequenceGenerator">
        <property name="prefix">
            <value>30</value>
        </property>
        <property name="suffix">
            <value>A</value>
        </property>
        <property name="initial">
            <value>1</value>
        </property>
    </bean>
</beans>

xml中的配置-方案二:
ide用的是IDEA讲xml配置文件放在resources中conf文件夹取名为:spring-ioc.xml

<?xml version="1.0" encoding="GBK"?>
<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-3.0.xsd">

    <bean name="sequenceGenerator" class="com.swagger.template.ioc.SequenceGenerator">
    <!--    省去value标签 -->
        <property name="prefix" value="30">
        </property>
        <property name="suffix" value="A">
        </property>
        <property name="initial" value="1">
        </property>
    </bean>
</beans>

这里有个[lombox的依赖包下注解@Data可以免去getter/setter]
java类:

package com.swagger.template.ioc;

import lombok.Data;

@Data
public class SequenceGenerator {

    private String prefix;
    private String suffix;
    private int initial;
    private int counter;

    //xml中配置bean一定要有这个默认空的构造器
    public SequenceGenerator() {
    }

    public SequenceGenerator(String prefix, String suffix, int initial) {
        this.prefix = prefix;
        this.suffix = suffix;
        this.initial = initial;
    }

    public synchronized String getSequence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefix);
        buffer.append(initial + counter++);
        buffer.append(suffix);
        return buffer.toString();
    }
}

测试:

package com.swagger.template.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        //ApplicationContext只是一个接口,我们要实例化一个接口的实现,从配置文件中获取
        //注意路径:我这里是放在conf文件夹下的
        ApplicationContext context =
                new ClassPathXmlApplicationContext("conf/spring-ioc.xml");

        //获取到的Bean是Object类型,要转换成我们需要的类型
        SequenceGenerator generator = (SequenceGenerator) context.getBean("sequenceGenerator");
        //输出结果测试,每次的都会不一样的序列值
        System.out.println("====================Test=======================");
        System.out.println(generator.getSequence());//输出结果:301A
        System.out.println(generator.getSequence());//输出结果:302A
         System.out.println("====================和new出来的对比=======================");
        SequenceGenerator generator1 = new SequenceGenerator();
        generator1.setInitial(0);
        generator1.setPrefix("30");
        generator1.setSuffix("A");
        System.out.println(generator1.getSequence());//输出结果:301A
        System.out.println(generator1.getSequence());//输出结果:302A
    }
}

总结
  1. bean的名称是唯一的
  2. 配置一个bean还有构造器配置,会使用到constructor-arg标签

ELEVEN:
个人转载无须申请版权许可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值