快速上手第一个Spring
1.导入jar包
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.1.10.RELEASE</ version>
</ dependency>
2.编写代码
1)编写一个Hello实体类
public class Hello {
private String name;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public void show ( ) {
System. out. println ( "Hello," + name ) ;
}
}
2)编写我们的spring文件,这里我们命名为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.xsd" >
< bean id = " hello" class = " com.kuang.pojo.Hello" >
< property name = " name" value = " Spring" />
</ bean>
</ beans>
3)测试
@Test
public void test ( ) {
ApplicationContext context = new ClassPathXmlApplicationContext ( "beans.xml" ) ;
Hello hello = ( Hello) context. getBean ( "hello" ) ;
hello. show ( ) ;
}