Spring的《XML显式装配bean》- 默认自动装配

来源:互联网 发布:windows phone 8.1gdr2 编辑:程序博客网 时间:2024/06/10 23:27

这一blog主要介绍如何在XML中配置默认的自动装配机制

前面两节讲自动装配都是在单独的bean中配置,默认自动装配则是设置在beans 标签里面,表示当前xml默认的自动装配原则:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"    default-autowire="byName">    <bean id="blueberryCheeseCake"        class="spring.ch2.topic2.Cake"        p:name="blueberryCheeseCake" scope="prototype" />    <bean id="jack"        class="spring.ch2.topic2.Chief"        p:name="jack" autowire="constructor" /></beans>

(1)上面再beans标签的最后有一个default-autowire=”byName” 这个标签代表的是这个配置文件里面所有的bean,将优先以这种方式装配,例如上面的byName

(2)当bean里面和头文件里面同时出现autowire,以bean的设置为优先。

0 0