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中的异步 ... 
随机推荐
- Android源码分析-全面理解Context
			前言 Context在android中的作用不言而喻,当我们访问当前应用的资源,启动一个新的activity的时候都需要提供Context,而这个Context到底是什么呢,这个问题好像很好回答又好像 ... 
- webService发布和调用--Axis2
			一.工具 1.下载 Axis2以及eclipse的Axis2插件.http://axis.apache.org/axis2/java/core/download.cgi 2.axis2-1.7.1-w ... 
- Win7 安装Apache 2.2.4报错:<OS 5>拒绝访问. :Failed to open the WinNT service manager
			Apache 2.2安装档与win7的“用户账户控制”冲突,所以只要先关闭,安装完后再开启即可. 重启,然后再重新安装Apache2.2.4,没有出现错误.然后再把上述设置改回去. 参考:http:/ ... 
- CGI、FastCGI 知识总结
			@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ... 
- juniper 550M訪问自身公网IP回流内部IP
			拓扑图示意: 网关设备juniper 550M, untrust 区: 公网地址段22.22.22.22/29 trust区: 内部员工PC地址:172.16.4.x /24 trust区: ... 
- 使用Condition实现多线程之间调用(生产消费模式)
			一,object 类的wait(),notify()和notifyAll() Java 线程类也是一个object 类,它的实例都继承自java.lang.Thread 或其子类.wait(),not ... 
- 如何通过XShell传输文件
			转载孟光孟叔的博客: https://learndevops.cn/index.php/2016/06/14/how-to-transfer-file-using-xshell xshell目前最好 ... 
- 8个经典HTML5 3D动画赏析
			HTML5技术已经越来越被我们所接受,特别是一些3D的动画特效.本文介绍的8个HTML5 3D动画并没有特别华丽的界面,但是比较实用,涉及到3D图片.3D图表.3D按钮等方面,一起来看看. 1.HTM ... 
- Android-NDK编译:cocos2d-x(三) eclipse 导入工程
			NDK 编译后,用eclipse导入cocos2d-x工程 菜单[File]-->[New]-->[Project] ,弹出New Project 对话框 窗口下方 选 [Android] ... 
- Linux Unix 环境变量设置实例
			背景 从第一次写Hello World我们便开始接触环境变量.这最基础的系统设置是必须要掌握的,尤其在是Linux/Unix系统中.比如,哪天某个Java进程出现问题,我们想分析一下其线程堆栈,却发现 ... 
