使用eclipse搭建Spring框架
来源:互联网 发布:js获取选中的radio 编辑:程序博客网 时间:2024/05/29 17:06
1.为了便于快速开发,所以我们需要下载一个spring tool工具
具体详情请参考http://blog.csdn.net/zhangkaidsy/article/details/77470908
但是和下载Hibernate Tools工具不一样的是,这次不需要去掉任何工具的勾选,默认全部勾选的就好,然后静静等待几分钟,成功后eclipse会提示是否重启,选择YES
2.重启后,在pom.xml添加Spring依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.10.RELEASE</version> </dependency>
如果需要添加Spring其他版本的依赖
请去Spring官网www.spring.io
或者去Maven repository自行查询Spring依赖版本
3.依赖添加成功后,在resources下新建xml
然后Finish即可
4.使用
新建HumanBody实体类
package com.zhangkai.entity;public class HumanBody { private Hand hand; private Foot foot; private Renal renal; public HumanBody(Hand hand, Foot foot, Renal renal) { super(); this.hand = hand; this.foot = foot; this.renal = renal; } public HumanBody() { super(); } public Hand getHand() { return hand; } public void setHand(Hand hand) { this.hand = hand; } public Foot getFoot() { return foot; } public void setFoot(Foot foot) { this.foot = foot; } public Renal getRenal() { return renal; } public void setRenal(Renal renal) { this.renal = renal; } @Override public String toString() { return "HumanBody [hand=" + hand.getHand() + ", foot=" + foot.getFoot() + ", renal=" + renal.getRenal() + "]"; }}
在编写三个接口,在这我就只是用其中一个接口作为示例
package com.zhangkai.entity;public interface Foot { public String getFoot();}
实现接口
package com.zhangkai.dao;import com.zhangkai.entity.Hand;public class HandRightDao implements Hand{ public String getHand() { return "这是右手"; }}
applicationContext.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="HumanBody" class="com.zhangkai.entity.HumanBody"><!-- 引用实体类 --> <property name="hand" ref="HandRightDao"></property><!-- 为实体类属性引用方法实现类 ref:方法实现类id在下方--> <property name="foot" ref="FootLiftDao"></property> <property name="renal" ref="RenalLiftDao"></property> </bean> <bean id="HandRightDao" class="com.zhangkai.dao.HandRightDao"></bean><!-- 引用方法实现类 --> <bean id="FootLiftDao" class="com.zhangkai.dao.FootLiftDao"></bean> <bean id="RenalLiftDao" class="com.zhangkai.dao.RenalLiftDao"></bean> <!-- 注意:id要保持一致 --></beans>
测试:
package com.zhangkai.test;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import com.zhangkai.entity.HumanBody;public class SpringTest { //延时加载:解析xml时并不会加载所有bean,只会在调用bean时去加载。优缺点:服务器启动速度较快,但用户等待时间较长 public void test1(){//延时加载(懒加载) BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); HumanBody humanBody=(HumanBody) bf.getBean("HumanBody"); System.out.println(humanBody); } //即时加载:解析XMl时将所有bean全部加载,调用bean时便不会再去xml解析了。优缺点:服务器启动速度较慢,但用户等待时间较短 @Test public void test2(){//即时加载 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); HumanBody humanBody=(HumanBody) ac.getBean("HumanBody"); System.out.println(humanBody); }}
希望此博客能对您有所帮助!
阅读全文
0 0
- 使用eclipse搭建Spring框架
- 如何使用Eclipse搭建Spring框架
- eclipse搭建spring框架
- eclipse 使用maven 搭建spring + springMVC + spring Data + jpa 框架
- eclipse 搭建spring框架总结
- 使用eclipse搭建springMVC框架
- 使用eclipse搭建ssh框架
- Eclipse 搭建 struts2 spring mybitas 框架
- Eclipse搭建简单的Spring框架
- Eclipse搭建SpringMVC+Spring+MyBatis框架
- 使用eclipse简单的搭建ssh框架
- Eclipse下使用Maven搭建SpringMVCWeb框架
- 使用axis1+spring+hibernate搭建webservice框架
- 使用axis1+spring+hibernate搭建webservice框架
- 使用axis1+spring+hibernate搭建webservice框架
- spring框架基本搭建及使用
- 使用Spring boot 简单搭建网站框架
- 使用Spring、SpringMvc、MyBatis搭建SSM框架
- MOOC清华《面向对象程序设计》第4章:基类与派生类的继承权限实验
- scala中的函数哪些有返回值,哪些没有返回值??
- F
- 图像处理界双线性插值算法的优化
- redis php扩展安装(window)
- 使用eclipse搭建Spring框架
- 直播协议 HTTP-FLV 详解
- in和exists的区别
- 多态,虚函数,纯虚函数 , 虚析构函数 ,抽象类
- 深度学习与文本分类总结第二篇--大规模多标签文本分类
- 操作系统的学习(3)——物理内存管理:非连续内存分配
- kaldi 安装
- ButterKnife解析
- Android自定义ViewGroup常用知识点归类