Spring之Core模块
Core模块主要的功能是实现了控制反转与依赖注入、Bean配置以及加载。Core模块中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等概念
BeanFactory
BeanFactory是实例化、配置、管理众多bean的容器
在Web程序中用户不需要实例化Beanfactory,Web程序加载的时候会自动实例化BeanFactory,并加载所欲的Beans,将各个Bean设置到Servlet、Struts的Action中或者Hibernate资源中
在Java桌面程序中,需要从BeanFactory中获取Bean,因此需要实例化BeanFactory,例如,加载ClassPath下的配置文件:
ClassPathResource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory factory = new XmlBeanFactory (res); Iservice service= factory.getBean("service"); …… factory.destroySingletons();或者使用文件流加载任意位置的配置文件
InputStream in = new FileInputStream("C:\\ApplicationContext.xml"); XmlBeanFactory factory = new XmlBeanFactory (in);或者用ClassPathXmlApplicationContext加载多个配置文件(以字符串形式传入)
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( new String [] {"applicationContext.xml","applicationContext-part2.xml"} ); BeanFactory factory = (BeanFactory) appContext; //ApplicationContext继承自BeanFactory接口配置Bean
工厂模式
如果一个bean不能通过new直接实例化,而是通过工厂类的某个方法创建的,需要把<bean>的class属性配置为工厂类(或者吧factory-bean属性配置为工厂类对象)
<bean id="examBean" class = "examples.MyBeanFactory" method="createInstance" /> <!--等价于下面的配置--> <bean id="examBean2" factory-bean = "examples.MyBeanFactory" method="createInstance" />构造函数
如果Bean的构造函数带有参数,需要指定构造函数的参数
<bean id = "examBean" class=" examples.ExampleBean"> <constructor-args><ref bean="anotherBeanId"/></constructor-args> <constructor-args><ref bean="anotherBeanId2"/></constructor-args> <constructor-args><value>1</value></constructor-args> </bean>参数又先后顺序,要与构造函数参数的顺序相同
单态模式
Bean可以定义是否为单态模式,在非单态模式下,每次请求该Bean都会生成一个新的对象
像数据源等一般配置为单态模式
<bean id="exampleBean" class="examples.ExamleBean" singleton="false"/>property属性
destroy-method属性配置关闭方法,如果有配置,在丢弃Java对象时会调用该方法,某些数据源、SessionFactory对象都需要用destroy-method配置关闭方法
<property name="examProperty" value="pValue" />等价于
<property name="examProperty"> <value>pValue</value> </property>注意:
<property name="password"> <value></value> </property>会将password设置为"",而不是null,如果想设置为null应该为
<property name="password"> <null/> </property><ref>属性
Spring配置文件的Bean之间可以相互引用,引用时用<ref>标签配合Bean的id属性使用
也可以使用内部配置
<bean id="dao" class = "com.clf.DaoImpl"></bean> <bean id="serviceImpl" class="com.clf. serviceImpl"> <property name="dao"> <ref bean="dao"/> </property> </bean>等价于内部配置
<property name="dao"> <bean class="com.clf.DaoImpl"/> </property>除了使用<ref>的bean属性,还可以使用local、parent,它们与bean属性的作用是一样的,但是,local只能使用本配置文件中的bean,parent只能使用父配置文件中的bean
<list>属性
<property name="propName"> <list> <value>String、Integer、Double等类型数据</value> <ref bean="dataSource"/> </list> </property><set>属性
<property name="propName"> <set> <value>String、Integer、Double等类型数据</value> <ref bean="dataSource"/> </set> </property><map>属性
<property name="propName"> <map> <entry key="key1"> <value>String、Integer、Double等类型数据</value> </entry> <entry key-ref="key2"> <ref bean="dataSource"/> </entry> </map> </property><props>属性
<property name="propName"> <props> <prop key="url">http://localhost:8080/clf</prop> <prop key="name">clf</prop> </ props > </property><destroy-method>和<init-method>属性
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> …… </bean>Spring在注销这些资源时会调用close方法
有些对象在实例化之后需要执行某些初始化代码,但是这些代码不能写进构造函数,这时候可以把初始化代码写进某个方法中,并用<init-method>指定该方法
<bean id="c" class="com.clf.SpringExample" init-method="init">depends-on属性
Spring会默认按照配置文件里的Bean顺序地实例化Bean,但是有时候实例化A对象之前需要先实例化后面的B对象,这时候可以使用depends-on属性强制先实例化B对象
<bean id="a" clas="com.clf.A" depends-on="b"></bean> <bean id="b" clas="com.clf.B"></bean><idref>与<ref>的区别
<idref>与<ref>的作用是一样的,都是配置Java对象的,不同的是,<idref>只有bean与local属性,没有parent属性
Spring加载XML配置文件时,会检查<idref>配置的Bean在不在,而<ref>只会在第一次调用时才会检查,换句话说,如果Bean不存在,<idref>在启动程序时就会抛出错误,而<ref>只会在运行中抛出错误
<autowire>
可以通过Bean的autowire属性设置自动装配规则。使用autowire后不需要再用<propertyname="" value="" />显式地设置该Bean的属性、依赖关系,Spring会根据反射,自动寻找符合条件的属性,设置到该Bean上
autowire属性定义的不是需要自动装配的属性名,而是自动装配的规则,一旦配置,所有的属性都会遵循autowire定义的规则
No:即不启用自动装配。Autowire默认的值。
byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用Setter方法为其注入。
byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用Setter方法为其注入。
constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Setter方法注入,而是使用构造子注入。
autodetect:在byType和constructor之间自动的选择注入方式。
default:由上级标签<beans>的default-autowire属性确定。
dependency-check
有时候某些Bean的属性配置有错误,这种错误在程序启动的时候不会有任何异常,会一直潜伏到Spring调用该Bean时才会被发现
依赖检查能够检查属性是否被设置,如果配置了依赖检查,程序启动是会进行配置校验,以便及时地发现错误。
但是需要注意的是,依赖检查是很生硬的,例如设置为object,将会检查所有的Java对象属性,只要有一个属性没有设置,就会抛出异常
no或default:不做任何检查,默认
simple:仅检查基本类型、集合属性
object:仅检查Java对象属性
all:检查所有属性
Bean的高级特性
BeanNameAware接口帮助Bean知道自己在配置文件中的id
import org.springframework.beans.factory.BeanNameAware; public class BeanNameTest implements BeanNameAware{ private String beanName; //Spring会调用该方法 public void setBeanName(String beanName){ this.beanName = beanName; } }BeanFactoryAware接口帮助Bean知道哪个BeanFactory实例化了自己
public interface BeanFactoryAware{ void setBeanFactoryAware(BeanFactory beanFactory) throws BeanException; }用法同BeanNameAware
此外还有一下常用的方法
boolean containsBean(String)判定指定名称的Bean是否存在
Object getBean(String)返回指定名称,如果没有该Bean会抛出异常
Object getBean(String,Class)返回指定名称的Bean,并转化为指定的类对象
boolean isSingleton(String)判断指定名称的Bean是否被配置为单态模式
String []getAliases(String)返回指定名称的Bean的别名
InitializingBean接口会在Bean实例化后、所有属性被设置后调用初始化方法。但是使用该接口会与Spring代码发生耦合,因此不推荐使用,Spring推荐使用init-method配置
public interface InitializingBean{ public void afterPropertiesSet(); //初始化时调用此方法 }DisposableBean接口会在Bean对象丢弃时调用销毁方法
public interface DisposableBean{ public void destroy(); //销毁时调用此方法 }属性覆盖器
对于一些参数,更实用更简单的方法是使用properties配置,而不是配置在Spring的配置文件中
PropertyPlaceholderConfigurer允许把XML配置的某些参数配置到properties文件中
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc. username}"/> <property name="password" value="${jdbc. password}"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="loaction" value="classpath:jdbc.properties"> </bean>jdbc.properties
jdbc.driverClassName= com.mysql.jdbc.Driver jdbc.url =jdbc:mysql://localhost:3306/clf?characterEncoding=UTF-8 jdbc.username =clf jdbc.password =admin
Spring之Core模块的更多相关文章
- Spring的Core模块
Core模块主要的功能是实现了反向控制IOC(Inversion of Control)与依赖注入DI(Dependency Injection).Bean配置以及加载.Core模块中有Beans.B ...
- Spring第二篇【Core模块之快速入门、bean创建细节、创建对象】
前言 上篇Spring博文主要引出了为啥我们需要使用Spring框架,以及大致了解了Spring是分为六大模块的-.本博文主要讲解Spring的core模块! 搭建配置环境 引入jar包 本博文主要是 ...
- Spring【AOP模块】就是这么简单
前言 到目前为止,已经简单学习了Spring的Core模块.....于是我们就开启了Spring的AOP模块了...在讲解AOP模块之前,首先我们来讲解一下cglib代理.以及怎么手动实现AOP编程 ...
- 移动商城第三篇【搭建Mybatis和Spring环境、编写Dao、Service在Core模块】
Mybatis和Spring环境搭建 由于我们的所编写的dao层.service代码可能前台和后台都需要用到的,因此我们把环境搭建在core模块中 逆向工程 首先,我们要做的就是品牌管理,我们首先来看 ...
- Spring第三篇【Core模块之对象依赖】
前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容 ...
- Spring第七篇【Spring的JDBC模块】
前言 上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的对JDBC的支持- 对于JDBC而言,我们肯定不会陌生,我们在初学的时候肯定写过非常非常多的JD ...
- Spring【DAO模块】就是这么简单
前言 上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的DAO模块对JDBC的支持,以及Spring对事务的控制... 对于JDBC而言,我们肯定不会陌 ...
- Spring之DAO模块
Spring的DAO模块提供了对JDBC.Hibernate.JDO等DAO层支持 DAO模块依赖于commons-pool.jar.commons-collections.jar Spring完全抛 ...
- [02] Spring主要功能模块概述
1.Spring主要功能模块 1.1 Core Container Spring的核心容器模块,其中包括: Beans Core Context SpEL Beans和Core模块,是框架的基础部 ...
随机推荐
- ●HDU 4787 GRE Words Revenge
题链: http://acm.hdu.edu.cn/showproblem.php?pid=4787 题解: AC自动机(强制在线构造) 题目大意: 有两种操作, 一种为:+S,表示增加模式串S, 另 ...
- poj2975 Nim 胜利的方案数
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5545 Accepted: 2597 Description N ...
- SPOJ 7258 Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- Java8的重要新特性
一.Lambda表达式 java8中Lambda表达式的书写方式: (参数) -> 表达式 (参数) -> 单行语句 (参数) -> { 语句 } 1.Lambda遍历List和Ma ...
- Linux学习之CentOS(八)----文件与目录的默认权限与隐藏权限(转)
文件与目录的默认权限与隐藏权限 一个文件有若干个属性, 包括读写运行(r, w, x)等基本权限,及是否为目录 (d) 与文件 (-) 或者是连结档 (l) 等等的属性! 要修改属性的方法在前面也约略 ...
- kibana使用
最近,被老大叫写一个kibana的使用方法给所有人用. 注意的一点: 能不用空格表示OR或者AND就不用空格表示,因为要么全用要么全部不用,否则会因为解析搜索同级的时候,若出现空格和OR,会冲突覆盖意 ...
- JPA 的 CascadeType 属性 和 FetchType属性 和 各种映射关系
代码地址:https://gitee.com/a247292980/lgp20151222 CascadeType CascadeType.PERSIST级联新增(又称级联保存): CascadeTy ...
- vue mint-ui 实现省市区街道4级联动(仿淘宝京东收货地址4级联动)
demo及源码地址 https://github.com/artiely/citypicker 先去下载一个“省份.城市.区县.乡镇” 四级联动数据,然后 引入 import { Picker } f ...
- 基于PHP的对接免费电子面单接口平台的案例-快宝开放平台
一.电子面单对接平台 电子面单对接平台分为两类: 1 .各大快递公司自有的电子面单接口开放平台:对接起来麻烦,需要每个快递公司分别调试接口,费时费力. 2 .第三方快递开放平台:如快宝开放平台(htt ...
- mongo数据删除和游标
数据删除 db.集合.remove(删除条件,是否只删除一个数据);默认删多条(false)true删除一条db.集合.remove({}) 删除所有元素但集合还在db.集合.drop() 删除集合 ...