`
leon.s.kennedy
  • 浏览: 106850 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

Spring IOC AutoWire

 
阅读更多

自动装配:

    a) byName

b)     byType

c)     如果所有的bean都用同一种,可以使用beans的属性:default-autowire

 

 

 <bean name="userDAO" class="com.dao.impl.UserDAOImpl">
   <property name="daoId" value="1"></property>
  </bean>
 
  <bean name="userDAO2" class="com.dao.impl.UserDAOImpl">
   <property name="daoId" value="2"></property>
  </bean>
 
  <bean id="userService" class="com.service.UserService" scope="prototype" >
  </bean>

 

UserDAOImpl中重写toString():

@Override
 public String toString() {
  return "daoId=" + daoId;
 }

 

测试UserService类自动装配UserDAOImpl

  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  UserService service = (UserService)ctx.getBean("userService");
  System.out.println(service.getUserDAO()); //1

 

注意:byType时,如果出现了两个相同类型时,则报错。原因很简单,Spring不知道要注入到哪个中

byName是根据 bean中的属性名

 

The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. The autowiring functionality has five modes. Autowiring is specified per bean and can thus be enabled for some beans, while other beans will not be autowired. Using autowiring, it is possible to reduce or eliminate the need to specify properties or constructor arguments, thus saving a significant amount of typing. [2] When using XML-based configuration metadata, the autowire mode for a bean definition is specified by using the autowire attribute of the <bean/> element. The following values are allowed

Autowiring modes

ModeExplanation
no

No autowiring at all. Bean references must be defined via a ref element. This is the default, and changing this is discouraged for larger deployments, since explicitly specifying collaborators gives greater control and clarity. To some extent, it is a form of documentation about the structure of a system.

byName

Autowiring by property name. This option will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. For example, if you have a bean definition which is set to autowire by name, and it contains a master property (that is, it has asetMaster(..) method), Spring will look for a bean definition namedmaster, and use it to set the property.

byType

Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byTypeautowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting thedependency-check="objects" attribute value specifies that an error should be thrown in this case.

constructor

This is analogous to byType, but applies to constructor arguments. If there isn't exactly one bean of the constructor argument type in the container, a fatal error is raised.

autodetect

Chooses constructor or byType through introspection of the bean class. If a default constructor is found, the byType mode will be applied.

 

分享到:
评论

相关推荐

    spring示例代码好又全.rar

    内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...

    spring学习笔记(有代码有注解解释)

    IoC 自动装载(Autowire);AOP以及如何使用; 适用人群:比较适合与我一样的在校普通大学生进行学习整理,以及适合初学spring的朋友进行巩固加深印象! 阅读建议:需要有一定的代码基础,一定的知识储备

    JAVA spring 系列案例50个和学习资料

    Spring系列第2篇:控制反转(IoC)与依赖注入(DI)。Spring系列第3篇:Spring容器基本使用及原理。Spring系列第4篇:xml中bean定义详解(-)Spring系列第5篇:创建bean实例这些方式你们都知道?Spring系列第6篇:玩转...

    Spring的学习笔记

    八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...

    Spring 2.0 开发参考手册

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    spring chm文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    spring2.5 学习笔记

    八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...

    spring纯注解开发

    1.IOC纯注解开发 @Component @Service @Reposity 2.DI纯注解开发 @Autowire @Qualifire @Value 3.管理第三方的Bean @Bean

Global site tag (gtag.js) - Google Analytics