简介

  1. Spring 是一个开源框架。
  2. Spring 是为简化企业级应用开发而生,使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。
  3. 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...
HelloWorld setName(): String

从上面的代码和控制台输出我们可以看出:在创建 Spring 的 IOC 容器时 , Spring 就已经把 HelloWorld 初始化 。首先加载无参构造器初始化 HelloWorld 对象 , 之后调用 setName() 方法给已经初始化的对象赋值。

配置文件与类的关系

Spring学习--HelloWorld的更多相关文章

  1. [原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  3. 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 ...

  4. 【Java EE 学习 49 上】【Spring学习第一天】【基本配置】

    一.HelloWorld 需要的jar文件(以2.5.5为例):spring.jar,common-logging.jar 1.新建类com.kdyzm.spring.helloworld.Hello ...

  5. Spring学习笔记(一)

    1.1.1Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 1.1.2S ...

  6. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  7. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  8. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  9. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

随机推荐

  1. thrift服务端到客户端开发简单示例

    (1)首先我们在服务器端写个helloworld.thrift文件,如下所示: service HelloWorld{ string ping(1: string name), string getp ...

  2. Linux硬盘性能检测

    对于现在的计算机来讲,整个计算机的性能主要受磁盘IO速度的影响,内存.CPU包括主板总线的速度已经很快了. 基础检测方法 1.dd命令 dd命令功能很简单,就是从一个源读取数据以bit级的形式写到一个 ...

  3. 获取.jar文件运行时所处的路径

    在Windows控制台中运行.jar文件时的两种环境: (1)控制台当前所在目录是.jar文件所在的目录 (2)控制台当前所在目录不是.jar文件所在的目录 我的期望: 我希望在上述两种环境下均可以得 ...

  4. es2017中的async和await要点

    1. async和await最关键的用途是以同步的写法实现了异步调用,是对Generator异步方法的简化和改进.使用Generator实现异步的缺点如下: 得有一个任务执行器来自动调用next() ...

  5. Spring 整合 Shiro

    一.引入依赖 <!-- spring start --> <dependency> <groupId>org.springframework</groupId ...

  6. VIN码识别:助力汽车后市场转型升级

    随着中国汽车市场的成熟,汽车后市场发展迅速,呈“井喷”式增长.据最新数据统计,2015年,中国汽车后市场产值突破8000亿规模,到2018年有望突破万亿. 所谓汽车后市场是指汽车销售以后,围绕汽车使用 ...

  7. Python 中的容器 collections

    写在之前 我们都知道 Python 中内置了许多标准的数据结构,比如列表,元组,字典等.与此同时标准库还提供了一些额外的数据结构,我们可以基于它们创建所需的新数据结构. Python 附带了一个「容器 ...

  8. CSS3 : transition 属性

    CSS3的 transition 属性用于状态过度效果! 1.语法: transition: property duration timing-function delay; -moz-transit ...

  9. 感知机学习算法(PLA)

    Perception Learning Algorithm, PLA 1.感知机 感知机是一种线性分类模型,属于判别模型. 感知机模型给出了由输入空间到输出空间的映射: f(X) = sign(WTX ...

  10. VC++之运算符重载简单小结

    封装继承和多态是面向对象三大基本支柱.在面向对象系统中有两种编译方式:静态联编和动态联编静态联编:也叫早期联编:指系统在编译时就决定如何实现某一动作,提供了执行速度快的优点.动态联编:也叫滞后联编:指 ...