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

一、 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




原文地址:https://www.cnblogs.com/victoria-c/p/5642283.html