Spring基础——一个简单的例子
一、学习版本 spring-framework-4.0.0
二、导入 jar 包:

三、在类路径下创建 Spring Config 文件:ApplicationContext.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.xsd">
</beans>
四、创建一个非侵入的 Bean
/**
* @author solverpeng
* @create 2016-07-15-10:09
*/
public class HelloWorld {
private String userName; public void setUserName(String userName) {
this.userName = userName;
} public void hello() {
System.out.println("hello:" + userName);
}
}
五、在 Spring Config 文件中配置该 Bean
<?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.xsd">
<bean id="helloWorld" class="com.nucsoft.spring.helloworld.HelloWorld">
<property name="userName" value="spring"/>
</bean>
</beans>
六、通过 IOC 容器对象来获取在 Spring Config 文件中配置的 Bean,并调用其方法
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml");
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.hello(); // 输出 hello:spring
}
七、总结
两个重点:
1.在 Spring Config 中配置 Bean
2.通过 IOC 容器中获取 Sring Config 配置的 Bean 对象
Spring基础——一个简单的例子的更多相关文章
- SpringMVC基础——一个简单的例子
一.导入 jar 包 二.配置 web.xml 文件 <servlet> <servlet-name>dispatcherServlet</servlet-name> ...
- Spring-Context之一:一个简单的例子
很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...
- 关于apriori算法的一个简单的例子
apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...
- 从一个简单的例子谈谈package与import机制
转,原文:http://annie09.iteye.com/blog/469997 http://blog.csdn.net/gdsy/article/details/398072 这两篇我也不知道到 ...
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- 扩展Python模块系列(二)----一个简单的例子
本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...
- fitnesse - 一个简单的例子(slim)
fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行 2.1 新建wikiPage 2.2 运行 ...
- Struts2的配置和一个简单的例子
Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...
- 一个简单的例子搞懂ES6之Promise
ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...
随机推荐
- ELK——在 CentOS/Linux 把 Kibana 3.0 部署在 Nginx 1.9.12
上一篇文章<安装 logstash 2.2.0.elasticsearch 2.2.0 和 Kibana 3.0>,介绍了如何安装 Logstash.Elasticsearch 以及用 P ...
- Linux所有者和组
组 添加组 groupadd 组名 查看所有组 cat /etc/group 添加成员useradd -g 组名 用户名 查看成员 cat /etc/passwd 查看用户权限 ls -l d rw ...
- Navi.Soft30.开放平台.百度.开发手册
1系统简介 1.1功能简述 现在是一个信息时代,并且正在高速发展.以前获取信息的途径非常少,可能只有电视台,收音机等有限的来源,而现在的途径数不胜数,如:QQ,微信,官方网站,个人网站等等 本开发手册 ...
- SAP-GR/IR的理解
SAP-GR/IR的理解 http://shousitukyou.blog.163.com/blog/static/13868005820109127046318/ GR/IR 1,采购的an i ...
- C#后台弹出对话框
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='jav ...
- [Javascript] Decorators in JavaScript
First, what is 'High Order function', basic just a function, inside the function return another fuct ...
- C#排序比较
与C#定义了相等性比较规范一样,C#也定义了排序比较规范,以确定一个对象与另一个对象的先后顺序.排序规范如下 IComparable接口(包括IComparable接口和IComparable< ...
- .Net事件管道详解图
- hexdump—Linux系统的二进制文件查看工具
hexdump 无参: 相当于 hexdump -x 0000000 457f 464c 0102 0001 0000 0000 0000 0000 0000010 0002 003e 0001 00 ...
- 使用Condition实现多线程之间调用(生产消费模式)
一,object 类的wait(),notify()和notifyAll() Java 线程类也是一个object 类,它的实例都继承自java.lang.Thread 或其子类.wait(),not ...