spring依赖的maven dependency
http://mvnrepository.com/artifact/org.springframework

在pom.xml中添加如下依赖:

<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>

第一个例子:
首先设置一个接口Perofrmance表示参赛者。

package com.moonlit.myspring;

public interface Performer {
void perform() throws PerformanceException;
}

创建一个Juggler(杂技师)类继承Performer表示参赛者是杂技师

package com.moonlit.myspring;

public class Juggler implements Performer {
private int beanBags = 3;
public Juggler() {
}
public Juggler(int beanBags) {
this.beanBags = beanBags;
}
public void perform() throws PerformanceException {
System.out.println("JUGGLING " + beanBags + " BEANBAGS");
}
}

在spring-idol.xml配置文件中定义一个名为duke的bean,他对应Juggler类(我把xml文件放在src/main/resources目录下)

<?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-3.0.xsd"> <bean id="duke" class="com.moonlit.myspring.Juggler" /> </beans>

测试代码:

package com.moonlit.practice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.moonlit.myspring.PerformanceException;
import com.moonlit.myspring.Performer; public class FirstBean {
public static void main(String[] args) throws PerformanceException {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-idol.xml");
Performer performer = (Performer) context.getBean("duke");
performer.perform();
}
}

运行结果如下:

JUGGLING 3 BEANBAGS

理解:首先定义了一个接口Performer,然后写了一个类Juggler继承自Peformer,Juggler有一个私有成员变量beanBags,他的默认值是3,然后Juggler实现了Performer的perform方法,方法的输出带有beanBags的数量。
然后在测试的程序中,通过ApplicationContext类型对象加载了spring-idol.xml文件的内容,而在xml文件中定义了名为"duke"的bean,然后我刚好就用到了。
然后bean返回的是一个Juggler,所以我将

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

改成

Juggler performer = (Juggler) context.getBean("duke");

也是可以的,但是在这里我想看的效果是通过application context返回的是不是一个Juggler,因为通过输出的结果就可以知道了,所以我这里用(Performer),对中输出的效果显示bean对应的Performer真的是一个Juggler,这就是通过xml定义一个bean并通过application context获得这个bean对象的整个过程。

Spring学习笔记--声明一个简单的Bean的更多相关文章

  1. 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

  2. Java框架spring 学习笔记(二):Bean的作用域

    Spring 框架Bean支持以下五个作用域: 下面介绍两种作用域,singleton和protoype singleton作用域 singleton作用域为默认作用域,在同一个ioc容器内getBe ...

  3. Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)

    配置文件和注解混合使用 创建对象操作使用配置文件方式实现 注入属性的操作使用注解方式实现 编写BookDao.java和OrderDao.java文件 BookDao.java package com ...

  4. blfs(systemv版本)学习笔记-制作一个简单的桌面系统

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 大概思路: lfs(系统)+xorg(驱动)+i3-wm(窗口+桌面)+lightdm(显示管理器+登录管理器) 链接: lfs ...

  5. Oracle学习笔记:一个简单的行转列例子

    一个简单的行列转换例子,原始数据. create table temp_cwh_student ( name ), subject ), score ) ) select * from temp_cw ...

  6. Java框架spring 学习笔记(三):Bean 的生命周期

    当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态.当bean不再需要,并且从容器中移除时,需要做一些清除工作.为了定义安装和拆卸一个 bean,我们只要声明init-metho ...

  7. Java框架spring 学习笔记(五):Bean定义继承

    子 bean 的定义继承父定义的配置数据.子定义可以根据需要重写一些值,或者添加其他值. 编写HelloWorld.java package com.example.spring; public cl ...

  8. cpp学习笔记 1一个简单的小程序以及一些的知识点

    今天买的cpp到了从今天開始又一次学习cpp如今发现学校发的书真的不怎莫样. <em>#include<stdio.h>//预处理命令 int main()/*第一个被调用的函 ...

  9. Windows程序设计学习笔记(1):一个简单的windows程序

    <Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...

随机推荐

  1. RabbitMQ(二):mandatory标志的作用

    本文转自:http://m.blog.csdn.net/article/details?id=54311277 在生产者通过channel的basicPublish方法发布消息时,通常有几个参数需要设 ...

  2. [ubuntu]E: The package firmware-upgrade needs to be reinstalled, but I can't find an archive for it.

    解决办法把firmware-upgrade卸载 sudo dpkg --remove --force-all firmware-upgrade 然后 sudo apt-get update 即可

  3. 非分离线程未使用join函数例子:

    //非分离线程未使用join函数例子: #include<stdlib.h> #include<pthread.h> #include<stdio.h> #incl ...

  4. linux下ppp拨号无线上网

    linux下用ppp上网需要两个程序:pppd和chat.ubuntu自带pppd和chat,可以使用man查看具体使用方法. 典型的ppp拨号需要准备几个文件: 1. pppd脚本. 2. chat ...

  5. Maven_POM配置详解

    本文转载,方便以后查阅,转载地址:http://blog.csdn.net/ithomer/article/details/9332071 <project xmlns="http:/ ...

  6. [ES6]探究数据绑定之Proxy

    知识储备 Proxy 方式实现数据绑定中涉及到 Proxy.Reflect.Set.Map 和 WeakMap,这些都是 ES6 的新特性. Proxy Proxy 对象代理,在目标对象之前架设一层拦 ...

  7. 记录github 免登陆用户名密码方式

    1.https 代码模式切换为ssh模式: (本博客有文章介绍) 2.~/.ssh/github_rsa.pub 内容添加到github “config“ 目录下面 3.配置~/.ssh/config ...

  8. laravel bald视图控制流与子视图

    1:laravel 视图控制流的写法 假设控制器代码如下 $data = [ 0 => '张三', 1 => '李四', 2 => '王五' ]; return view('test ...

  9. 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope

    解决办法 出现错误:jni/ffmpeg/libavutil/common.h:175:47: error: 'UINT64_C' was not declared in this scope 解决: ...

  10. 转载:Erlang 函数(Efficiency Guide)

    转自:http://www.cnblogs.com/futuredo/archive/2012/10/26/2737644.html Functions 1  Pattern matching 模式匹 ...