参考w3cschool文档:https://www.w3cschool.cn/wkspring/dgte1ica.html 创建第一个spring项目,命名HelloSpring。项目创建好后,在src下依次创建一个com.tueorialspoint的包和一个Beans.xml文件,包里面再创建两个Java文件(HelloWorld.java和MainApp.java)。创建完后的目录结构如图1所示。
图1 项目目录结构
各个文件内容如下: HelloWorld.java
package com.tutorialspoint; public class HelloWorld { private String message; public void getMessage() { System.out.println("Your Message:"+message); } public void setMessage(String message) { this.message = message; } }MainApp.java
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj=(HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } }Beans.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-3.0.xsd"> <bean id="helloWorld" class="com.tutorialspoint.HelloWorld"> <property name="message" value="Hello World!"/> </bean> </beans>导入相关jar包后,运行程序,出现下图中的错误
安装完spring框架后,并且当前项目导入相关spring的jar包,没有导入commons-logging的jar包。
下载commons-logging的jar包并导入到项目中 下载地址:https://dl.pconline.com.cn/download/1017089-1.html 导入后运行,运行成功