问题
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
回答1:
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();
}
来源:https://stackoverflow.com/questions/60256125/bean-is-not-formed-for-datasource-in-spring-mvc-project