手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Spring基础学习笔记-Bean的基础知识

[日期:2016-07-05] 来源:Linux社区  作者:victoria-c [字体:大 中 小]

一、 Bean的定义,初始化,使用和销毁
二、ref指定依赖的三种模式
三、Bean的五种自动装配模式(autowire)
四、Bean依赖检查的4种模式:配合atuowire使用,dependency-check=""
五、集合的注入方式
六、管理Bean

config.xml文件
<!--Bean的配置文档-->
<!--首先定义为XML的方式来存储Bean的配置-->
<?xml version="1.0" encoding="UTF-8"?>
<!--声明使用的是http://www.springframework.org/dtd/spring-beans.dtd-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframenwork.org/dtd/spring-beans.dtd">
<!--配置Bean的开始,根节点Beans中包含一个或多个Bean元素-->
<beans>
    <bean id="" class="" init-mehod="" destory-method="" autowire="" dependecy-check="">
        <property>
            <value></value>
        </property>   
    </bean>
   
</beans>

一、 Bean的定义,初始化,使用和销毁

1.Bean的定义
2.初始化
 init-method方法
 实现InitializingBean接口,增加afterPropertiesSet()方法;
3.Bean的使用
使用有三种方式:
1)Beanwrapper
HelloWorld hellworld=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(helloworld);
bw.setPropertyValue("msg","helloworld");
bw.getPropertyValue("msg");

2)BeanFactory
InputStream is=new FileInputStream("config.xml");
xmlBeanFactory factory=new xmlBeanFactory(is);
HelloWorld helloWorld=(HelloWorld)factory.getBean("HelloWorld");
helloWorld.getMsg();

3)ApplicationContext
ApplicationContext actx =new FileSystemXmlApplicationContext("config.xml");
HelloWorld HelloWorld = (HelloWorld)actx.getBean("HelloWorld");
HelloWorld.getMsg();


4.Bean的销毁
1)destory-method
2)实现org.springframework.beans.factory.DisposableBean接口,增加destory()方法

二、ref指定依赖的三种模式
1.local
2.bean
3.parent
<property>
    <ref="local"/>||<ref="bean"/>||<ref="parent"/>
<property>

三、Bean的五种自动装配模式(autowire)
1.byName
2.byType
3.constructor
4.autodetect:有constructor就调用constructor,没有的用byType
5.no:默认,不自动装配

四、Bean依赖检查的4种模式:配合atuowire使用,dependency-check=""
1.simple 对基本数据类型,字符串等进行检查
2.object 对于依赖的对象进行检查
3.all (包含simple和object)
4.none

五、集合的注入方式
1.list-Lis
<property name="">
    <list>
        <value></value>
        <value></value>
        <value></value>
    </list>
</property>
2.set-Set
<property name="">
    <set>
        <value></value>
        <value></value>
        <value></value>
    </set>
</property>
3.map-Map
<property name="">
<map>
    <entry key="">
        <value></value>
    </entry>
</map>
</property>

4.props-Properties
<property name="">
    <props>
        <prop key="">HelloWorld</prop>
    </props>
</propertiry>


六、管理Bean
1.BeanWrapper
2.BeanFactory
3.ApplicationContext

Spring中如何配置Hibernate事务 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm

运用Spring注解实现Netty服务器端UDP应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的详细介绍:请点这里
Spring 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/132913.htm

linux
相关资讯       Spring Bean  Spring基础学习