Spring框架
- Spring框架的作用
- Spring 的核心(IOC功能)
- SpringAOP功能
请求—>servlet-àservice---->多个dao—jsp
Spring一般将【servlet-àservice---->多个dao—>jsp】交给IOC和AOP管理
(管理组件对象,维护对象关系,降低组件耦合度)
Spring Web MVC功能:MVC设计 目的:架构一个MVC结构的web程序
注意:(2和3可选择)
- Spring整合其他技术,例如JDBC,Hibernate,struts,Mybatis等
- Spring—>整合API---->调用原有技术API()
SpringIOC应用
IOC概念:Inversion of Control控制反转或反向控制
控制反转:改变对象的获取方式。之前编码方式采用new构造器方式获取对象;IOC中用由容器创建对象之后注入进来使用。只要修改配置就可以改变对象关系,实现组件间的解耦。(管理创建组件对象,依赖注入信息)
1)管理对象:创建,初始化,释放资源,销毁
2)维护对象关系
搭建SpringIOC开发环境
----引入相关jar包
----在src下添加applicationContext.xml
举例:
1)原始方法获得一个对象
package test; import java.util.Calendar; import java.util.GregorianCalendar; public class TestBean { public static void main(String[] args) { Calendar c=new GregorianCalendar(); Calendar c1=Calendar.getInstance(); System.out.println(c); System.out.println(c1); } }
2)SpringIOC的创建对象的方式
具体流程:
SpringIOC容器----->管理组件及对象关系
- 创建ApplicationContext对象
- 向applicationContext.xml配置<bean>
- 利用ApplicationContext对象getBean()
管理对象
在applicationContext.xml添加相关配置:
(1)创建对象(构造方法)
<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--采用new GregorianCalendar(),构造方法--> <bean id="c1" class="java.util.GregorianCalendar"> </bean> </beans>
测试:
package test; import java.util.Calendar; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { public static void main(String[] args) { String conf="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(conf); Calendar c1=ac.getBean("c1",Calendar.class); //使用 System.out.println(c1); } }
(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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--采用new GregorianCalendar(),构造方法--> <bean id="c1" class="java.util.GregorianCalendar"> </bean> <!--采用Calendar.getInstance(),静态工厂方法--> <bean id="c2" class="java.util.Calendar" factory-method="getInstance"></bean> </beans>
(3)创建对象(对象工厂方法)---封装了对象的创建细节
<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--采用new GregorianCalendar(),构造方法--> <bean id="c1" class="java.util.GregorianCalendar"> </bean> <!--采用Calendar.getInstance(),静态工厂方法--> <bean id="c2" class="java.util.Calendar" factory-method="getInstance"></bean> <!--采用c2.getTime(),对象工厂方法--> <bean id="date" factory-bean="c2" factory-method="getTime"></bean> </beans>
测试:
package test; import java.util.Calendar; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { public static void main(String[] args) { String conf="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(conf); Calendar c1=ac.getBean("c1",Calendar.class); //使用 System.out.println(c1); Calendar c2=ac.getBean("c2",Calendar.class); System.out.println(c2); Date date=ac.getBean("date",Date.class); System.out.println("date="+date); } }
SpringIOC创建Bean对象的控制
- 控制对象创建方式(使用范围)
在bean元素中使用scope属性控制,可以支持singleton或prototype,默认是singleton
<bean scope=”singleton”>该组件在Spring 中只有一个bean对象</bean>
<bean scope=”prototype”>该组件在Spring 中每次ac.getBean(“id”)都返回一个新的对象</bean>
<bean id="e1" scope="prototype" class="org.web.bean.ExampleBean"></bean>
2.指定对象初始化方法
Java(原始方式使用构造器)
Spring方式:先在org.tarena.bean.ExampleBean中添加init方法,然后 用<bean>元素的init-method指定初始化方法
<bean id="e1" scope="prototype" init-method="init" class="org.web.bean.ExampleBean"></bean>
3.指定对象销毁方法:destroy-method属性
利用<bean>元素的destroy-method指定,满足下面条件才有效
-----1)组件对象为单利模式
-----2)调用AbstractApplicationContext容器对象的close()方法
<bean id="e1" scope="prototype" init-method="init" destroy-method="mydestory" class="org.web.bean.ExampleBean"></bean>
4.控制单例对象创建时机
在默认情况下,单例对象是Spring容器创建时实例化;可以使用<bean>元素的lazy-init=true属性将创建的时机推迟到getBean()方法调用时。
<bean id="e1" scope="prototype" lazy-init="true" init-method="init" destroy-method="mydestory" class="org.web.bean.ExampleBean"></bean>
IOC维护对象关系
采用注入方式创建对象关系Dependency Injection依赖注入(DI)
依赖注入:set方法注入;构造器注入;
set方法注入
创建copputer类,添加set和get方法
package com.web.entity; import java.io.Serializable; public class Computer implements Serializable { private String cpu; private String hdd; private String mainbord; //显示配置信息 public void show(){ System.out.println("cpu"+cpu); System.out.println("hdd"+hdd); System.out.println("mainbord"+mainbord); } public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getHdd() { return hdd; } public void setHdd(String hdd) { this.hdd = hdd; } public String getMainbord() { return mainbord; } public void setMainbord(String mainbord) { this.mainbord = mainbord; } }
在容器中配置信息(set注入)
<bean id="p1" calss="com.web.entity.Computer"> <!--信息set注入--> <property name="cpu" value="i7"></property> <property name="hdd" value="索尼"></property> <property name="mainbord" value="华硕"></property> </bean>
测试:
String conf="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(conf); Computer c=ac.getBean("p1",Computer.class); c.show();
构造器注入
创建Phone类,添加构造器方法
package com.web.entity; import java.io.Serializable; public class Phone implements Serializable { private String cpu; private String ram;
//构造器 public Phone(String cpu,String ram){ this.cpu=cpu; this.ram=ram; } public void show(){ System.out.println("手机配置"); } public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } }
在容器中配置信息:构造器注入
<bean id="p2" calss="com.web.entity.Phone"> <!--构造器注入--> <constructor-arg index="0" value="高通"></constructor-arg> <constructor-arg index="1" value="2G"></constructor-arg> </bean>
注入对象(set方式)
创建students类,包含computer和Phone
package com.web.entity; import java.io.Serializable; public class Students implements Serializable { private Computer c; private Phone p; public void show(){ c.show(); p.show(); } }
在容器中配置注入对象
<bean id="p1" calss="com.web.entity.Computer"> <!--set注入computer--> <property name="c" ref="p1"></property> <property name="p" ref="p2"></property> </bean>