网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Spring中xml配置的示例分析

这篇文章给大家分享的是有关Spring中xml配置的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

成都创新互联公司是一家专业提供万源企业网站建设,专注与成都网站建设、网站设计、H5建站、小程序制作等业务。10年已为万源众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

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):

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

加个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是灵活的,不需要单例模式时,可以如下配置:

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文件配置:

感谢各位的阅读!关于“Spring中xml配置的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


文章名称:Spring中xml配置的示例分析
标题URL:http://bjjierui.cn/article/gsosep.html

其他资讯