在线客服
扫描二维码
下载博学谷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框架知识的小伙伴,可以上博学谷在线观看视频,相信会收获更多的干货知识哦!
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
abstract class 和 interface 有什么区别?
“abstract class 和 interface 有什么区别?”这个问题在Java基础的笔试题中是比较常见的。下面就是关于这个问题博学谷老师整理的答案。
6530
2019-08-14 10:19:51
零基础Java在线学习方式有哪些?
零基础Java在线学习方式有哪些?总结起来,基本上就是四种方式,即多阅读相关书籍,观看学习视频,实战练习敲代码,与在线老师交流沟通。无论是在线自学也好,还是报线上的培训班,最有效的Java学习方式,也就是以上这些。下面小编来详细为大家讲解Java在线学习具体要怎么做。
5080
2019-09-29 10:31:58
英语不好能学好Java开发吗?
学好Java开发对英语的要求并不高,编程常用的高频词并不多,有的单词是程序员随便写的不要求是完整的英文单词()变量名可以用部分英文,拼音,数字组成。系统已有的成员方法的名字,有的英文还挺长,可以借助开发工具输入提醒功能进行自动录入,不需要你记住很长的方法名字。
4711
2019-12-04 10:56:53
Zookeeper入门学什么?
零基础入门学习Zookeeper,ZooKeeper是一种分布式协调服务,用简单的架构和API,解决了在分布式环境中协调和管理服务的难题。使用zookeeper搭建一套分布式同步锁,合实际应用揭秘现在流行的微服务架构Dubbo。
4327
2020-01-07 17:05:33
从0搭建后端技术涉及到哪些知识点?
从0搭建后端技术涉及到哪些知识点?从零开始搭建,在创业公司没有完善的基础设施,需要从开源界,从云服务商甚至有些需要自己去组合去拼装去开发一个适合自己的组件或系统以达成我们的目标.
2049
2022-05-16 15:00:59