Spring4.IoC容器bean配置详解

之前在Spring2.Ioc容器有简单的配置过bean,在bean的配置中有多种配置方式,下面一一介绍。
bean的配置方式
     1.通过全类名(反射)

     2.通过工厂方法(静态工厂方法&实例工厂方法)

     3.FactoyBean


通过全类名方式

配置Bean
    <bean id="Car" class="cc.badboy.entity.Car">
        <property name="name" value="福特"/>
    </bean>
class:全路径必须提供无参构造函数
id:标识容器中的Bean,id属性唯一
注意:当我们初始化ioc容器的时候,ioc容器会实例化单例的bean。
这样配置是不会有问题的因为,有默认无参构造函数,如果没有无参构造会出以下异常
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Car' defined in class path resource [ApplicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cc.badboy.entity.Car]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cc.badboy.entity.Car.<init>()
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Car' defined in class path resource [ApplicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cc.badboy.entity.Car]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cc.badboy.entity.Car.<init>()
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1110)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1055)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at Test.main(Test.java:11)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [cc.badboy.entity.Car]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cc.badboy.entity.Car.<init>()
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1103)
	... 18 more
Caused by: java.lang.NoSuchMethodException: cc.badboy.entity.Car.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
	... 19 more


获取ioc容器的Bean
功过getBean()方法来获取ioc容器的Bean。
getBean("id")  通过Bean的id标识来获取bean
getBean(class)  通过类的class来获取
通过第二种方式来获取Bean需要注意
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [cc.badboy.entity.Car] is defined: expected single matching bean but found 2: Car,Car1
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1031)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)
	at Test.main(Test.java:12)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我的配置文件是这样写的
    <bean id="Car" class="cc.badboy.entity.Car">
        <property name="name" value="福特"/>
    </bean>

    <bean id="Car1" class="cc.badboy.entity.Car">
        <property name="name" value="布加迪"/>
    </bean>
可以看到,我配置了两个Bean,但class属性是同一个类。
当我们使用第二种方式来获取Bean,getBean(Car.class);这样程序不知道你要获取那个Bean.所有就会出现异常。


注入方式

   属性注入,属性注入是使用set方法来注入
   构造函数注入,通过构造函数注入
   工厂注入,一般不用

属性注入例子
    <bean id="Car" class="cc.badboy.entity.Car">
        <span style="color:#ff0000;"><property name="name" value="福特"/></span>
    </bean>
上面红色标记属性注入,name属性:表明属性名;value属性:表明属性值。注意value属性只能注入基本数据类型。
如果需要指定引用数据类型的时候,就需要使用ref属性。ref="id"

构造函数注入例子

我们现在有一个构造方法
    public Car(String name) {
        System.out.println("汽车品牌为:" + name);
    }
配置Bean
    <bean id="Car" class="cc.badboy.entity.Car">
        <span style="color:#ff0000;"><constructor-arg name="name" value="福特"/></span>
    </bean>
上面红色标记为构造函数中的参数,name属性:表明参数列表的中参数名;value属性:参数值
也可以使用index与type属性来之名是第几个参数,或参数类型是什么。如果使用index属性配置参数,需要注意index的属性下标时从0开始的。

如果value属性需要赋值特殊值,例如< >要知道,简括号在xml中是有特殊意义的,所以需要使用<![CDATA[值]]>

集合属性赋值例子
配置集合属性两种方式
第一种方式

    <bean id="Car" class="cc.badboy.entity.Car">
        <constructor-arg name="name">
            <value><![CDATA[福特]]></value>
        </constructor-arg>
    </bean>

    <bean id="person" class="cc.badboy.entity.Person">
        <property name="cars">
            <list>
                <ref bean="Car"></ref>
            </list>
        </property>
    </bean>
第二种方式
    <bean id="Car" class="cc.badboy.entity.Car">
        <constructor-arg name="name">
            <value><![CDATA[福特]]></value>
        </constructor-arg>
    </bean>

    <util:list id="list">
        <ref bean="Car"/>
    </util:list>

    <bean id="person" class="cc.badboy.entity.Person">
        <property name="cars" ref="list"/>
    </bean>
Map集合属性例子
    <bean id="person" class="cc.badboy.entity.Person">
        <property name="cars">
            <map>
                <entry key="AA" value-ref="Car"></entry>
            </map>
        </property>
    </bean>
第二种方式
    <bean id="Car" class="cc.badboy.entity.Car">
        <constructor-arg name="name">
            <value><![CDATA[福特]]></value>
        </constructor-arg>
    </bean>

    <util:map id="map">
        <entry key="AA" value-ref="Car"/>
    </util:map>

    <bean id="person" class="cc.badboy.entity.Person">
        <property name="cars" ref="map"/>
    </bean>
给Properties类属性复制
    <bean id="person" class="cc.badboy.entity.Person">
        <property name="properties">
            <props>
                <prop key="userName">root</prop>
                <prop key="passWord">123456</prop>
            </props>
        </property>
    </bean>



Bean继承配置
    <bean id="Car" class="cc.badboy.entity.Car">
        <constructor-arg name="name">
            <value><![CDATA[福特]]></value>
        </constructor-arg>
    </bean>

    <bean id="person" class="cc.badboy.entity.Person" <span style="color:#ff0000;">parent="Car"</span>>
    </bean>
parent表明要继承那个Bean的配置,子Bean也可以覆盖父Bean中的配置。

Bean的初始化依赖
当我初始化某个Bean时,在此之前必须已经初始化了另一个Bean
    <bean id="Car" class="cc.badboy.entity.Car">
        <constructor-arg name="name">
            <value><![CDATA[福特]]></value>
        </constructor-arg>
    </bean>

    <bean id="person" class="cc.badboy.entity.Person" depends-on="Car">
    </bean>




使用外部属性文件
Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器可以将用户配置Bean的部分内容移到属性文件,使用${var}来获取值。
    <!-- 导入属性文件 -->
    <context:property-placeholder location="属性文件"/>
    
    <bean id="Car" class="cc.badboy.entity.Car">
        <constructor-arg name="name">
            <value>${键}</value>
        </constructor-arg>
    </bean>













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章