-
spring 中配置bean的注入是<property>的name与与ref的id名需要一样吗??5
- <bean id="iService" class="com.lopu.framework.service.IServiceImpl">
- <property name="dao" ref="dao"></property>
- </bean>
- <bean id="fnBean" class="com.lopu.framework.modules.maintain.action.FlightNatureAction" scope="prototype">
- <property name="fnService">//此name必须是iService吗???
- <ref bean="iService" />
- </property>
- </bean>
- public class FlightNatureAction extends ActionSupport implements
- ModelDriven<FlightNature> {
- private IService fnService;
- private List<FlightNature> fnLists = new ArrayList<FlightNature>();
- private FlightNature fnBean = new FlightNature();
- public IService getFnService() {
- return fnService;
- }
- public void setFnService(IService fnService) {
- this.fnService = fnService;
- }
- public List<FlightNature> getFnLists() {
- return fnLists;
- }
- public void setFnLists(List<FlightNature> fnLists) {
- this.fnLists = fnLists;
- }
- @SuppressWarnings("unchecked")
- public String list() throws Exception {
- //如果这样写会报NPE,即fnService没注入成功,但我把整段程序中的fnService改为iService,并改下配置文件就一切正常了
- this.fnLists = this.fnService.loadAll(FlightNature.class);
- return SUCCESS;
- }
- public FlightNature getModel() {
- return this.fnBean;
- }
- }
本人spring菜鸟,多多指教。。。2010年1月21日 19:52
3个答案按时间排序按投票排序
-
采纳的答案
引用<property name="fnService">//此name必须是iService吗???
<ref bean="iService" />
</property>
property name对应了
public void setFnService(IService fnService) {
this.fnService = fnService;
}
那么参数只要是IService类型的就OK了。。
跟下面是一样的
- <bean id="a" class="com.lopu.framework.service.IServiceImpl">
- <property name="dao" ref="dao"></property>
- </bean>
- <bean id="fnBean" class="com.lopu.framework.modules.maintain.action.FlightNatureAction" scope="prototype">
- <property name="fnService">//此name必须是iService吗???
- <ref bean="a" />
- </property>
- </bean>
2010年1月21日 20:00
-
引用<bean id="AAAAA" class="com.lopu.framework.service.IServiceImpl">
<property name="dao" ref="dao"></property>
</bean>
<bean id="fnBean" class="com.lopu.framework.modules.maintain.action.FlightNatureAction" scope="prototype">
<property name="iService">//此name必须是iService吗???
<ref bean="AAAAA" />
</property>
</bean>
你的代码改成这样也是完全可以的,只要ref的bean,和你要注入的类型一直就能注入进去2010年1月21日 20:06
-
引用<bean id="fnBean" class="com.lopu.framework.modules.maintain.action.FlightNatureAction" scope="prototype">
<property name="fnService">//此name必须是iService吗???
<ref bean="iService" />
</property>
</bean>
咱们来看你这段代码
1. <property name="fnService"> 这个是干什么的? 是spring在bean的实例化过程中,把bean封装成beanwraper,然后进行统一的装配,这个name是指的是
com.lopu.framework.modules.maintain.action.FlightNatureAction里的域成员,和你配置文件里的引用<bean id="iService" class="com.lopu.framework.service.IServiceImpl">没有关系,只是实例化这个bean通过action中的set方法,注入进去而已。
<property name="dao" ref="dao"></property>
</bean> -