简介

  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. 浅析express以及express中间件

    一.express: 1.express: Express是什么? Express是基于node.js平台的web应用开发框架: 作用:可以实现快速搭建骨架: 优点:开发web应用更加方便,更加快捷. ...

  2. javascript对象转为字符串

    function getStringTime(time){ //年 year = time.getFullYear(); //月 month = time.getMonth() if(String(m ...

  3. 台湾ML笔记--1.1什么时候适合使用ML

    适用情况: 1 exists some 'underlying pattern' to be learned --so 'performance measure' can be imporoved 例 ...

  4. 问题:docker pull 用户登陆tricky,Error response from daemon: unauthorized: incorrect username or password

    问题描述: PS C:\WINDOWS\system32> docker pull rabbitmqUsing default tag: latest Please login prior to ...

  5. 类和实例属性的查找顺序 mro查找

    如果多个类继承父类,然后又被多个类继承这种复杂的问题,可以使用 mro方法 例如: class A: pass class C(D): pass class B(D): pass class A(B, ...

  6. 【集训试题】SiriusRen的卡牌 set

    题意概述: 给出N张卡牌,每张有三个属性a,b,c,同时给出所有属性可能的最大值A,B,C.对于一张卡牌,当这张卡牌至少有两个属性大于另外一张卡牌的对应两个属性的时候,认为这张卡牌更加优秀.现在问有多 ...

  7. JSONP跨域jQuery处理整理(附天气数据实例)

    写在前面 跨域的解决方案有多种,其中最常见的是使用同一服务器下的代理来获取远端数据,再通过ajax进行读取,而在这期间经过了两次请求过程,使得获取数据的效率大大降低,这篇文章蓝飞就为大家介绍一下解决跨 ...

  8. JavaWeb 基于Session的用户登陆注销实现

    通过Session来存储用户的部分登陆信息来验证用户是否在线,这应该时最容易实现的一种Web端方案,本文以SSM(Spring.SpringMVC.myBatis)框架为载体,来具体实现这套登陆系统. ...

  9. Python创建目录文件夹

    Python对文件的操作还算是方便的,只需要包含os模块进来,使用相关函数即可实现目录的创建. 主要涉及到三个函数 1.os.path.exists(path) 判断一个目录是否存在 2.os.mak ...

  10. 算法(5)Jump Game

    题目:非负数的数组,每个数组元素代表这你能最大跨越多少步,初始在0的位置,问,能不能正好调到数组的最后一位! https://leetcode.com/problems/jump-game/#/des ...