Spring

来源:互联网 发布:安居客网络经纪人登录 编辑:程序博客网 时间:2024/04/28 14:42

1、spring介绍
Spring的出现是为了取代EJB(Enterprise JavaBean)的臃肿、低效、脱离现实的缺点。
Spring致力于J2EE应用的各层(表现层、业务层、持久层)的解决方案,Spring是企业应用开发的“一站式”选择。
定义:
 Spring是分层的JavaSE/EE应用一站式的轻量级开源框架(官网: http://spring.io/ ),以Ioc(Inverse of control)控制反转和Aop(Aspect Oriented Programming)面向切面编程为核心。
轻量级:针对EJB来说,使用方便。
一站式:spring针对各各层(表现层、业务层、持久层)提出解决方案。
 表现层:springmvc(spring自己的mvc框架),提供和其它web框架整合方案。
 业务层:spring基于aop(面向切面编程)思想进行事务控制。
 持久层:spring自己提供JdbcTemplate,提供和其它持久层框架整合的方案。
2、Spring量大特性
(1)、IOC:控制反转
1)、IoC (Inverse of Control)即控制反转。是指将原来程序中自己创建实现类对象的控制权反转到IOC容器中。只需要通过IOC获了对象的实例,将IOC当成一个工厂。
2)、IOC核心容器jar包 (项目中使用的spring版本是4.2.4)
•spring-beans-4.2.4.RELEASE.jar
•spring-context-4.2.4.RELEASE.jar
•spring-core-4.2.4.RELEASE.jar
•spring-expression-4.2.4.RELEASE.jar
3)、spring的ioc容器的配置文件:applicationContext.xml(默认名称)

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- id或name:id和name都可以标识bean的名称,名称在spring的ioc容器中不允许重复,如果不指定id和name,默认名称就是类路径     class:指定接口实现类的全限定名,spring根据class去实例化 --><!-- 配置service --><bean id="customerService" class="com.chyson.wap.service.impl.CustomerServiceImpl" scope="singleton"><!-- 依赖注入属性配置,ref表示从容器中找符合名称的bean实例,spring会调用“set属性名”方法完成注入 --><property name="customerDao" ref="customerDao"/><property name="customerDetailDao" ref="customerDetailDao"/></bean><!-- 配置dao --><bean id="customerDao" class="com.chyson.wap.dao.impl.CustomerDaoImpl" scope="singleton"></bean><bean id="customerDetailDao" class="com.chyson.wap.dao.impl.CustomerDetailDaoImpl" scope="singleton"></bean><!-- 配置action --><bean id="customerAction" class="com.chyson.wap.web.action.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"/></bean></beans>

spring对bean进行实例化方法

<!-- 通过无参构造器(默认) --><bean id="customer1" class="com.chyson.wap.domain.CstCustomer"></bean><!-- 通过有参构造器构造器:public CstCustomer(Long custId,String custName) --><bean id="customer2" class="com.chyson.wap.domain.CstCustomer"><!-- index:参数位置,第一个参数位置为0value:参数值type:参数类型 --><constructor-arg index="0" value="00001" type="java.lang.Long"/><constructor-arg index="1" value="Chyson" type="java.lang.String"/></bean><!-- 通过静态工厂方法获取bean的实例class:配置工厂类的路径factory-method:调用工厂方法,获取对象 --><bean id="customer3" class="com.chyson.wap.domain.CustomerFactory" factory-method="getCustomer"></bean>

(2)DI:依赖注入

依赖注入(Dependency Injection)。所谓依赖注入,就是由IOC容器在运行期间,动态地将对象的依赖关系注入到对象的属性中。

<?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"><!-- 定义dao spring ioc才知道如何去管理bean id或name:id和name都可以标识bean的名称,名称在 spring的ioc容器中不允许重复,如果不指定id或name,默认名称就是类路径 class :指定接口实现类的全限定名,spring容器根据class去实例化 --><bean id="customerDao" class="com.chyson.wap.dao.impl.CustomerDaoImpl"></bean><bean id="customerDetailDao" class="com.chyson.wap.dao.impl.CustomerDetailDaoImpl"></bean><bean id="customerService" class="com.chyson.wap.service.impl.CustomerServiceImpl"><!-- spring进行依赖注入时候根据name拼接成一个set方法,name是set方法后边串 ref:从容器中获取指定名称的bean实例 value:指定一个具体的数据值 ref和value的区别:ref是一个引用,引用一个具体对象实例,value是一个具体的数值 --><property name="customerDao" ref="customerDao"></property><property name="customerDetailDao" ref="customerDetailDao"></property></bean><!-- 配置action --><bean id="customerAction" class="com.chyson.wap.web.action.CustomerAction" scope="prototype"><!-- 配置action依赖的service,完成依赖注入 --><property name="customerService" ref="customerService" /></bean></beans>

依赖注入的方法:

1)、通过有参构造器注入属性值

<!-- 通过有参构造器构造器:public CstCustomer(Long custId,String custName)--><bean id="customer2" class="com.chyson.wap.domain.CstCustomer"><!-- index:参数位置,第一个参数位置为0value:参数值type:参数类型--><constructor-arg index="0" value="00001" type="java.lang.Long"/><constructor-arg index="1" value="Chyson" type="java.lang.String"/> </bean>

2)、通过set方法注入

<bean id="customerService" class="com.chyson.wap.service.impl.CustomerServiceImpl"><!-- spring进行依赖注入时候根据name拼接成一个set方法,name是set方法后边串 ref:从容器中获取指定名称的bean实例 value:指定一个具体的数据值 ref和value的区别:ref是一个引用,引用一个具体对象实例,value是一个具体的数值 --><property name="customerDao" ref="customerDao"></property><property name="customerDetailDao" ref="customerDetailDao"></property></bean>

3)、p命名空间和spEL表达式注入

原创粉丝点击