Spring bean 自动装配不工作
Spring bean autowiring not work
有人可以帮忙解决这个问题吗?
我将 Spring 与 zk 集成,自动连接的服务 bean 在控制器中始终为 null。
package com.test.MVC.controller;
@Controller
@Scope("prototype")
public class MainController extends SelectorComposer<Component>{
private MainService mainService;
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
List<Map<String, Object>> list = mainService.query(null);//the mainService is always null here
}
@Autowired
public void setMainService(MainService mainService) {
this.mainService = mainService;
}
}
package com.test.MVC.service.impl;
@Service("mainService")
public class MainServiceImpl implements MainService {//MainService: surface
private JdbcTemplate jdbcTemplate;
public List<Map<String, Object>> query(Integer number){...}//please ignore the content
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {//inject bean
this.jdbcTemplate = jdbcTemplate;
}
}
我确实在 xml 文件中添加了以下设置
<context:component-scan base-package="com.test.MVC" />
<context:annotation-config/>
更新:
我刚刚发现控制器中的 setter 永远不会被调用。
这是因为您是在 ZK 容器中搜索,而不是在 spring 容器中搜索。
Zk 有一个特定的 variabeleResolver。
请阅读:https://www.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Wire_Variables
有人可以帮忙解决这个问题吗? 我将 Spring 与 zk 集成,自动连接的服务 bean 在控制器中始终为 null。
package com.test.MVC.controller;
@Controller
@Scope("prototype")
public class MainController extends SelectorComposer<Component>{
private MainService mainService;
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
List<Map<String, Object>> list = mainService.query(null);//the mainService is always null here
}
@Autowired
public void setMainService(MainService mainService) {
this.mainService = mainService;
}
}
package com.test.MVC.service.impl;
@Service("mainService")
public class MainServiceImpl implements MainService {//MainService: surface
private JdbcTemplate jdbcTemplate;
public List<Map<String, Object>> query(Integer number){...}//please ignore the content
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {//inject bean
this.jdbcTemplate = jdbcTemplate;
}
}
我确实在 xml 文件中添加了以下设置
<context:component-scan base-package="com.test.MVC" />
<context:annotation-config/>
更新: 我刚刚发现控制器中的 setter 永远不会被调用。
这是因为您是在 ZK 容器中搜索,而不是在 spring 容器中搜索。
Zk 有一个特定的 variabeleResolver。
请阅读:https://www.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Wire_Variables