Spring 自动装配的问题

Dee 2011-09-20 02:22:04
<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-2.0.xsd" default-autowire="no">

<bean id="testService" class="com.ipi.service.impl.TestServiceImpl" autowire="no">
<property name="testDao" ref="testDao"></property>
</bean>
<bean id="testDao" class="com.ipi.dao.impl.TestDaoImpl">
</bean>
<bean id="sfid"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean></beans>
这里
default-autowire="no"
我已经禁用了自动装配 为什么 student = testService.test(); 这里的testService 还是会自动的初始化 ,怎么才能禁用自动注入啊??
...全文
154 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bijanen19861113 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lijing0511 的回复:]
<property name="testDao" ref="testDao"></property> 这个去掉呗
[/Quote]

然后在Service代码中手动获取testDao的bean,在调用此方法的才装配
lijing0511 2011-09-21
  • 打赏
  • 举报
回复
<property name="testDao" ref="testDao"></property> 这个去掉呗
tamink2011 2011-09-21
  • 打赏
  • 举报
回复
ding !!!!!!
飓风zj 2011-09-21
  • 打赏
  • 举报
回复
<bean id="testService" class="com.ipi.service.impl.TestServiceImpl" autowire="no">
<property name="testDao" ref="testDao"></property>
</bean>
你在配置文件已经注入了 肯定是会被实例化的啊
jumpheightway 2011-09-21
  • 打赏
  • 举报
回复
还有一种处理办法是配置成非单实例或者lookup bean可以得到你想要的
jumpheightway 2011-09-21
  • 打赏
  • 举报
回复
<property name="testDao" ref="testDao"></property>
这个配置处理的
和自动装配没有关系
自动装配是在配置的情况下才有效
spring默认是不进行自动装配的
Dee 2011-09-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xlb0311 的回复:]
首先,你没理解这里autowire的意思吧。这里的aotuwire意思是配装testService的时候自动配装testService中的testDAO。

而你的配置文件中有 ref="testDao" 所以autowire="no"对这个bean就起不了作用。autowire="**"的设置就是为了少写几个 ref="**"
[/Quote]

那我如何才能在student = testService.test(); 这里不让他初始化呢,我想手动getbean
xlb0311 2011-09-20
  • 打赏
  • 举报
回复
首先,你没理解这里autowire的意思吧。这里的aotuwire意思是配装testService的时候自动配装testService中的testDAO。

而你的配置文件中有 ref="testDao" 所以autowire="no"对这个bean就起不了作用。autowire="**"的设置就是为了少写几个 ref="**"
dzr1990 2011-09-20
  • 打赏
  • 举报
回复
autowire="no"在文档里的意思是要自动装配这个bean必须通过其他bean引用这个bean的时候才会装配。

我猜你这里是不是有<property name="testDao" ref="testDao"></property>才会自动装配testService的啊,你把ref去掉试试?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧