01、spring IOC基本使用
(1)使用maven的方式来构建项目
定义项目的groupId、artifactId
(2)添加对应的pom依赖
4.0.0
com.mashibingspringHelloword231.0-SNAPSHOT 11 11 org.springframework spring-context 5.3.9
(3)编写配置
1、实体类创建
Person.java
package com.mashibing.bean;
public class Person {
private Long id;
private String name;
private Integer age;
private String gender;
public Person() {}public Person(Long id, String name, Integer age, String gender) { this.id = id; this.name = name; this.age = age; this.gender = gender;}@Overridepublic String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}';}public Long getId() { return id;}public void setId(Long id) { this.id = id;}public String getName() { return name;}public void setName(String name) { this.name = name;}public Integer getAge() { return age;}public void setAge(Integer age) { this.age = age;}public String getGender() { return gender;}public void setGender(String gender) { this.gender = gender;}
}
2、spring.xml配置
3、编写测试类测试类SpringDemoTest.java
public class SpringDemoTest {
public static void main(String[] args) {
//ApplicationContext:表示ioc容器
//ClassPathXmlApplicationContext:表示从当前classpath路径中获取xml文件的配置
//根据spring的配置文件来获取ioc容器对象
ApplicationContext context=new ClassPathXmlApplicationContext(“spring.xml”);
Person person = context.getBean(“person”, Person.class);
System.out.println(person);
}
}
4,测试结果
Person{id=3625221991********, name=’单强’, age=32, gender=’男’}