博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分分钟教会大家第一个Spring入门案例
阅读量:4684 次
发布时间:2019-06-09

本文共 1148 字,大约阅读时间需要 3 分钟。

  1.下载Spring jar包,并添加到项目中。

    官网地址http:springsource.org 

  

2.在项目中新建一个类

    

package cn.test;public class HelloSpring {// 定义属性,该属性的值将通过Spring框架进行设置private String result;/** * 定义一个打印的方法用于输出result的值 */public void print() {System.out.println("Hello" + getResult() + "!"); }public HelloSpring() {super();// TODO Auto-generated constructor stub}public String getResult() {return result;}public void setResult(String result) {this.result = result;}}

 

 3.编写Spring配置文件,在src目录下创建applicationContext.xml文件配置如下

 

Heyongjun

 

4.书写测试类

 

package cn.test; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {  public static void main(String[] args) {  //通过ClassPathXmlApplicationcontext实例化Spring的上下文  ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  //通过Applicationcontext中的getBean()方法,根据id来获取Bean实例 HelloSpring hs= (HelloSpring)context.getBean("hellpSpring"); hs.print();  }}

 

转载于:https://www.cnblogs.com/hyjj/p/5880951.html

你可能感兴趣的文章
lesson1 预备知识
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
23 Java学习之RandomAccessFile
查看>>
SSH远程会话管理工具 - screen使用教程
查看>>
hibernate validation HV000030: No validator could be found for constraint
查看>>
Telink MESH SDK 如何使用PWM
查看>>
LR SP PC
查看>>
C# 图片识别(支持21种语言)【转】
查看>>
jQuery基础教程
查看>>
P2709 小B的询问
查看>>
第三组的抓包作业
查看>>
ILNumerics项目的应用之线性方程
查看>>
django考点
查看>>
python-socket
查看>>
python day2 模块初识、pyc定义
查看>>
基础数据结构
查看>>
WebApi2官网学习记录---单元测试
查看>>
Backbone.js源码分析(珍藏版)
查看>>
完全背包问题
查看>>