一起学spring--我的第一个Spring程序,简单粗暴易懂
来源:互联网 发布:什么是淘宝客返利网 编辑:程序博客网 时间:2024/05/17 07:11
欢迎来《一起下spring》系列博文第一篇
一、首先让我们来做一个对比:不使用spring和使用spring的区别
1、首先是不使用spring的情况:
创建一个Student对象,里面只有一个方法,用于打印信息。
package com.huai.first;public class Student {public void printStudent(){System.out.println("hello, I am a student");}}
接下来我想写一个main函数,实例化这个Student,并执行这个方法。
package com.huai.first;public class MainTest {public static void main(String[] args) {Student stu = new Student();stu.printStudent();}}
这就是不使用spring的情况。那如果使用了spring框架了呢?请接着往下面看:
2、使用spring的情况
同样地,我们创建一个Student类,代码和上面一样。
接下来,我同样要写一个main函数来执行Student对象中的方法。应该怎么写?我先贴出代码:
package com.huai.first;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class FirstSpringTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Student stu = context.getBean(Student.class);stu.printStudent();}}
分析:
(1)、创建一个spring容器
(2)、从spring容器中获取stu对象。
我们来看一下上面这段代码,发现并没有new Student();的操作,没有实例化Student类的对象?怎么可以这样呢?具体怎么回事?原来、、、、、、
===》spring容器帮我们实例化了student对象
所以我们只需要从spring容器中获取student对象就可以了,也就是对应这条语句:
Student stu = context.getBean(Student.class);
好,我们“获得”了student对象,(这里我用‘获得’,而不是‘创建’),那么,我们就可以调用这个student对象的方法了。
为了使得这段代码能够跑起来,我们还需要进行其他两步准备工作:1、添加jar包;2、填写spring的xml配置文件。
1、添加spring的jar包到WEB-INF下的lib文件夹下面:(本博文最下面有下载的链接,粘贴后别忘了右键--build path -- add to build path)
(1)、commons-logging
(2)、spring-aop
(3)、spring-beans
(4)、spring-context
(5)、spring-context-support
(6)、spring-core
(7)、spring-expression
2、增加配置文件:
在src文件夹下面增加applicationContext.xml文件:(重点在于这一句配置语句<bean class="com.huai.first.Student"/>)
这条配置语句的作用是:告诉spring容器,我需要你帮我实例化这个Student对象;(spring容器回答说:没问题,你到时候直接来取这个‘对象’就可以了)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><bean class="com.huai.first.Student"/></beans>
jar包的下载链接
http://download.csdn.net/detail/liangyihuai/9155295
- 一起学spring--我的第一个Spring程序,简单粗暴易懂
- 一起学spring--依赖注入---简单粗暴的例子展示
- Spring -- 第一个简单的Spring程序
- 我的第一个Spring程序
- 我的第一个spring程序
- 一起学JAVA之《spring boot》02-开发您的第一个Spring Boot应用程序
- Spring的第一个程序
- Spring的第一个程序
- 简单易懂的spring例子
- 第一个Spring程序
- Spring第一个程序
- 第一个spring程序
- 第一个spring程序
- 第一个Spring程序
- Spring第一个程序
- Spring 第一个程序
- 第一个Spring程序
- 第一个Spring程序
- 设计模式之观察者模式
- 基于AM335x的U-Boot/SPL 的CCS 调试
- HDU 1166 敌兵布阵(树状数组)
- 传值 音频
- C/C++static、const详解
- 一起学spring--我的第一个Spring程序,简单粗暴易懂
- POJ 3902 The Bad Number(贪心)
- Linux写时拷贝技术(copy-on-write)
- 聊天界面,frame模型
- DirectX11 创建混合状态
- [231]Power of Two
- 【UML】对象图Object diagram
- 解释性语言和编译性语言的区别
- 剑指offer—二叉搜索树与双向链表