IOC装配Bean:

1.1.1 Spring框架Bean实例化的方式:

提供了三种方式实例化Bean.

* 构造方法实例化:(默认无参数)

* 静态工厂实例化:

* 实例工厂实例化:

无参数构造方法的实例化:

<!-- 默认情况下使用的就是无参数的构造方法. -->

<bean id="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>

静态工厂实例化:

<!-- 第二种使用静态工厂实例化 -->

<bean id="bean2" class="cn.itcast.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>

public class PersonServiceFactory { public static PersonService createPersonService(){ return new PersonServiceImpl(); } }

实例工厂实例化:

<!-- 第三种使用实例工厂实例化 -->

<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>

<bean id="bean3Factory" class="cn.itcast.spring3.demo2.Bean3Factory"/>

public class PersonServiceFactory { public PersonService createPersonService(){ return new PersonServiceImpl(); } }

1.1.2 Bean的其他配置:

一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称

id 属性在IoC容器中必须是唯一的

id 的命名要满足XML对ID属性命名规范 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

如果Bean的名称中含有特殊字符,就需要使用name属性 例如: <bean name="#person" class="cn.itcast.bean.Person"/>

因为name属性可以相同,所以后出现Bean会覆盖之前出现的同名的Bean

id和name的区别:

id遵守XML约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

name没有这些要求

***** 如果bean标签上没有配置id,那么name可以作为id.

***** 开发中Spring和Struts1整合的时候, /login.

<bean name=”/login” class=””>

现在的开发中都使用id属性即可.

类的作用范围:

scope属性 :

* singleton :单例的.(默认的值.)

* prototype :多例的.

* request :web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();

* session :web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();

* globalSession :一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

实际开发中主要使用singleton,prototype

Bean的生命周期:

配置Bean的初始化和销毁的方法:

配置初始化和销毁的方法:

* init-method=”setup”

* destroy-method=”teardown”

执行销毁的时候,必须手动关闭工厂,而且只对scope=”singleton”有效.

Bean的生命周期的11个步骤:

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.调用<bean init-method="init"> 指定初始化方法 init

8.如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization

9.执行业务处理

10.如果Bean实现 DisposableBean 执行 destroy

11.调用<bean destroy-method="customerDestroy"> 指定销毁方法 customerDestroy

在CustomerService类的add方法之前进行权限校验?

1.1.3 Bean中属性注入:

Spring支持构造方法注入和setter方法注入:

构造器注入:

<bean id="car" class="cn.itcast.spring3.demo5.Car">

<!-- <constructor-arg name="name" value="宝马"/>

<constructor-arg name="price" value="1000000"/> -->

<constructor-arg index="0" type="java.lang.String" value="奔驰"/>

<constructor-arg index="1" type="java.lang.Double" value="2000000"/>

</bean>

setter方法注入:

<bean id="car2" class="cn.itcast.spring3.demo5.Car2">

<!-- <property>标签中name就是属性名称,value是普通属性的值,ref:引用其他的对象 -->

<property name="name" value="保时捷"/>

<property name="price" value="5000000"/>

</bean>

setter方法注入对象属性:

<property name="car2" ref="car2"/>

名称空间p:注入属性:

Spring2.5版本引入了名称空间p.

p:<属性名>="xxx" 引入常量值

p:<属性名>-ref="xxx" 引用其它Bean对象

引入名称空间:

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

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">

<bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="宝马" p:price="400000"/>

<bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="童童" p:car2-ref="car2"/>

SpEL:属性的注入:

Spring3.0提供注入属性方式:

语法:#{表达式}

<bean id="" value="#{表达式}">

<bean id="car2" class="cn.itcast.spring3.demo5.Car2">

<property name="name" value="#{'大众'}"></property>

<property name="price" value="#{'120000'}"></property>

</bean>

<bean id="person" class="cn.itcast.spring3.demo5.Person">

<!--<property name="name" value="#{personInfo.name}"/>-->

<property name="name" value="#{personInfo.showName()}"/>

<property name="car2" value="#{car2}"/>

</bean>

<bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">

<property name="name" value="张三"/>

</bean>

1.1.4 集合属性的注入:

<bean id="collectionBean" class="cn.itcast.spring3.demo6.CollectionBean">

<!-- 注入List集合 -->

<property name="list">

<list>

<value>童童</value>

<value>小凤</value>

</list>

</property>

<!-- 注入set集合 -->

<property name="set">

<set>

<value>杜宏</value>

<value>如花</value>

</set>

</property>

<!-- 注入map集合 -->

<property name="map">

<map>

<entry key="刚刚" value="111"/>

<entry key="娇娇" value="333"/>

</map>

</property>

<property name="properties">

<props>

<prop key="username">root</prop>

<prop key="password">123</prop>

</props>

</property>

</bean>

1.1.5 加载配置文件:

一种写法:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml",”bean2.xml”);

二种方法:

<import resource="applicationContext2.xml"/>

Spring 框架 详解 (三)-----IOC装配Bean的更多相关文章

  1. spring框架详解: IOC装配Bean

    1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. 构造方法实例化:(默认无参数) 静态工厂实例化: 实例工厂实例化: 无参数构造方法的实例化: <!-- 默认情况下使用 ...

  2. Spring框架详解介绍-基本使用方法

    1.Spring框架-控制反转(IOC) 2.Spring框架-面向切面编程(AOP) 3.Spring 内置的JdbcTemplate(Spring-JDBC) Spring框架-控制反转(IOC) ...

  3. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  4. Spring 框架 详解 (二)

    Spring的入门的程序: 1.1.1 下载Spring的开发包: spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包 * docs :sprin ...

  5. spring框架详解

    把之前分享的spring框架整理一份放在这里. 整体架构: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 框架图(选自:http://docs.spring.io/spr ...

  6. Spring 框架 详解 (一)

    Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架 * 分层: * SUN提供的EE的三层结构:web层.业务层.数据访问层(持久层,集成层) * Struts2是 ...

  7. 【59】Quartz+Spring框架详解

    什么是Quartz Quartz是一个作业调度系统(a job scheduling system),Quartz不但可以集成到其他的软件系统中,而且也可以独立运行的:在本文中"job sc ...

  8. spring AOP详解三

    CGLib采用非常底层的字节码技术,可以为一个类创建子类,并在子类中采用方法拦截的结束拦截所有父类方法的调用,并顺势织入横切逻辑.我们采用CGLib技术可以编写一个可以为任何类创建织入横切逻辑代理对象 ...

  9. Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计

    在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...

随机推荐

  1. 将Excel导入数据库

    在Control 中: public ActionResult ImportExcel() { return View(); } //客户导入 [HttpPost] public ActionResu ...

  2. oracle的用户

    1:创建用户 create user zhaoyb identified by zhaoyb default tablespace HUAPUSALEDB create user 用户名 identi ...

  3. install chrome in elementary os

    Elementary OS Freya 0.3.2 was officially out for public. As previous release, it comes pre-installed ...

  4. SlidingMenu实现app侧滑功能

    很多APP都有侧滑菜单的功能,部分APP左右都是侧滑菜单~SlidingMenu 这个开源项目可以很好帮助我们实现侧滑功能,如果对SlidingMenu 还不是很了解的童鞋,可以参考下本篇博客. 本片 ...

  5. MySQL OnlineDDL

    参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...

  6. SQL算术数字的默认类型

    select 100*100*100*100*100 --错误:将 expression 转换为数据类型 int 时出现算术溢出错误. select   cast(1000 as  bigint) * ...

  7. post 405 method not allowed

    HTTP 405 错误 – 方法不被允许 (Method not allowed) 介绍 HTTP 协议定义一些方法,以指明为获取客户端(如您的浏览器或我们的 CheckUpDown 机器人)所指定的 ...

  8. 转载WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  9. vim查找/替换字符串

    1.:s 命令来替换字符串. :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n,$s/vivia ...

  10. bodybuilding

    增大肌肉块的14大秘诀:大重量.低次数.多组数.长位移.慢速度.高密度.念动一致.顶峰收缩.持续紧张.组间放松.多练大肌群.训练后进食蛋白质.休息48小时.宁轻勿假. 1. 大重量.低次数:健美理论中 ...