<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="ink2" class="hr.BlackInk"></bean>
<bean id="ink" class="hr.ColorInk"> </bean>
<bean id="page" class="hr.B5Page"></bean>
<!-- autowire="byName" 根据名称自动 注入 -->
<!--autowire="byType" 根据属性的类型自动找到相应的类 注入 -->
<bean id="printer" class="hr.Printer" autowire="byType">
<!--
<property name="ink" ref="ink2"></property>
<property name="page" ref="page"></property>
-->
</bean>
</beans>
Spring的自动注入
最新推荐文章于 2025-11-27 10:55:11 发布
本文展示了一个使用Spring框架进行Bean配置的例子,并演示了如何通过byType的方式实现依赖注入。这种方式简化了配置文件,避免了显式指定依赖关系。
2217

被折叠的 条评论
为什么被折叠?



