Spring

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:参数位置,第一个参数位置为0
	value:参数值
	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:参数位置,第一个参数位置为0
value:参数值
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表达式注入

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值