一.概念:
spring是在2003年兴起的Java EE轻量级、开源框架,为了使java EE项目开发起来简单而设计的。
特点:
1、Spring是一个轻量级(Lightweight)的容器(Container)。
2、spring是松耦合性框架
3、Spring是实现IoC(Inversion of Control)容器和非入侵性(No intrusive)的框架。
4、Spring提供AOP(Aspect-oriented programming)概念的实现方式。
5、Spring提供对持久层(Persistence)、事物(Transcation)的支持。
6、Spring提供MVC Web框架的实现,并对一些常用的企业服务API(Application Interface)提供一致的模型封装。
7、Spring提供了对现存的各种优秀框架(Structs、Hibernate、Ibatis、Webwork等)相整合的方案。
二、spring的核心模块
Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式,组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:
1--Spring Core:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建,装配,销毁等功能,bean主要就是来管理,产生对象
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。
核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转 (IOC)模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
三:spring IOC
1、Ioc的概念
Ioc==Inversion of Control:控制反转。
对象的协作关系由对象自己负责。
< bean id=“boy” class=“com.offcn.entity”>< /bean>
在test类main方法里面{
ApplicationContext app=new ClassPathXmlApplicationContext(“appliactionContext.xml”);
Boy boy= (Boy) app.getBean(“boy”);
boy.say()
}
依赖注入:对象的协作关系有容器来建立。
2、IoC依赖注入的类型
(1)基于set方法,在applicationContext.xml文件中
< beans>
< bean id=”自定义的id名” class=”类路径” >
< property name=”属性名”>
< value>属性值</ value>
< /property>
< /bean>
</ beans>
1— 注入对象
在applicationContext.xml文件中
< bean id="(自定义对象)boy" class=“类路径”></ bean>
//在girl对象里面注入boy对象–前提:girl类里面的属性有boy对象
< bean id=“girl” class=“com.offcn.entity.Girl”>
<!--name参数:就是set方法的属性值,ref就是配置文件的里的id-->
<property name="boy" ref="boy"></property>
</bean>
2— 注入集合
1、Set
userObject这个工具类必须有Set或List或Map的遍历方法,userObject的属性里要有Set等集合的对象
在applicationContext.xml文件中
< beans>
< bean id=“userObject” class=“com.offcn.utils.UserObject”>
< property name=“set”>
< set>
< value>fsdf</ value>
< value>sadf</ value>
< value>礼拜</ value>
< value>唐山</ value>
</ set>
</ property>
< /bean>
</ beans>
2、 List
< property name=“list” >
< list>
< value>admin</ value>
< value>zhangsan</ value>
< value>wulong</ value>
< value>work</ value>
< /list>
</ property>
3、 Map
< property name=“map”>
< map>
< entry key=“01”>
< value>淘宝</ value>
< /entry>
< entry key=“02”>
< value>惊涛</ value>
< /entry>
< entry key=“03”>
< value>海浪</ value>
< /entry>
< /map>
< /property>
Map 有< entry>子元素来存取key,value(key只能为String)
4、Properties
Properties有< props>子元素
(2) --基于构造器–在applicationContext.xml文件中设置
student类必须有给属性赋值的构造方法student(){
}
(3) 注解注入
@autowired,@Qualifier(“userDao”)
在applicationContext.xml文件中加入
<beans xmlns=http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
// 扫描包中注解标注的类
<context:component-scan base-package=“com.offcn.userserviceimpl,com.offcn.daoimpl” />
@Repository(“userDao”)//用于标注数据访问组件
@Service("userService") //用于标注业务层的组件
在业务层实现类使用@autowired,@Qualifier("userDao")或者 @resource
(4) 自动装配 --不常用
byType,byName,constructor,autodetect
四 . IOC容器管理 bean
Bean的命名以及实例化方法
Bean的命名
Bean实例化方法
1.第一种是实现接口方式
< bean id=“userService” class=“com.offcn.serviceImpl.UserServiceImpl”>< /bean>
2.第二种是使用静态工厂,方法不需要括号createUserService
userObject是工厂类,createUserService是userObject的静态方法,create1UserService是非静态方法
< bean id=“userObject” class=“com.offcn.utils.UserObject” factory-method=“createUserService”>< /bean>
3.第三种是使用非静态工厂,方法不需要括号create1UserService
< bean id=“userObject” class=“com.offcn.utils.UserObject”>< /bean>
< bean id=“user” factory-bean=“userObject” factory-method=“create1UserService”></ bean>
4.通过p来给对象属性注入值
前提在applicationContext.xml中:< beans xmlns:p=“http://www.springframework.org/schema/p”></ beans>
< bean id=“person” class=“com.offcn.entity.Person” p:pid=“123” p:pname=“jack”>< /bean>
Bean的重写机
Bean的作用域
Singleton作用域--单例
prototype作用域--多例
request与session作用域
globalSession作用域