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中的异步 ...
随机推荐
- ImageEdit 展示图片(XAML, C#)
<dxe:ImageEdit Source="/Gemr;component/Images/FakeUI/MedicalRecordFake.jpg" Stretch=&qu ...
- Qt txt文本中获取字符串的问题
QT对txt文本进行读写一般是采用QFile和QTextStream结合使用,在此不细说,主要说一下读取txt文本的注意事项.因为txt文本中有中文也有英文还有数字,要准确获得需要的字串可就要多一个心 ...
- Struts2中的 配置文件
struts2中涉及到的配置文件有: web.xml.struts.xml.struts.properties.default.properties.struts-default.xml web.xm ...
- android OOM分析工具LeakCanary
http://my.oschina.net/u/255456/blog/523659?fromerr=oGosxKBf LeakCanary 只是探测到可能出现内存泄露,然后dump 一个java h ...
- spring mvc jsp运行不起来的问题
spring mvc已经处理成让jsp运行,即: <bean class="org.springframework.web.servlet.view.InternalResourceV ...
- [GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- Cocos2d-JS项目之一:环境(IDE 运行js-tests、IDE 和 studio 统一工程)
环境:cocos 引擎(包括 studio)2.2.1 for Mac.cocos2d-js-v3.5.Cocos Code IDE 1.2 for Mac,cocos 引擎指的是下面这个东西: 各种 ...
- WinStore之Application Data
一.Application Data简介 Applicaion Data相当于桌面应用的注册表,存储一些用户配置信息,如运行时状态,用户喜好等,需要注意的时,当卸载应用时,这些数据会被删除,所以不要存 ...
- LocalBroadcastManager 使用小解
最近在开发平板项目,完全是fragmentactivity+fragment的结构.看起来似乎简单,但是和以前不同的是,业务逻辑非常复杂,多处的非常规跳转,fragment之间的数据交换,一处更新多处 ...