在线客服
扫描二维码
下载博学谷APP扫描二维码
关注博学谷微信公众号
今天,小编将分享大家在学习Spring框架的时候都会遇到的xml配置实例的干货讲解。大家都应该知道,Spring提供的最基本功能是,对象无需自己查找或者创建与它相关联的其他对象,由容器负责将需要相互协作的对象引用传递给各个对象。如果如果没有配置,Spring只是一个没有任何作用的空容器。因此Spring提供了使用XML文件配置和使用注解配置Bean的方式。
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培训班在线学习方法是什么?
7363
2019-08-08 10:23:30
自学Java要多长时间?学多久可以找工作?
自学Java要多长时间?学多久可以找工作?相信这些都是Java初学者十分关心的问题,但是这些问题并没有一个明确的答案,毕竟学习者自身的能力水平和付出的努力不同,得到的结果也就不一样。一般来讲,自学Java到可以找工作的程度,至少需要一年以上。这还是算比较乐观的情况,许多人常常在入门Java不久后就放弃了学习了。下面我们来看看Java的学习方法和学习内容。
10665
2020-01-11 11:25:39
SpringMVC快速入门学习笔记
今天我们来分享一篇SpringMVC快速入门的学习笔记,主要内容包括SpringMVC简介、开发步骤、代码实现和SpringMVC流程。对SpringMVC有兴趣的同学就接着看下去吧~
4071
2020-04-20 19:25:30
为什么Java学完之后忘得好快怎么办?
参加培训班学Java知识点较多,短时间学习记忆不深就容易出现遗忘的情况,对于初学者来说这种情况比较正常,应该时常巩固加深印象,多练习做到学以致用。
4907
2021-01-06 13:55:46
如何才能快速成为一名Java架构师?
如何才能快速成为一名Java架构师?很多程序员从业2、3年后都会进入一个迷茫期,或是瓶颈期,不知道如何进一步提升成为一名Java架构师。解决这个问题其本质是知道如何学习才能快速成为架构师。接下来我们就具体的了解一下。
3017
2022-09-29 16:42:05