Hello Spring
1.新建一个空的Maven项目
2.导包
spring-webmvc.jar
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
3.编写一个实体类
User实体类
package com.feng.pojo;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+name);
}
}
4.在resources目录下新建一个beans.xml
编写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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.feng.pojo.User">
<property name="name" value="Spring"/>
</bean>
</beans>
5. 测试
import com.feng.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericGroovyApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new GenericGroovyApplicationContext("beans.xml");
User hello = (User) context.getBean("hello");
hello.show();
}
}
6.遇到的问题
若运行时,报Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject错误,则需要导入
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.7</version>
</dependency>