今天开始学习spring,每天都会将自己学习的一些内容,或是一些总结以博客的形式记录下来,方便自己以后回顾,如果能给他人学习带来丁点的帮助那也是最好不过了。本系列博文的spring学习是基于4.0版本。

  spring是什么?spring是一个开源框架,spring为简化企业级应用开发而生,使用spring可以使简单的javabean实现以前只有EJB才能实现的功能。Spring是一个ioc,aop容器框架。spring用于配置bean,并维护bean与bean之间关系的框架。

  spring的模块

  

  spring简单案例

    1,创建一个javaweb工程,目录结构如下:

    

    2,导入spring相关的jar包文件

      

    3,创建一个简单的实体类

    

package com.spring.beans;
public class Student {
  private String name;
  private Integer age;
  private String email;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  @Override
  public String toString() {
    return "Student [name=" + name + ", age=" + age + ", email=" + email + "]";
  }   public void sayHello(){
    System.out.println("hello "+name);
  }
}

    4,配置相关的spring.xml文件

    


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  <!-- 配置一个bean -->
  <!-- 配置的bean需要在beans标签中配置,id属性是配置的bean的唯一标识符 class表示当前配置的bean对应的java类 -->
  <bean id="student" class="com.spring.beans.Student">
    <!-- porperty表示为配置的bean注入属性值,name属性的值对应实体类中的属性,value表示你想要注入的值 -->
    <property name="name" value="onsim" />
    <property name="age" value="4" />
    <property name="email" value="onsim@163.com" />
  </bean>

</beans>


    5,创建一个测试类

    

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

package com.spring.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.spring.beans.Student;


public class Test {


@SuppressWarnings("resource")
public static void main(String[] args) {
  //传统方式
  /*Student student = new Student();
  student.setName("assllon");
  student.setAge(25);
  student.setEmail("assllon@qq.com");
  System.out.println(student);
  student.sayHello();*/
  //利用spring的方式
  //获取spring的核心容器
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
  //根据beanid获取bean实例
  Student student = (Student) applicationContext.getBean("student");
  System.out.println(student);
  student.sayHello();
  }

}

  6,运行main方法得到相应的结果。

    

  这样一个简单的spring版本的helloworld小程序就完成了。

spring初体验 一之helloworld的更多相关文章

  1. (一)SpringBoot基础篇- 介绍及HelloWorld初体验

    1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...

  2. (一)SpringBoot2.0基础篇- 介绍及HelloWorld初体验

    1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...

  3. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  4. iOS7初体验(1)——第一个应用程序HelloWorld

    iOS7 Beta已经发布了,迫不及待地下载了iOS 7及Xcode 5并体验了一下.先做一个简单的Hello World看看都有哪些变化吧. 1. 启动Xcode5-DP: 2. 从菜单选择File ...

  5. spring cloud 初体验

    spring cloud分为注册端.客户端以及消费端 初体验的理解就是: 注册端就是将之前所有的应用在这边进行注册,然后给每个应用都生成自己的标识,这些应用就是来自于客户端,消费端则通过调用注册端(有 ...

  6. 215.Spring Boot+Spring Security:初体验

    [视频&交流平台] SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http://t.cn/R3QeRZc SpringBoot Shiro视频 ...

  7. Spring JDBCTemplate连接SQL Server之初体验

    前言 在没有任何框架的帮助下我们操作数据库都是用jdbc,耗时耗力,那么有了Spring,我们则不用重复造轮子了,先来试试Spring JDBC增删改查,其中关键就是构造JdbcTemplate类. ...

  8. Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用[z]

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

  9. SpringCloud初体验:五、Sidecar 将 PHP 这类非 Java 生态语言的服务接入 Spring Cloud

    先起一个 Sidecar 服务,一个PHP服务一个应用,和PHP服务部署在同一台机子,通过 localhost 访问,这样就解决了网络开销,相当于本地进程间调用 Sidecar 服务比较简单, 1.这 ...

随机推荐

  1. Thinkphp路由配置和静态缓存规则【原创】

    ThinkPHP框架对URL有一定的规范,所以如果你希望定制你的URL格式的话,就需要好好了解下内置的路由功能了,它能让你的URL变得更简洁和有文化. 首先我们在Common/config.php设置 ...

  2. GC垃圾回收器

    java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”.jvm解决的两个问题:给对象分配内存以及回收分配给对象的内存.GC:将内存中不再被使用的对象进行回收.GC的作用域是JVM运行时 ...

  3. GIT 身份验证失败问题解决方案,由于修改密码产生的问题

    fatal: Authentication failed for 'http:xxxxxxxxxx.git/' 解决方案 1. git config --global user.name " ...

  4. LeetCode 237 Delete Node in a Linked List 解题报告

    题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access ...

  5. luogu2839 [国家集训队]middle

    题目链接:洛谷 题目大意:给定一个长度为$n$的序列,每次询问左端点在$[a,b]$,右端点在$[c,d]$的所有子区间的中位数的最大值.(强制在线) 这里的中位数定义为,对于一个长度为$n$的序列排 ...

  6. bugfree3.0.1-BUG解决方案修改

    该篇内容来自文档“masterBugFree2.pdf”,记录在这里. 1.如何将解决方案改为中文 在\Bugfree\Lang\ZH_CN_UTF-8 \_COMMON.php 文件中做如下修改/* ...

  7. Django进阶之查询优化、extra注入SQL及批量创建

    Django查询优化 Django的查询优化用到两个函数——select_related()和prefetch_related(). select_related()用的是连表join的方式,主要处理 ...

  8. java框架之SpringCloud(3)-Eureka服务注册与发现

    在上一章节完成了一个简单的微服务案例,下面就通过在这个案例的基础上集成 Eureka 来学习 Eureka. 介绍 概述 Eureka 是 Netflix 的一个子模块,也是核心模块之一.Eureka ...

  9. Jenkins Pipeline脚本

    node { echo 'Hello World' } node 在Jenkins环境中分配一个执行器和工作空间. echo 在控制台输出中写入简单的字符串 try { timeout(time: 1 ...

  10. node.js (01http 模块 url 模块)

    // 引入 http 模块-->Node.js 中的很多功能都是通过模块实现. var http = require('http'); // http.createServer() 方法创建服务 ...