Spring的HelloWorld
①创建Java工程
②添加Spring包到CLASSPATH中,主要的jar包包括
commons-logging-1.1.3.jar (Spring的jar依赖该包)
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
③创建bean类Book(eclipse中ctrl+o查看类的成员)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.dcx.spring.bean; public class Book { private Integer id; private String name; private String author; private double price; public Book() { super (); } public Book(Integer id, String name, String author, double price) { super (); this .id = id; this .name = name; this .author = author; this .price = price; } public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this .author = author; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + "]" ; } } |
④为Book创建Spring的bean配置文件,该文件放置在项目的src文件夹下,项目结构如下
beans.xml文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 | <?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 id= "book" class = "com.dcx.spring.bean.Book" > <property name= "id" value= "1" /> <property name= "name" value= "活着" /> <property name= "author" value= "余华" /> <property name= "price" value= "15.8" /> </bean> </beans> |
⑤建立测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package junit.test; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dcx.spring.bean.Book; public class TestIOC { private ApplicationContext ioc; @Before public void setUp(){ //通过加载XML文件来创建ioc容器 ioc = new ClassPathXmlApplicationContext( "beans.xml" ); } @After public void tearDown(){<br> //关闭ioc AbstractApplicationContext acc = (AbstractApplicationContext)ioc; acc.close(); } @Test public void test01() { //获取bean对象 Book book = (Book)ioc.getBean( "book" ); System.out.println(book); } } |
⑥查看测试结果,成功获取bean对象
分类:
JAVA
标签:
spring入门项目
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞