spring基础-Bean的装配方式(三)自动装配

自动装配使用的是<bean>元素的autowire属性。

autowire属性有5个值:

  • byName:根据property的name自动装配
  • byType:根据property的数据类型(type)自动装配
  • constructor:根据构造函数参数的数据类型,进行byType模式自动装配
  • autodetect:发现默认的构造函数,使用constructor模式,否则使用byType模式
  • no:不适用自动装配,依赖必须通过ref元素定义

具体例子实用上一篇文章的例子,更改xml的配置即可,xml配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
     <bean id="userDao" class="cn.assemble.UserDaoImpl"></bean>
     <bean id="userService" class="cn.assemble.UserServiceImpl" autowire="byName"></bean>
     <bean id="userAction" class="cn.assemble.UserAction" autowire="byName"></bean>
</beans>


发布了14 篇原创文章 · 获赞 0 · 访问量 4187

猜你喜欢

转载自blog.csdn.net/sky892012/article/details/54017280