作者:mobiledu2502917185 | 来源:互联网 | 2023-09-14 09:43
大家都说Spring框架好,而且之后的培训也会遇到框架,所以趁今天有点时间就看了一下Spring框架,这篇文章也是看后的一点小小的理解,非常的基础,大神请主动走开.好了不说的
大家都说Spring框架好,而且之后的培训也会遇到框架,所以趁今天有点时间就看了一下Spring框架,这篇文章也是看后的一点小小的理解,非常的基础,大神请主动走开....好了不说的,看看我理解的Spring框架吧。
首先是框架,我认为框架是为了某种目的更好的实现而编写的。或者说是为了更加方便的实现某种功能而编写的。Spring框架我们知道无论是一般的应用程序还是javaEE都是可以应用。自然有其方便之处。但是在一开始接触的时候被各种名词搞的晕头转向,什么工厂生成模式,控制反转,(我感觉控制反转就是我们之前编写程序的时候需要某一个类的对象都是new出来的,但是我们在框架中是不用new的,我们在框架中只需要getBean(name)就可以得到我们想要的对象了,这个就是框架给我们提供的方便)比如说我们有一个Course类,在编写的过程中我们需要一个Course的对象,之前我们new Course来创建一个Course对象,这样很麻烦哎,于是我们使用Spring框架可以解决这样的麻烦。那么我们下面就通过一个小的例子来看一下更为简单的方法。
第一步当然是下载Spring框架,这个我相信应该难不倒你吧.
第二步我们在Ecplise创建一个java工程,或者是web工程,都可以的,我们在这个地方创建一个web工程项目。
现在我们创建好的项目需要做的事情是将我们下载的Spring/lib里面的各种包导入到项目中,那么我们需要导入那些包呢
将这些包导入到项目中,是我们开始需要做的,如果我们后来需要扩展怎样的功能可以进一步的导入一下包,一开始我们可能还不知道这些包都具有什么样的作用,现在不要问这些,继续下一步的操作。
就是到我们熟悉的地方,我们在src中创建两个包,一个是dao包,一个是domain包,这两个包创建好之后,我们正在domain包中创建一个实体类,比如说Course类:如下:
package domain;import java.util.Date;public class Course {private String id;private String courseName;private Date startTime;private int suitable;private int type;private int totalHours;private int hotLevel;@Overridepublic String toString() {return "Course [id=" + id + ", courseName=" + courseName + ", startTime=" + startTime + ", suitable=" + suitable+ ", type=" + type + ", totalHours=" + totalHours + ", hotLevel=" + hotLevel + ", selectedCount="+ selectedCount + ", note=" + note + "]";}public Course(String id, String courseName) {super();this.id = id;this.courseName = courseName;}private int selectedCount;private String note;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}public Date getStartTime() {return startTime;}public void setStartTime(Date startTime) {this.startTime = startTime;}public int getSuitable() {return suitable;}public void setSuitable(int suitable) {this.suitable = suitable;}public int getType() {return type;}public void setType(int type) {this.type = type;}public int getTotalHours() {return totalHours;}public void setTotalHours(int totalHours) {this.totalHours = totalHours;}public int getHotLevel() {return hotLevel;}public void setHotLevel(int hotLevel) {this.hotLevel = hotLevel;}public int getSelectedCount() {return selectedCount;}public void setSelectedCount(int selectCount) {this.selectedCount = selectCount;}public String getNote() {return note;}public void setNote(String note) {this.note = note;}public Course() {super();// TODO Auto-generated constructor stub}public Course(String id, String courseName, Date startTime, int type2, int type, int totalHours, String note) {super();this.id = id;this.courseName = courseName;this.startTime = startTime;this.suitable = type2;this.type = type;this.totalHours = totalHours;this.note = note;}public Course(String id, String courseName, Date startTime, int suitable, int type, int totalHours, int hotLevel,int selectCount, String note) {super();this.id = id;this.courseName = courseName;this.startTime = startTime;this.suitable = suitable;this.type = type;this.totalHours = totalHours;this.hotLevel = hotLevel;this.selectedCount = selectCount;this.note = note;}}当然你可以写自己的实体类比如说person或者Student等等
然后我们需要做的事情是写配置文件,这个需要我们自己动手写名字就为:applicationContext.xml,如下:
配置好这些之后我们需要做的事情就是看一下我们的Spring框架是不是安装成功了
我们在dao包下编写一个Test类,如下:
package dao;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import domain.Course;public class Test { public static void main(String []args) { String location="classpath:applicationContext.xml"; ApplicationContext ctx=new ClassPathXmlApplicationContext(location); Course cous=(Course)ctx.getBean("course"); System.out.println(cous.getId()); }}然后执行我们的程序,如果最后在控制台成功的输出0001就表示我们的框架搭建是成功的。
最后控制台输出:
十二月 29, 2016 5:04:35 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7eda2dbb: startup date [Thu Dec 29 17:04:35 CST 2016]; root of context hierarchy十二月 29, 2016 5:04:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]十二月 29, 2016 5:04:36 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2ff4f00f: defining beans [course]; root of factory hierarchy0001如果说你最后输出的日志文件不是这样的,你可以将log4j.jar这个包移除然后添加另外的一个包
commmon-logging-jar这个包试一下,
我这构建的Spring框架也许有不足,如果你有更好的办法也欢迎一起交流。。。
希望对你有所帮助