UserAction 里面注入 UserService
在配置文件中,(不管是注释还是手写)都会有这么一行
<bean id="UserAction" class="UserAction">
<property name="userService" ref="userService" />
</bean>
<bean id="userService" class="UserServiceImpl"/>
因此 注入方式是property name,要与setXxx):
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
因此配置文件是这样也是可以的:
<bean id="UserAction" class="UserAction">
<property name="service" ref="userService" />
</bean>
就需要写成
private UserService service;
public void setService(UserService userService) {
this.service = userService;
}
而你的方式,setUserService(UserServiceImpl userService)
配置文件应该如下,让spring去寻找 bean为 userServiceImpl的id
<bean id="UserAction" class="UserAction">
<property name="service" ref="userService" />
</bean>
<bean id="userServiceImpl" class="UserServiceImpl"/>