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模块,是框架的基础部 ...
随机推荐
- (MariaDB/MySQL)之DML(1):数据插入
本文目录: 1.insert和replace插入数据 1.1 insert into values() 1.2 insert into set 1.3 insert into select_state ...
- [SHOI2008]cactus仙人掌图
[题目描述] 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的回路. 举例 ...
- ●BZOJ 2337 [HNOI2011]XOR和路径
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2337题解: 概率dp, 因为异或的每一位之间没有关系,我们就依次考虑每一位k.(即边权要么为 ...
- 决战 状压dp
决定在这个小巷里排兵布阵.小巷可以抽象成一个们彼此之间并不是十分和♂谐.具体来说,一个哲学家会有一个的矩形.每一位哲学家会占据一个格子.然而哲学家的01矩阵来表示他自己的守备范围.哲学家自己位于这个矩 ...
- Linux下双网卡Firewalld的配置流程
实验室拟态存储的项目需要通过LVS-NAT模式通过LVS服务器来区隔内外网的服务,所以安全防护的重心则落在了LVS服务器之上.笔者最终选择通过firewalld放行端口的方式来实现需求,由于firew ...
- java 左移 右移
public class test{ public static void main(String[] args) { int m = 9; int n = m >> 3; int p = ...
- JS 中判断空值 undefined 和 null
1.JS 中如何判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正 ...
- sssp-springmvc+spring+spring-data-jpa增删改查
环境:IDE:eclipse.jdk1.7.mysql5.7.maven 项目结构图 上面目录结构你可以自己创建 搭建框架 首先加入maven依赖包以及相关插件 <dependencies> ...
- Python的一个解释凯撒密码的程序
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' { Title:CaserCode Author:naiquan Type:crypto Detai ...
- leetcode刷题笔记326 3的幂
题目描述: 给出一个整数,写一个函数来确定这个数是不是3的一个幂. 后续挑战:你能不使用循环或者递归完成本题吗? 题目分析: 既然不使用循环或者递归,那我可要抖机灵了 如果某个数n为3的幂 ,则k=l ...