Spring学习--HelloWorld
简介
- Spring 是一个开源框架。
- Spring 是为简化企业级应用开发而生,使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。
- Spring 是一个 IOC 和 AOP 容器框架。
Spring 框架图

代码演练
下面我们开始 Spring 第一个经典项目:HelloWorld
普通模式的Helloworld:
package com.itdjx.spring.beans; /**
* @author Wáng Chéng Dá
* @create 2017-02-28 10:38
*/
public class HelloWorld { private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void hello() {
System.out.println("hello " + this.getName());
}
}
package com.itdjx.spring.beans; /**
* @author Wáng Chéng Dá
* @create 2017-02-28 10:39
*/
public class Main { public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("itdx");
helloWorld.hello(); }
}
控制台输出:
| hello itdx |
Spring 模式下的HelloWorld:
<?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-->
<bean id="helloWorld" class="com.itdjx.spring.beans.HelloWorld">
<property name="name" value="String"/>
</bean>
</beans>
package com.itdjx.spring.beans; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-02-28 10:39
*/
public class Main { public static void main(String[] args) { //Spring模式下
/**
* 1.创建 Spring 的 IOC 容器
* 2.从 IOC 容器中获取 Bean 实例
*/
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.hello(); }
}
控制台输出:
| hello String |
通过控制台输出可以看出,HelloWorld 中那么属性的值在配置文件 <property name="name" value="String"/> 中就已经被赋好了,所以 HelloWorld 中 name 属性值对应 value 的值。
若是修改 Helloworld 中 setName 为 SetName1 的话,配置文件 <property name="name" value="String"/> 中的 name 属性值就得需要改成 name="name1"。
下面我们更直观的看一下Spring框架的加载流程:
package com.itdjx.spring.beans; /**
* @author Wáng Chéng Dá
* @create 2017-02-28 10:38
*/
public class HelloWorld { private String name; public String getName() {
return name;
} public void setName(String name) {
System.out.println("HelloWorld setName(): " + name);
this.name = name;
} public void hello() {
System.out.println("hello " + this.getName());
} public HelloWorld() {
System.out.println("HelloWorld Constructor...");
}
}
package com.itdjx.spring.beans; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-02-28 10:39
*/
public class Main { public static void main(String[] args) { //Spring模式下
/**
* 1.创建 Spring 的 IOC 容器
* 2.从 IOC 容器中获取 Bean 实例
*/
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
// helloWorld.hello(); }
}
控制台输出:
|
HelloWorld Constructor... |
从上面的代码和控制台输出我们可以看出:在创建 Spring 的 IOC 容器时 , Spring 就已经把 HelloWorld 初始化 。首先加载无参构造器初始化 HelloWorld 对象 , 之后调用 setName() 方法给已经初始化的对象赋值。
配置文件与类的关系

Spring学习--HelloWorld的更多相关文章
- [原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- Spring学习1:Spring基本特性
http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC ...
- 【Java EE 学习 49 上】【Spring学习第一天】【基本配置】
一.HelloWorld 需要的jar文件(以2.5.5为例):spring.jar,common-logging.jar 1.新建类com.kdyzm.spring.helloworld.Hello ...
- Spring学习笔记(一)
1.1.1Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 1.1.2S ...
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- MyEclipse Spring 学习总结三 SpringMVC
MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...
- Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...
随机推荐
- 笔记-twisted-adbapi-scrapy
笔记-twisted-adbapi-scrapy-mysql 1. 异步插入mysql 在爬虫中需要insert到mysql,但有一个问题是在爬虫环境中commit的及时性与性能冲突. 一般 ...
- bootstrap重新设计checkbox样式
文章采集于: https://www.cnblogs.com/GumpYan/p/7845445.html#undefined 在原文基础上修改了勾勾的内容,直接采用bootstrap字体库.修改了横 ...
- 一些可能有点用处的C#开发经验
前言: 下个月就要去进行Java开发了,以后C#碰的就少了(可惜去年买了三本C#的书,几乎还是全新的……),平时一些经验都记在OneNote里面,现在收集整理出来,因为只能利用交接工作的打酱油的时间, ...
- pytest 测试报告
测试报告 运行测试用例后,为了保存结果,我们需要生成测试报告,同时可以把运行的测试报告发送相关人员查阅,这时需要安装一个插件(pytest-html) pytest-html插件安装 pip inst ...
- Django数据模型--表关系(一对多)
一.一对一关系 使用方法:models.ForeignKey(要关联的模型) 举例说明:年级.教师和学生 from django.db import models class Grade(models ...
- vs code 代码格式化整理
vs code格式化代码的快捷键如下:(来源于这里) On Windows Shift + Alt + F On Mac Shift + Option + F On Ubuntu Ctrl + Shi ...
- Spring实战第七章————SpringMVC配置的替代方案
SpringMVC配置的替代方案 自定义DispatherServlet配置 我们之前在SpittrWebAppInitializer所编写的三个方法仅仅是必须要重载的abstract方法.但还有更多 ...
- 九度OJ--Q1167
import java.util.Scanner;import java.util.TreeSet; /* * 题目描述: * 输入一个数组的值,求出各个值从小到大排序后的次序. * 输入: * 输入 ...
- python基础训练营01
一.基础讲解: 1.1 文件末尾的.py后缀,指出这个文件,是一个python文件,因此,系统将使用python解释器来运行该文件,确定文件中每一个单词的含义. 1.2 python编辑/运行方法: ...
- mysql ibd 文件还原数据
-- 这里要还原的表名为 test_table -- 1建库,并选中库,库名随意 -- 2查看InnoDB 引擎独立表空间是否开启 SHOW VARIABLES LIKE '%per_table%' ...