Eclipse中创建Spring项目的步骤
1.创建一个动态网站项目
2.添加Spring框架的jar包
链接:https://pan.baidu.com/s/1AjhcveNCVokizsrrF_-YLA
提取码:gza8
3.创建一个实体类Stutent
package com.spring; public class Student { private String name; private String number; private String major; private String school; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
4.编写Spring的配置文件,该配置文件模板可以从Spring的参考手册或Spring的例子中得到,用来进行bean的配置,文件名可以自定义,一般默认为applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置bean信息 --> <bean id="student123" class="com.spring.Student">
<!--值的注入是通过setName()方法进行注入的,也叫做数据装配--> <property name="name" value="张三"></property> <property name="number" value="123456789"></property> <property name="major" value="计算机科学与技术"></property> <property name="school" value="XXXX职业技术学院"></property> </bean> </beans>
5.创建测试类Test
package com.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
//获取Ioc容器,读取配置文件,初始化spring上下文 ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml"); //根据id获取容器中的bean Student stu= (Student) aContext.getBean("student123"); System.out.println("name="+stu.getName()); System.out.println("number="+stu.getNumber()); System.out.println("major="+stu.getMajor()); System.out.println("school="+stu.getSchool()); } }
6.运行结果
转载文章链接已标明,如有侵权请告知。文章仅作为知识记忆所用,如有错误,敬请指正。
分类:
Java EE
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-03-30 jQuery中load方法的使用