Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念
一、开发前的准备
两个开发包spring-framework-3.1.1.RELEASE-with-docs.zip和commons-logging-1.2-bin.zip,将它们解压,然后把Spring开发包下dist目录的所有包和commons-logging包下的commons-logging-1.1.1.jar复制到名为Spring3.1.1的文件夹下。那么Spring开发所需要的包就组织好了。
二、建立项目,导入包
在项目节点上右键,Build Path/ADD Libraries/USER Libraries/new/输入包名Spring3.1.1/OK/ADD JARS/选中准备好的包/OK。此外引入JUnit4方便程序测试。
三、添加文件
person接口Person.Java:
package com.bean; public interface Person{ public void Speak(); }
AmericanImpl.java:
package com.bean; public class AmericanImpl implements Person{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public void Speak() { // TODO Auto-generated method stub System.out.println("I'm American,My name is "+this.name+",I'm "+this.age+" years old!"); } }
ChineseImpl.java:
package com.bean; public class ChineseImpl implements Person{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public void Speak() { // TODO Auto-generated method stub System.out.println("I'm Chinese,My name is "+this.name+",I'm "+this.age+" years old!"); } }
配置文件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" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="chinese" class="com.bean.ChineseImpl"> <property name="name"> <value>小明</value> </property> <property name="age"> <value>10</value> </property> </bean> <bean id="american" class="com.bean.AmericanImpl"> <property name="name"> <value>Tom</value> </property> <property name="age"> <value>15</value> </property> </bean> </beans>
用于测试的Test.java:
package com.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.Person; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Person person=(Person)context.getBean("chinese"); person.Speak(); person=(Person)context.getBean("american"); person.Speak(); } }
四、测试结果
在Test.Java上右击Run As|Java Application,输出结果如下:
作者:wuenwuen
出处:https://www.cnblogs.com/MrReboot/p/6195939.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
标签:
Spring
Present to people who live kind and love learning ☕.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下