Spring总结笔记(一)

来源:互联网 发布:matlab优化函数 编辑:程序博客网 时间:2024/05/22 10:50
Spring简介
概述 : 基于JAVA的轻量级J2EE(一站式)应用框架
应用 : 应用于J2EE开发的各个层面(表现层, 业务层 , 数据层)
两大核心
IOC : 控制反转
AOP : 面向切面编程


Spring系统架构
概述 : Spring是一个整合的平台 , 可以将很多的技术整合在一起使用 ,使每个组件发挥其最大功效
架构
核心容器 : Core Container
数据访问/集成 : Data Access/Integration
Web
AOP
Test


Spring依赖的jar包
核心jar : core , beans , context , expression
日志jar : commons-logging(apache) , log4j


IOC(Inversion of Control) 控制反转
基本概念
概述 : 资源由Spring创建、管理并注入给需要的应用程序或对象
优势 : 资源由Spring统一调度,统一管理,使对象的控制更加集中,避免应用程序中出现多余的对象,浪费资源
IOC基础
1). 创建工程 , 导入spring 的 jar包
2). 创建配置文件 applicationContext.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"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


</beans>
3). 将资源以bean的形式配置在spring的配置文件中
4). 通过spring的执行程序获取bean进行操作
5). 获取spring的通用工厂
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml")(常用)
ApplicationContext context = new FileSystemXmlApplicationContext ("applicationContext.xml")(了解)


DI(Dependency Injection)依赖注入
概述 : 为应用程序提供所依赖的资源的过程 , 称为依赖注入; 应用程序被动等待Spring为其注入资源
setter注入
简单对象 (基本类型,封装类, String)  : <property name="" value=""/>
实体对象 : <property name="userService" ref="userService"/> , ref属性引用spring容器中的bean的id
IOC与DI的关系
针对Spring而言:Spring反向控制(IOC)应用程序需要使用的外部资源
针对应用程序而言:应用程序依赖Spring为其注入(DI)运行所需的资源


BeanFactory接口(了解)
使用BeanFactory获取spring中的bean
1). Resource resource = new ClassPathResource("applicationContext.xml");
2). BeanFactory bf = new XmlBeanFactory(resource );
3). UserService service = (UserService) factory.getBean("userService");
ApplicationContext是BeanFactory的子接口
BeanFactory与ApplicationContext区别
加载方式
BeanFactory:延迟加载,使用bean时才进行初始化
ApplicationContext:加载配置文件时,初始化bean对象 (延时加载的bean的属性里加lazy-init="true")
功能 : ApplicationContext又实现了其他的接口, 具有更为丰富的功能(国际化, 自动装配 ...)
ApplicationContext的常用实现类
FileSystemXmlAppliCationContext 根据文件路径获取
ClassPathXmlApplicationContext  根据类路径获取


Bean实例化的三种方式
无参数构造
静态工厂
1). 创建静态工厂类
2). 配置 : <bean id="bean2" class="cn.itcast.spring.xml.init.Bean.Bean2Factory"  factory-method="getInst"></bean>
class:配置工厂类的全路径名   factory-method:配置工厂类获取实例的静态方法名
实例工厂
1). 创建静态工厂类
2).  <bean id="bean3Factory" class="cn.itcast.spring.xml.init. Bean3Factory"></bean>
3). <bean id="bean3"  factory-bean="bean3Factory" factory-method="getInst"></bean>


bean的作用域(scope)
配置 : 在bean中的scope属性中配置 , 默认单例(singleton)
scope的属性值
singleton(默认)(常用) : 创建出的实例为单例模式,在IoC容器中唯一
prototype(常用) : 创建出的实例为非单例模式,每次获取bean得到新对象
request(用于web开发) : 创建的实例绑定request对象,获取的bean作为request的属性
session (用于web开发) : 创建的实例绑定session对象,获取的bean作为session的属性


bean的生命周期
生命周期的过程(了解)
1). instantiate bean对象实例化
2). populate properties 封装属性
3). 如果Bean实现BeanNameAware执行setBeanName
4). 如果Bean实现BeanFactoryAware或ApplicationContextAware设置工厂setBeanFactory或上下文对象setApplicationContext
5). 如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization
6). 如果Bean实现InitializingBean执行afterPropertiesSet
7). 调用自定义的init-method方法
8). 如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization
9). 执行业务处理
10). 如果Bean实现DisposableBean执行destroy
11). 调用自定义的destroy-method
需要关注
1). 增强bean的功能可以使用后处理Bean,BeanPostProcessor
2). 如果需要初始化或销毁操作我们可以使用init-method  destroy-method


bean的属性注入 .
构造器注入(了解) 
1). 为bean提供对应的构造器
2). 配置: <constructor-arg index="..." type="..."  value="..."></constructor-arg>
setter注入(重要)
简单对象 (基本类型,封装类, String)  : <property name="" value=""/>
实体对象 : <property name="car" ref="car"/> , ref属性引用spring容器中的bean的id


集合注入
List
<property name="country">
<list>
<value>中国</value>
<value>美国</value>
<value>韩国</value>
<value>法国</value>
</list>
</property>
数组
<property name="arr">
<array>
<value>9</value>
<value>1</value>
<value>8</value>
</array>
</property>
Set
<set>
<value>Tom</value>
<value>Jerry</value>
<value>汤姆</value>
<value>杰瑞</value>
<!-- <ref bean="bean名称"/> -->
</set>
Map
<property name="map">
<map>
<entry key="SINA" value="新浪"/>
<entry key="BAID" value="百度"/>
<entry key="NQ" value="网秦"/>
</map>
</property>


或 


<property name="map">
<map>
<entry>
<key>
<value>SINA</value>
</key>
<value>新浪</value>
</entry>
<entry>
<key>
<value>BAID</value>
</key>
<value>百度</value>
</entry>
</map>
</property>
Properties
<property name="propes">
<props>
<prop key="ZSF">张三丰</prop>
<prop key="ZWJ">张无忌</prop>
<prop key="ZZR">周芷若</prop>
</props>
</property>


P/C命名空间支持(了解)
P名称空间(property 属性注入)
1). 在约束信息中加入使用命名空间p  xmlns:p="http://www.springframework.org/schema/p"
2). bean的配置  简单属性格式: p:<属性名>=“属性值”  引用属性格式: p:<属性名>-ref=“bean名称”
C名称空间(constructor 构造器注入)
1). 在约束信息中加入使用命名空间c   xmlns:p="http://www.springframework.org/schema/p"
2). bean的配置   简单属性格式: c:<属性名>=“属性值”  引用属性格式: c:<属性名>-ref=“bean名称”


SpringEL表达式格式(了解)
所有格式统一使用  value=“ . . . ”
常量 : #{10}  #{3.14}  #{2e5}  #{'itcast'}
引用Bean : #{beanId}  
引用Bean属性 : #{beanId.propertyName}
引用Bean方法 : beanId.methodName().method2()
引用静态方法 : #{T(java.lang.Math).PI}
T() 运算符的作用就是调用静态方法
运算符支持 : #{3 lt 4 and 4 ge 3}
集合支持 : #{likes[3]}


Spring注解 
注解开发bean
1). 添加spring-context的命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
2). 注解扫描路径配置 : <context:component-scan base-package="cn.itcast.spring"/> , <context:annotation-config />(可以省略)
3). bean的注解
@Component : 指定对应的类为Spring控制的bean
@Repository : 用于对数据层实现类进行标注
@Service : 用于对业务逻辑层实现类进行标注
@Controller : 用于对控制层实现类进行标注
注解开发属性注入
@Autowired : 标注类的成员变量为自动装配注入属性  
属性required  
true : 注入失败时候抛出异常
false : 不做校验 , 注入失败则就是null
简单类型:@Value , 该注解与@Autowired配合使用
@Autowired //自动装配
@Value("100") //注入简单类型
private Integer pageNum;
对象类型 : @Qualifier ,  该注解与@Autowired配合使用
@Autowired(required=false) //自动装配默认按照类型进行装配
@Qualifier("userService") //注入引用类型,要求制定bean的名称
private UserService userService;
注意 : @Qualifier必须给出注入的bean的名称 ; 如果对应的bean不存在,抛出异常,注入失败


自动装配注意事项
注入类型class
1). 如果类名对应的类只有一个,注入成功
2). 如果类名对应的类有多个,注入失败
注入类型interface
1). 如果不存在对应接口的实现类,注入失败
2). 如果对应接口的实现类为一个,注入成功
3). 如果对应接口的实现类为多个,注入失败
4). 如果对应接口的实现类指定有bean名称
如果存在对应名称的自动装配bean,注入成功
如果不存在对应名称的自动装配bean,注入失败


@Resource完成自动装配
@Resource与@Autowired功能非常相似,用于bean的自动装配,格式略有区别 ; 如: @Resource("userService")


生命周期控制方法与bean的作用范围
bean的作用范围 : @Scope("prototype")
生命周期
@PostConstruct : 为当前Bean指定init-method参数
@PreDestroy : 为当前Bean指定destory-method参数


@Bean的支持性(了解)
1). @Bean : 指定当前方法返回的对象为指定名称spring控制的Bean
2). @Configuration : 指定当前类为配置类,用于加载Bean定义
@Configuration
public class SpringConfig {
@Bean(name="aa") //定义了一个spring的bean
public UserAction getInst(){
return new UserAction();
}
@Bean(name="bb")//通过静态工厂定义一个spring的bean
public static UserAction getInst2(){
return new UserAction();
}
}
3). AnnotationConfigApplicationContext对象手动加载
//免配置文件格式
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
//通过注册的形式将定义的bean加载到AnnotationConfigApplicationContext上下文对象中
ctx.register(SpringConfig.class);
ctx.register(UserAction.class);
//注册完毕后,重新加载一下上下文对象(刷新)
ctx.refresh();


Junit整合Spring
1). 加入jar包 spring-test-3.2.0.RELEASE.jar
2). 设置类运行器 @RunWith(SpringJUnit4ClassRunner.class) (该注解使用在类上)
3). 加载spring的配置文件@ContextConfiguration(locations="classpath:applicationContext.xml")


web开发中的应用
1). 加入spring-web-4.2.4.jar
2). 在web.xml中配置 org.springframework.web.context.ContextLoaderListener
3). xml中配置contextConfigLocation参数 , 加载spring配置文件
4). 获取ApplicationContext : (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)
5). 获取ApplicationContext的方式二 : WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext())
原创粉丝点击