博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1. 我的第一个Spring程序
阅读量:3971 次
发布时间:2019-05-24

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

我的第一个Spring程序

文件目录

在这里插入图片描述

1.导入jar包,这里我们使用maven导入

org.springframework
spring-webmvc
5.2.0.RELEASE
junit
junit
4.12
org.aspectj
aspectjweaver
1.9.4
src/main/resources
**/*.properties
**/*.xml
false
src/main/java
**/*.properties
**/*.xml
false

2.编写实体类(这个类就一个属性)

package com.qin.pojo;public class HelloSpring {
private String msg; //使用set方法 public void setMsg(String msg) {
this.msg = msg; } public String getMsg() {
System.out.println("my msg:"+msg); return msg; }}

3.编写application-spring文件(也就是beans文件)他是spring的核心配置文件

4.编写测试文件(HelloSpringTest)

package com.qin.pojo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloSpringTest {
public static void main(String[] args) {
//我们需要使用ClassPathXmlApplicationContext类,来帮我们找到Spring的核心配置文件 ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("application-spring.xml"); //通过getBean方法来获取bean的id 来找到具体的类 helloSpring就是bean的id HelloSpring helloSpring = cpx.getBean("helloSpring", HelloSpring.class); //helloSpring就相当于是被HelloSpring类new 出来的对象,只不过spring帮我们做了 //所以我们可以使用HelloSpring类的getMsg()方法 helloSpring.getMsg(); }}

结果

在这里插入图片描述

转载地址:http://qhnki.baihongyu.com/

你可能感兴趣的文章
Flutter UI基础 - AppBar中标题文字如何居中
查看>>
Flutter UI基础 - Drawer 抽屉视图与自定义header
查看>>
Flutter UI基础 - GridView
查看>>
Flutter UI基础 - 使用InkWell给任意Widget添加点击事件
查看>>
OC WKWebView的使用
查看>>
Flutter UI基础 - Image.asset 图片铺满布局
查看>>
Flutter UI基础 - Row、Column详解
查看>>
Flutter UI基础 - 添加背景图片
查看>>
Flutter UI基础 - 布局之Row/Column/Stack
查看>>
Flutter UI基础 - 层叠布局Stack的使用
查看>>
Go - 解决 go get 超时问题
查看>>
SQL - SQL Server 之遍历数据集合的几种方法
查看>>
SQL - SQL Server 之处理JSON数据
查看>>
SQL - SQL Server 之WHILE循环的坑
查看>>
SQL - SQL Server 性能优化之SQL语句总结
查看>>
Docker - docker-compose常用命令
查看>>
SQL - SQL Server判断字符串中是否有中文
查看>>
SQL - SQL Server查询近7天的连续日期
查看>>
SQL - SQL Server中如何取年、月、日 -DATEPART函数
查看>>
SQL - SQL Server 一列或多列重复数据的查询,删除
查看>>