Spring快速入门Spring

导入jar包

使用spring需要导入基本的jar包

image-20210421111210326

1.编写一个实体类
public class UserVO {
	
	public UserVO() {
		super();
	}
	public UserVO(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "UserVO [id=" + id + ", name=" + name + "]";
	}
	
}
2.编写我们的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  <!--基本命名空间的定义-->
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
        	http://www.springframework.org/schema/aop   
        	http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--bean就是java对象 , 由Spring创建和管理-->
   <bean id="user" class="com.znsd.spring.vo.UserVO">
       <property name="name" value="Spring"/>  <!--通过set注入-->
   </bean>
</beans>
3.测试
@Test
void test() { //
	// 通过ClassPathXmlApplicationContext实例化Spring的上下文
	ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
	// 通过ApplicationContext的getBean()方法,根据id来获取bean的实例
	UserVO userVO = (UserVO) ac.getBean("user");

	System.out.println(userVO);

}