0

I am facing error "Property datasource is required".

Below is the configuration in dao-beans xml.

<bean id="Template" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:/comp/env/jdbc/TEMPLATES" />
    </bean>

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

Class has the following configurations:

private DataSource Template;

    private SimpleJdbcCall languageGet;

    private String cspLanguageGet;

    @Autowired
    @Qualifier("Template")
    public void setTemplate(DataSource Template) {
        this.Template = Template;
    }

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

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

I tried with the many solutions which I found over but no luck.I can't use since my java version is 1.8

Below is the error stack trace.

java.lang.IllegalArgumentException: 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)

Context.xml deployed in tomcat has the following configuration:

<Resource
    name="jdbc/TEMPLATES"
    auth="Container"
    type="javax.sql.DataSource"
    maxActive="100"
    maxIdle="30"
    maxWait="10000"
    driverClassName="net.sourceforge.jtds.jdbc.Driver"
    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
    url="jdbc:jtds:sqlserver://gtwtdwdbsqlv001.gbt.gbtad.com:1433;databaseName=Template"
    username="Test"
    password="*******"
    />

I am stuck in this from past 3 days.Not able to resolve this.Can anyone pls help me to understand where it is going wrong

kinnu
  • 396
  • 2
  • 12
  • 22
  • please refer https://stackoverflow.com/questions/36575383/why-use-jndiobjectfactorybean-to-config-jndi-datasource-did-not-work – clevertension Feb 17 '20 at 05:05
  • @clevertension,but all of these bean configuration are in my xml file,so how will I call afterPropertiesSet() on jndiObjectFactoryBean – kinnu Feb 17 '20 at 05:15
  • @clevertension,Can you pls suggest – kinnu Feb 17 '20 at 05:45
  • try to configure resource from "jdbc/TEMPLATES" to "/jdbc/TEMPLATES" – clevertension Feb 17 '20 at 07:33
  • Please don't ask [the same question twice](https://stackoverflow.com/questions/60201716/invocation-of-init-method-failed-nested-exception-is-java-lang-illegalargumente)! Instead update your original one with additional information. – M. Deinum Feb 17 '20 at 08:26

1 Answers1

0

It depends which version of Spring you are using but You may include DataSoure bean as shown below

<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:file:C:/temp/test" />
    <property name="username" value="sa" />
    <property name="password" value="" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />
</bean>

OR

@Bean
public DataSource getDataSource() {
  DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
  dataSourceBuilder.driverClassName("org.h2.Driver");
  dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");
  dataSourceBuilder.username("sa");
  dataSourceBuilder.password("");
  return dataSourceBuilder.build();
}
Swarit Agarwal
  • 2,520
  • 1
  • 26
  • 33
  • No you don't. You can do a JNDI lookup perfectly fine. – M. Deinum Feb 17 '20 at 08:24
  • @M.Deinum Is answer irrelevant or you just provided additional way. On what basis it has been downgraded please elaborate – Swarit Agarwal Feb 17 '20 at 08:26
  • Because you can do a JNDI lookup as well. There is no MUST way to define a datasource. If you have a datasource managed by your app server, you do a JNDI lookup you don't re-define the datasource again. – M. Deinum Feb 17 '20 at 08:27
  • Language can be edited but downgrading isn't a solution. This kind of behaviour isn't acceptable – Swarit Agarwal Feb 17 '20 at 08:28
  • Nor will this solve the error by the user, so it isn't an answer to the question as well. As that is a matter of wrongly configured XML not of a wrongly configured datasource (or datasource lookup as the topic starter already has). – M. Deinum Feb 17 '20 at 08:29
  • The error user has received is clearly mentioning "Datasource" bean isn't available. There are multiple ways to solve that. And defining bean of ds is one of them. I don't think answer has some improper information – Swarit Agarwal Feb 17 '20 at 08:31
  • Which comes from the `init` method. If the JNDI lookup would fail, the error would be a different one. So the answer won't help in solving the error in the init. Which is due to wrongly or missing configuration. Propably annotation processing because he is mixing XML and annotation config. – M. Deinum Feb 17 '20 at 08:43