• 在线客服

  • 扫描二维码
    下载博学谷APP

  • 扫描二维码
    关注博学谷微信公众号

  • 意见反馈

原创 Spring学习干货xml配置实例

发布时间:2019-08-08 11:58:53 浏览 6275 来源:博学谷资讯 作者:照照

    今天,小编将分享大家在学习Spring框架的时候都会遇到的xml配置实例的干货讲解。大家都应该知道,Spring提供的最基本功能是,对象无需自己查找或者创建与它相关联的其他对象,由容器负责将需要相互协作的对象引用传递给各个对象。如果如果没有配置,Spring只是一个没有任何作用的空容器。因此Spring提供了使用XML文件配置和使用注解配置Bean的方式。

     

    Spring学习xml配置实例

     

    1、一个Performer接口:

     

    package springAction;

     

    public interface Performer {

     

    void perform();

     

    }

     

    2、一个Juggler类实现了Performer接口:

     

    package springAction;

     

    public class Juggler implements Performer{

     

    private int beanBags = 3;

     

    public Juggler(){

     

    }

     

    public Juggler(int beanBags){

     

    this.beanBags = beanBags;

     

    }

     

    public void perform(){

     

    System.out.println("Juggling " + beanBags + " beanbags");

     

    }

     

    }

     

    3、一个 PoeticJuggler类继承了Juggler类:

     

    package springAction;

     

    public class PoeticJuggler extends Juggler {

     

    private Poem poem;

     

    public PoeticJuggler(Poem poem){

     

    super();

     

    this.poem = poem;

     

    }

     

    public PoeticJuggler(int beanBags, Poem poem){

     

    super(beanBags);

     

    this.poem = poem;

     

    }

     

    public void perform(){

     

    super.perform();

     

    System.out.println("PoeticJugger reciting...");

     

    poem.recite();

     

    }

     

    }

     

    4、一个Poem接口:

     

    package springAction;

     

    public interface Poem {

     

    void recite();

     

    }

     

    5、一个单例类Stage:

     

    package springAction;

     

    public class Stage {

     

    private Stage(){

     

    }

     

    private static class StageSingletonHolder{

     

    static Stage instance = new Stage();

     

    }

     

    public static Stage getInstance(){

     

    return StageSingletonHolder.instance;

     

    }

     

    }

     

    6、看看xml文件怎么写(springAction.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-4.1.xsd">

     

    <!-- 配置一个bean -->

     

    <bean id="duke" class="springAction.Juggler" >

     

    <!--给构造函数传递参数,没有的话则调用默认构造方法 -->

     

    <constructor-arg value="15"/>

     

    </bean>

     

    <bean id="sonnet29" class="springAction.Sonnet29"></bean>

     

    <bean id="poeticDuke" class="springAction.PoeticJuggler">

     

    <constructor-arg value="22"></constructor-arg>

     

    <!-- 基本数据类型参数用value=字符串(Spring根据构造参数类型自动解析字符串),

     

    引用类型的参数用ref= bean id -->

     

    <constructor-arg ref="sonnet29"></constructor-arg>

     

    </bean>

     

    <!-- factory-method通过工厂方法将单例类配置为bean,因为Stage没有构造函数 -->

     

    <bean id="theStage" class="springAction.Stage" factory-method="getInstance">

     

    </bean>

     

    </beans>

     

    加个main函数运行一下,看看效果:

     

    package springAction;

     

    import org.springframework.context.ApplicationContext;

     

    import org.springframework.context.support.ClassPathXmlApplicationContext;

     

    public class springActionMain {

     

    public static void main(String[] args) {

     

    // TODO Auto-generated method stub

     

    ApplicationContext ctx = new ClassPathXmlApplicationContext(

     

    "springAction/springAction.xml");

     

    try{

     

    Performer performer = (Performer)ctx.getBean("duke");

     

    performer.perform();

     

    performer = (Performer)ctx.getBean("poeticDuke");

     

    performer.perform();

     

    }catch(Exception e){

     

    e.printStackTrace();

     

    }finally{

     

    ((ClassPathXmlApplicationContext)ctx).close();

     

    }

     

    }

     

    }

     

    7、bean的作用域

     

    所有的Spring Bean默认是单例,当容器分配一个Bean时,它总是返回同一个实例。但,spring是灵活的,不需要单例模式时,可以如下配置:
    <bean id="ticket" class="省略号" scope="prototype"/>

     

    8、初始化Bean和销毁Bean

     

    当Spring容器实例化一个Bean时可能需要做一些初始化的工作,当Spring销毁一个Bean时,可能需要做一些清理工作。Spring支持Bean提供自定义的方法来做初始化工作和销毁工作。

     

    9、为Bean注入属性

     

    一般地,java Bean会有一些私有属性,并有一些set和get方法。Spring可以利用set方法为bean的私有属性注入值。

     

    例子:

     

    package springAction;

     

    public class Instrumentalist implements Performer{

     

    private String song;

     

    private Instrument instrument;

     

    private int age;

     

    public Instrumentalist(){

     

    }

     

    public void perform(){

     

    System.out.println("age = " + age);

     

    System.out.println("Playing "+song);

     

    instrument.play();

     

    }

     

    public String getSong() {

     

    return song;

     

    }

     

    public void setSong(String song) {

     

    this.song = song;

     

    }

     

    public Instrument getInstrument() {

     

    return instrument;

     

    }

     

    public void setInstrument(Instrument instrument) {

     

    this.instrument = instrument;

     

    }

     

    public int getAge() {

     

    return age;

     

    }

     

    public void setAge(int age) {

     

    this.age = age;

     

    }

     

    }

     

    xml文件配置:

     

    <bean id="saxophone" class="springAction.Saxophone"/>

     

    <bean id="kenny" class="springAction.Instrumentalist">

     

    <!-- 配置属性song -->

     

    <property name="song" value="ABC"></property>

     

    <!-- 配置属性age,虽然此处是字符串“14”,但是Spring会识别age的类型,然后把字符串“14”转变后赋值给age -->

     

    <property name="age" value="14"></property>

     

    <!-- 配置instrument对象,用ref的方式赋引用,和构造函数一致 -->

     

    <property name="instrument" ref="saxophone"></property>

     

    </bean>

     

    以上就是Spring学习之xml配置实例的干货讲解,还有想学习Spring框架知识的小伙伴,可以上博学谷在线观看视频,相信会收获更多的干货知识哦!

    申请免费试学名额    

在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!

上一篇: 在线培训Java学费多少钱? 下一篇: 参加java程序员培训需要注意哪些问题?

相关推荐 更多

热门文章

  • 前端是什么
  • 前端开发的工作职责
  • 前端开发需要会什么?先掌握这三大核心关键技术
  • 前端开发的工作方向有哪些?
  • 简历加分-4步写出HR想要的简历
  • 程序员如何突击面试?两大招带你拿下面试官
  • 程序员面试技巧
  • 架构师的厉害之处竟然是这……
  • 架构师书籍推荐
  • 懂了这些,才能成为架构师
  • 查看更多

扫描二维码,了解更多信息

博学谷二维码