简介

  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. 【js笔记】数组那些事[0]

    js中数组是一个特殊的对象,索引是它的属性,整数索引在内部被转化为字符串类型. 1 数组的创建 new关键字方法:var arr=new Array() var arr=new Array(10); ...

  2. vue循环绑定v-model

    直接上代码 结构: <repayInput v-if="formData" v-for="(item, index) in formData" :isPw ...

  3. 汇编指令MOVSX与MOVZX

    MOVSX 操作数A ,操作数B MOVZX 操作数A ,操作数B 相同点:操作数B 空间必须小于 操作数A 1.格式与MOV基本相同 2.能完成小存储单元向大存储单元的数据传送 比如 movsx e ...

  4. 树莓派初次使用的基本配置.md

    记录了一下树莓派初次使用的配置过程,包括装系统.修改 IP 等等. 树莓派(英语:Raspberry Pi),是一款基于 Linux 的单板机电脑. 它由英国的树莓派基金会所开发,目的是以低价硬件及自 ...

  5. BZOJ 1010 HNOI2008 玩具装箱 斜率优化

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1010 Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的 ...

  6. 学习bash——管道命令

    摘要:管道命令概述.常见管道命令的使用(cut/grep.sort/wc/uniq.tee.tr/col/join/paste/expand.xargs.减号-) 一.概述 命令执行完会在屏幕上打印相 ...

  7. 点击查看大图Activity

    1.使用方式 Intent intent = new Intent(FriendCircleActivity.this, ImageGralleryPagerActivity.class);//0,索 ...

  8. pta指针作业

    #PTA实验作业 6-1 本题pta提交列表 设计思路 本题是一道简单的指针程序题,两个数已经分别被指针定义,只要把用其指针把二者加在一起和减去即可 调试过程 本题无调试过程 代码截图 6-2  1. ...

  9. 【EasyNetQ】- 使用SSL连接

    EasyNetQ可以通过SSL连接.戈登·库尔特(Gordon Coulter)撰写的这本指南最初是针对一个提出的问题而写的. 首先,您必须仔细按照https://www.rabbitmq.com/s ...

  10. red入门学习笔记

    删除以name开头的所有键值. 查找开头和结尾相同,中间字符不同