0
点赞
收藏
分享

微信扫一扫

基于XML的DI-内部匿名Bean

Spring基于XML的DI-内部匿名Bean

上一篇博文提到,匿名bean,就是没有名称(id)的bean,不能被其他bean用名称获取到,只能通过autowire=”byType”方式获取;如果有某个类,不想被其他类通过任何方式获取,只为某个类提供服务,则需要采用内部匿名类

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="myStudent" class="com.hk.spring.di10.Student" autowire="byType">
<property name="name" value="张三"/>
<property name="age" value="9"/>
<property name="school">
<!-- 内部匿名bean -->
<bean class="com.hk.spring.di10.School">
<property name="schloolName" value="红林小学"/>
</bean>
</property>
</bean>
</beans>


举报

相关推荐

0 条评论