赞
踩
在jsp开发过程中如何让使用spring框架,对spring框架的配置,介绍和使用
1.spring简介
1.1.Spring 是一个开源框架。Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。用于配置bean,并维护bean之间关系的框架.bean (是java中的任何一种对象 javabean/service/action/数据源./dao, ioc(控制反转 inverse of control) di( dependency injection 依赖注入)。
1.2. 优点
(1)J2EE应该更加容易使用。
(2)面向对象的设计比任何实现技术(比如J2EE)都重要。
(3)面向接口编程,而不是针对类编程。Spring将使用接口的复杂度降低到零。(面向接口编程有哪些复杂度?)
(4)代码应该易于测试。Spring框架会帮助你,使代码的测试更加简单。
(5)JavaBean提供了应用程序配置的最好方法。
(6)在Java中,已检查异常(Checked exception)被过度使用。框架不应该迫使你捕获不能恢复的异常。
1.3.Spring的组成:
(1). Spring Core:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建,装配,销毁等功能
SpringContext:ApplicationContext,扩展核心容器,提供事件处理、国际化等功能。它提供了一些企业级服务的功能,提供了JNDI,EJB,RMI的支持。
(2) Spring AOP:提供切面支持
(3) Spring DAO:提供事务支持,JDBC,DAO支持
(4) Spring ORM:对流行的O/R Mapping封装或支持
(5)Spring Web:提供Web应用上下文,对Web开发提供功能上的支持,如请求,表单,异常等。
(6) Spring Web MVC:全功能MVC框架,作用等同于Struts。
2.如何在eclipse中配置spring.jar 包和创建spring配置文件 applicationContext.xml文件
2.1.下载jar包
(1)官网下载https://spring.io/ 创建java project 工程 将下载好的spring.jar拷贝到lib目录下,并buil path 引入 必须引入的包:spring.jar (框架核心包)和commons-logging.jar(日志输出包)
如图:
2.2.创建配置文件 applicationContext.xml将配置文件放在src目录下
文件中配置基本参数:
[code lang="xml"] <beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> </beans> [/code]
3.在xml文件中配置bean 及bean的使用
3.1.xml文件配置bean来创建对象
[code lang="xml"] <beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 在容器文件中 配置bean(servce /dao /domain /action) --> <bean id="userService" class="com.service.UserService"> <property name="name"> <!-- 提现注入的概念 --> <value>xxxxx</value> </property> <!--在Userservice中引用com.service.ByService bean --> <property name="byService" ref="byService" /> </bean> <bean id="byService" class="com.service.ByService"> <property name="name" value="xxxx"></property> </bean> </beans> [/code]
3.2获取配置的bean,创建一个Text.javal来使用spring来完成上面的任务
[code lang="objc"] //1.得到spring 的applicationContext对象(容器对象) ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService us=(UserService) ac.getBean("userService"); us.sayHello(); [/code]
总结:使用spring ,没有new 对象,我们把创建对象的任务交给spring框架。是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护bean与bean的关系,当我们需要使用某个bean的时候,我们可以getBean(id),使用即可.
3.3.从bean工厂中获取bean
[code lang="objc"] BeanFactory factory = new XmlBeanFactory( new ClassPathResource("com/cxk/ioc/beans.xml")); factory.getBean("student"); [/code]
总结:
(1).如果使用ApplicationContext ,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
(2).如果是 BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)
4.在spring配置文件xml中如何给集合类型注入值
4.1.java中主要的集合有几种: map set list / 数组
(1).创建一个Department类
[code lang="java"] package com.cxk.collection; import java.util.List; import java.util.Map; import java.util.Set; public class Department { private String name; private String [] empName; private List<Employee> empList; private Set<Employee> empsets; private Map<String,Employee> empMaps; public Set<Employee> getEmpsets() { return empsets; } public void setEmpsets(Set<Employee> empsets) { this.empsets = empsets; } public String[] getEmpName() { return empName; } public void setEmpName(String[] empName) { this.empName = empName; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Employee> getEmpList() { return empList; } public void setEmpList(List<Employee> empList) { this.empList = empList; } public Map<String, Employee> getEmpMaps() { return empMaps; } public void setEmpMaps(Map<String, Employee> empMaps) { this.empMaps = empMaps; } } [/code]
(2)创建一个Employeel类
[code lang="java"] package com.cxk.collection; public class Employee { private String name; private int id; 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; } } [/code]
(3). 配置文件:
[code lang="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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="department" class="com.hsp.collection.Department"> <property name="name" value="财务部"/> <!-- 给数组注入值 --> <property name="empName"> <list> <value>小明</value> <value>小明小明</value> <value>小明小明小明小明</value> </list> </property> <!-- 给list注入值 list 中可以有相当的对象 --> <property name="empList"> <list> <ref bean="emp2" /> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> </list> </property> <!-- 给set注入值 set不能有相同的对象 --> <property name="empsets"> <set> <ref bean="emp1" /> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> </set> </property> <!-- 给map注入值 map只有key不一样,就可以装配value --> <property name="empMaps"> <map> <entry key="11" value-ref="emp1" /> <entry key="22" value-ref="emp2"/> <entry key="33" value-ref="emp1"/> </map> </property> <!-- 给属性集合配置 -->【点http协议 referer 】 <property name="pp"> <props> <prop key="pp1">abcd</prop> <prop key="pp2">hello</prop> </props> </property> </bean> <bean id="emp1" class="com.hsp.collection.Employee"> <property name="name" value="北京"/> <property name="id" value="1"/> </bean> <bean id="emp2" class="com.hsp.collection.Employee"> <property name="name" value="天津"/> <property name="id" value="2"/> </bean> </beans> [/code]
(4).写一个函数来测试打印的结果
[code lang="java"] public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac=new ClassPathXmlApplicationContext("com/cxk/collection/beans.xml"); Department department=(Department)ac.getBean("department"); System.out.println("getName--"+department.getName()); for (String emName:department.getEmpName()) { System.out.println("emName---"+emName); } System.out.println("通过listq集合取出数据----"); for (Employee e : department.getEmplist()) { System.out.println("empoee--name-"+e.getName()); } System.out.println("通过set集合取出数据----"); for (Employee e : department.getEmpsets()) { System.out.println("empoee--name-"+e.getName()); } System.out.println("通过map集合取出数据 -迭代方法----"); //1 迭代器 Map<String, Employee> empmaps=department.getEmpMaps(); Iterator<String> iterator=empmaps.keySet().iterator(); while (iterator.hasNext()) { String key=(String)iterator.next(); Employee employee=empmaps.get(key); System.out.println("key="+key+"----"+employee.getName()); } System.out.println("通过map集合取出数据 --简介方法----"); //2 entry 简介方法 for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){ System.out.println("map--name-"+entry1.getKey()+":"+entry1.getValue()); } System.out.println("通过propertis集合取出数据 ---"); Properties pp=department.getPp(); //System.out.println("pp1---"+pp.get("pp1").toString()); for(Entry<Object, Object> entry:pp.entrySet()){ System.out.println("entry--"+entry.getKey().toString()+entry.getValue().toString()); } //枚举取 System.out.println("通过枚举取 --集合取出数据 ---"); Enumeration en=pp.keys(); while (en.hasMoreElements()) { String key=(String)en.nextElement(); //Entry<Object, Object> elment = (Entry<Object, Object>) en.nextElement(); //System.out.println("elment---"+elment.getKey()+"---"+elment.getValue()); System.out.println("elment---"+key+"---"+pp.getProperty(key)); } [/code]
5.通过构造函数注入值
(1).bean.xml配置代码
[code lang="xml"] <!-- 配置一个雇员对象 --> <bean id="employee" class="com.hsp.constructor.Employee"> <!-- 通过构造函数来注入属性值 --> <constructor-arg index="0" type="java.lang.String" value="小明" /> </bean> [/code]
(2).构造函数类:
[code lang="java"] package com.cxk.constructor; public class Employee { private String name; private int age; public Employee () { } public Employee(String name, int age) { this.name = name; this.age = age; } public Employee (String name) { System.out.println("Employee (String name)"); this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } [/code]
(3).获取方法:
[code lang="java"] ApplicationContext ac=new ClassPathXmlApplicationContext("com/cxk/constructor/beans.xml"); Employee employee=(Employee)ac.getBean("employees"); employee.getName(); System.out.println("name---"+employee.getName()); [/code]
6.自动装配bean的属性值
(1)配置参数介绍:
(2)byName的用法:
[code lang="xml"] <!-- 配置一个master对象 配置 autowire="byName" --> <bean id="master" class="com.hsp.autowire.Master" autowire="byName"> <property name="name"> <value>chen</value> </property> </bean> <!-- 配置dog对象 --> <bean id="dog" class="com.hsp.autowire.Dog"> <property name="name" value="小黄"/> <property name="age" value="3"/> </bean> [/code]
创建Master和Dog两个类 并创建测试函数 调用方法与上面的案例一样
(3).byType: byType:寻找和属性类型相同的bean,找不到,装不上,找到多个抛异常。
(4).constructor: autowire="constructor"
说明 : 查找和bean的构造参数一致的一个或 多个bean,若找不到或找到多个,抛异常。按照参数的类型装配
(5).autodetect 说明 :autowire="autodetect"(4)和(3)之间选一个方式。不确定性的处理与(3)和(2)一致。
(6).defualt这个需要在
当你在指定了 default-atuowrite后, 所有的bean的 默认的autowire就是 指定的装配方法;如果没有在 没有 defualt-autorwire=“指定” ,则默认是defualt-autorwire=”no”
(7).no: 不自动装配
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。