J2EE学习笔记——Spring基础

来源:互联网 发布:轩辕剑之天之痕mac 编辑:程序博客网 时间:2024/04/20 02:03

  

更多编程信息   请访问  www.ibcve.com     爱编程唯一网     !!


 Spring是一个开源框架,Spring 是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。

简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

◆轻量——从大小与开销两方面而言Spring都是轻量的。完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。并且Spring所需的处理开销也是微不足道的。此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于Spring的特定类。

控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。

◆面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。

◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例——以及它们是如何相互关联的。然而,Spring不应该被混同于传统的重量级的EJB容器,它们经常是庞大与笨重的,难以使用。

框架——Spring可以将简单的组件配置、组合成为复杂的应用。在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。

所有Spring的这些特征使你能够编写更干净、更可管理、并且更易于测试的代码。它们也为Spring中的各种模块提供了基础支持。


项目整体截图:


引入项目Springle支持:


3. 新建一个 student.java 

package xuyan.com.vo;public class Student {         public int getID() {return ID;}public void setID(int iD) {ID = iD;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return Age;}public void setAge(int age) {Age = age;}private int ID;         private String name;         private int Age;}


更多JAVA编程信息   请访问  www.ibcve.com     爱编程唯一网     !!


4.建一个StudentDao.java   的接口 在里面写一个Save 方法

package xuyan.com.dao;import xuyan.com.vo.Student;public interface StudentDao {   //保存学生public boolean SaveStudent(Student s);}

5.  实现上面接口里的方法: StudentDaoImple.java

package xuyan.com.imple;import xuyan.com.dao.StudentDao;import xuyan.com.vo.Student;public class StudentDaoImple implements StudentDao {public boolean SaveStudent(Student s) {if(s!=null){System.out.println("学号"+s.getID());System.out.println("姓名"+s.getName());System.out.println("年龄"+s.getAge());return true;}else {return false;}}}

6.再写一个 services 方法:  StudetnServices.java

package xuyan.com.service;import xuyan.com.dao.StudentDao;import xuyan.com.vo.Student;public class StudentServices {   //保存学生private StudentDao sDao;   //以备注入public StudentDao getsDao() {return sDao;}public void setsDao(StudentDao sDao) {this.sDao = sDao;}public boolean SaveStudent(Student s){if(sDao.SaveStudent(s)){return true;}else{return false;}}}

7.配置 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  <bean name="student" class="xuyan.com.vo.Student"/>  <bean name="studentDao" class="xuyan.com.imple.StudentDaoImple"/>   <bean name="studentservice" class="xuyan.com.service.StudentServices">    <property name="sDao" ref="studentDao" />   </bean></beans>

8.好了  ,开始测试,建立一个测试方法TestStudentService.java


import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import xuyan.com.service.StudentServices;import xuyan.com.vo.Student;import junit.framework.Assert;import junit.framework.TestCase;public class TestStudentService extends TestCase{     public void testSaveStudent()     {     ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");      Student s = (Student) ctx.getBean("student");      s.setID(12);      s.setName("skyxuyan");      s.setAge(24);            StudentServices sServices = (StudentServices) ctx.getBean("studentservice");           Assert.assertEquals(true, sServices.SaveStudent(s));     }}


好了,一个Spring3.0 的基础配置 和 建立已经完成,希望对大家有帮助!!


更多JAVA编程信息   请访问  www.ibcve.com     爱编程唯一网     !!

原创粉丝点击