首页 > 编程语言 > 详细

Spring 快速使用Spring

时间:2021-04-24 00:54:11      阅读:24      评论:0      收藏:0      [点我收藏+]

Spring快速入门Spring

导入jar包

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

技术分享图片

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的实例
		UserService userService = (UserService) ac.getBean("userService");

		System.out.println(userService.getUserDao());
		System.out.println(userService.getAddressList());
		System.out.println(userService.getAddressProp());
		System.out.println(userService.getAddressMap());
		UserVO userVO = (UserVO) ac.getBean("user");
		
		System.out.println(userVO);

	}

Spring 快速使用Spring

原文:https://www.cnblogs.com/liuzhihui021221/p/14695728.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!