0

I am working on an legacy Spring MVC Project and found issue while deploying to tomcat 8.5

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

However the class has datasource defined .I searched in google and found to add datasource in server-context xml but i don't have any such server-context.xml in my application.

Below is the bean which is throwing error.

<bean id="languageDao"
        class="com.test.daoImpl.LanguageDAOImpl"
        init-method="init">
        <property name="cspLanguageGet" value="csp_LANGUAGE_Get" />
    </bean>

LanguageDAOImpl.class:

public class LanguageDAOImpl implements LanguageDAO {


    private DataSource dataSource;

    private SimpleJdbcCall languageGet;

    private String cspLanguageGet;

    @Qualifier("template")
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Required
    public void setCspLanguageGet(String cspLanguageGet) {
        this.cspLanguageGet = cspLanguageGet;
    }

    public void init() {
        this.languageGet = new SimpleJdbcCall(dataSource).withProcedureName(cspLanguageGet);
        this.languageGet.compile();
    }

//functions

Error stack trace:

Property 'dataSource' is required
        at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:134)
        at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:165)
        at org.springframework.jdbc.core.simple.AbstractJdbcCall.<init>(AbstractJdbcCall.java:87)
        at org.springframework.jdbc.core.simple.SimpleJdbcCall.<init>(SimpleJdbcCall.java:69)
        at com.aexp.travel.docdelivery.tcapp.daoImpl.LanguageDAOImpl.init(LanguageDAOImpl.java:56)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

Can anyoone please where it is wrong?

kinnu
  • 396
  • 2
  • 12
  • 22
  • Nice that it is defined in the class, it is however not getting autowired. Either remove the annotations and autowire in xml, or enable annotation support through `` or by enabling component scanning ``. – M. Deinum Feb 13 '20 at 06:47
  • i do have with the base package in dispatcher-servlet.xml but still facing the error – kinnu Feb 13 '20 at 06:50
  • 1
    Then this class isn't part of that context, but loaded otherwise. For instance by the `ContextLoaderListener` instead of the `DispatcherServlet`. Else it would simply fail before invoking the `init` method because the `@Autowired` wouldn't be fullfilled. – M. Deinum Feb 13 '20 at 06:56
  • How can I add or update then – kinnu Feb 13 '20 at 07:09
  • Do i really need to declare a bean of LanguageDaoImpl in dao-beans.xml if I annotate it with @Repository? – kinnu Feb 13 '20 at 07:17
  • Not if you have a proper component-scan. However that will not invoke the `init` method unless you place `@PostConstruct` on it. – M. Deinum Feb 13 '20 at 07:18
  • Please update the question with complete error stack to understand how the init method is getting triggered – R.G Feb 13 '20 at 07:46
  • @ R.G Please find the error stack trace: – kinnu Feb 17 '20 at 05:54

0 Answers0