上手spring
IDEA创建一个Maven项目,加入所需要的依赖:
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-webmvc</artifactId> 4 <version>5.2.5.RELEASE</version> 5 </dependency>
所谓的spring容器,我的理解就是帮助程序员new对象,程序中所有的对象都可以交给spring容器去创建管理。只要把需要创建对象的类加入到spring的xml文件中去即可。
这里我们先创建一个pojo:
1 package spring.beans; 2 3 import java.lang.reflect.Array; 4 import java.util.Arrays; 5 import java.util.List; 6 import java.util.Map; 7 8 public class People { 9 private String name; 10 private int age; 11 private String[] hobby; 12 private List job; 13 private Map score; 14 private String wife; 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public int getAge() { 25 return age; 26 } 27 28 public void setAge(int age) { 29 this.age = age; 30 } 31 32 public String[] getHobby() { 33 return hobby; 34 } 35 36 public void setHobby(String[] hobby) { 37 this.hobby = hobby; 38 } 39 40 public List getJob() { 41 return job; 42 } 43 44 public void setJob(List job) { 45 this.job = job; 46 } 47 48 public Map getScore() { 49 return score; 50 } 51 52 public void setScore(Map score) { 53 this.score = score; 54 } 55 56 public String getWife() { 57 return wife; 58 } 59 60 public void setWife(String wife) { 61 this.wife = wife; 62 } 63 64 @Override 65 public String toString() { 66 return "People{" + 67 "name='" + name + '\'' + 68 ", age=" + age + 69 ", hobby=" + Arrays.toString(hobby) + 70 ", job=" + job + 71 ", score=" + score + 72 ", wife='" + wife + '\'' + 73 '}'; 74 } 75 }
然后在新建一个beans.xml文件来管理bean。文件名可随便取即可:
<?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="student" class="spring.beans.Student"> <property name="name" value="小明"/> </bean>
<!--这里的bean的功能相当于People people = new People()。property就是给类中属性赋值--> <bean id="people" class="spring.beans.People"> <property name="name" value="小王"/> <property name="age" value="21"/> <property name="hobby"> <array> <value>红楼梦</value> <value>三国演义</value> <value>水浒传</value> <value>西游记</value> </array> </property> <property name="job"> <list> <value>程序员</value> <value>销售</value> </list> </property> <property name="score"> <map> <entry key="weight" value="100"/> <entry key="height" value="180"/> </map> </property> <property name="wife"> <null/> </property> </bean> </beans>
这样就可以测试了:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.beans.People; import spring.beans.Student; public class TextSpring { public static void main(String[] args) {
//通过 new ClassPathXmlApplicationContext("beans.xml")来获取spring的配置文件,返回一个ApplicationContext; ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
//getBean("people"); 获取beans.xml文件中由spring容器创建好的对象People。 People p = (People) app.getBean("people"); System.out.printf(p.toString()); } }