属性 'dataSource' 的 JDBC Spring 是必需的或 NullPointer



当我在mapRow完成后运行compile()时。这个错误给了我Property 'dataSource' is required。我的xml 上有这个

<bean id="ProcedureRepository" class="mypackage.ProcedureRepository">
    <property name="dataSource" ref="dataSource"/>
</bean>

我的java ProcedureRepository

private DataSource dataSource;
@Resource
@Qualifier("dataSource")
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}
public String searchCode(String code){
    new SeachCode(dataSource).execute(code);
    return code;
}

我的SeachCode

public class SearchCode extends StoredProcedure{
public SearchCode(DataSource dataSource) {
    super(dataSource, "MYPROC");
...
compile();

当我称之为时就会发生这种情况

ProcedureRepository procedureRepository = new ProcedureRepository();
procedureRepository.searchCode(parameters.code);

我不知道我遗漏了什么,我试图在我的xml中添加新的bean,但没有成功,在SearchCode 添加新的setDataSource也是如此

当您使用new创建存储库时,您只会得到一个不由Spring容器管理的对象。如果您想获得一个Spring bean,它具有Spring注入的所有依赖项,您可以从Spring应用程序上下文中获得它。例如:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("path to spring app context xml file)");    
ProcedureRepository repo= (ProcedureRepository) applicationContext.getBean("ProcedureRepository");
repo.searchCode(parameters.code)

在这里,您可以阅读更多关于从Spring应用程序上下文获取bean的信息。

最新更新