2016-04-21 105 views
2

任何人都可以帮助解决这个问题吗? 我将Spring与zk集成在一起,并且自动连线的服务bean在控制器中始终为空。Spring bean autowiring不起作用

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/> 

更新设置: 我刚刚发现在控制器中的二传手永远不会被调用。

+0

你扫描的包让春天知道它需要为Mainservice – LearningPhase

+1

创建豆请加的servlet-context.xml中 – MaVVamaldo

+0

@LearningPhase是的,我没有扫描包。 – Christine

回答