简介

  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. Spring 的好处?

    1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监 ...

  2. 使用maven插件生成grpc所需要的Java代码

    1.首先需要编写自己需要的.proto文件,本文重点不在这里,.proto可以参考grpc官方例子 https://grpc.io/docs/quickstart/java.html 2.创建自己的J ...

  3. Notepad++删除空行的多种实现办法

    Notepad++支持基础的正则表达式,同时由于自身丰富的插件和功能,所以删除空行或有空格的空行,有多种实现办法,条条大路通罗马,闪电博客抛砖引玉,供大家参考. 一.删除空行(不包括有空格类符号的空行 ...

  4. 两个完整的jquery slide多方面滑动效果实例

    实例1,需要引用jquery-ui.js <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  5. hihoCoder 1175:拓扑排序二

    题目链接: http://hihocoder.com/problemset/problem/1175 题目难度:一星级(简单题) 今天闲来无事,决定刷一道水题.结果发现这道水题居然把我卡了将近一个钟头 ...

  6. java集合浅谈(一)

    一.类库结构图概览 容器对象仅能持有对象引用(对象的指针),而不是Copy对象信息,从网上搜得几张Java中集合类库的结构图,如下所示: 二.解说Collection 2.1 Collection ( ...

  7. 深度可分卷积(Depthwise Separable Conv.)计算量分析

    上次读到深度可分卷积还是去年暑假,各种细节都有些忘了.记录一下,特别是计算量的分析过程. 1. 标准卷积和深度可分卷积 标准卷积(MobileNet论文中称为Standard Convolution, ...

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

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

  9. 用express搭建一个简单的博客系统

    转自:https://blog.csdn.net/qq_29721837/article/details/62055603 Express 简介 Express 是一个简洁而灵活的 node.js W ...

  10. PHP中的6种加密方式

    PHP中的6种加密方式 1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str  --  原始字符串. raw ...