0

I'm sure I'm making a simple mistake and I have searched for the answer. Basically, my war file builds fine but when I deploy it under Tomcat 7 I get a failure because injection of JdbcTemplate fails. I'll paste the full exception below.

I tried putting the driver jar here:

../webapps/adddrop-1.0.0-SNAPSHOT/WEB-INF/lib

The code is simple enough:

@Autowired 
private JdbcTemplate jdbcTemplate;

In the POM is:

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.2</version>
    </dependency>

In application.properties is:

# JWA Properties for JDBC/Oracle
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@stusys-qa-db-2.xxx.com:1523:abcqa2
spring.datasource.username=jxxxxx
spring.datasource.password=xxxxxxxx

Here is the exception when I try to deploy the WAR:

    Jul 13, 2017 2:57:49 PM org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defin
ed in URL [jar:file:/Applications/apache-tomcat-7.0.78/webapps/adddrop-1.0.214-SNAPSHOT/WEB-INF/lib/csf-common-legacy-2.0.51-20170713.011229-1.jar!/applicationContext-csf-comm
on.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addDropLandingPageSelection
Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.spr
ingframework.jdbc.core.JdbcTemplate edu.mit.addDrop.web.AddDropLandingPageSelectionController.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1092)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1984)
Jim Archer
  • 1,337
  • 5
  • 30
  • 43
  • is it spring boot application? If no did you configured `JdbcTemplate` as bean somewhere in configuration? – bilak Jul 13 '17 at 19:30
  • @bilak no this is my first non-spring boot application. This sounds like it could be the problem, I didn't realize I had to config it separately. - Update: I just checked some tutorials... What has to be configured differently? I see the datasource being configured in XML but I did that is properties. – Jim Archer Jul 13 '17 at 19:33
  • So you are using java configuration or xml configuration? If xml just create there config for JdbcTemplate and inject there your datasource. Maybe [this](https://stackoverflow.com/questions/17868096/using-spring-jdbctemplate-injecting-datasource-vs-jdbctemplate) will help you. – bilak Jul 13 '17 at 19:47

1 Answers1

0

Configure a datasource if you have not done yet, for example:

@Bean
public DataSource dataSource() {
  DriverManagerDataSource ds = new DriverManagerDataSource();
  ds.setDriverClassName("org.h2.Driver");
  ds.setUrl("jdbc:h2:tcp://localhost/~/spitter");
  ds.setUsername("sa");
  ds.setPassword("");
  return ds;
}

and configure the jdbctemplate as follows:

 @Bean
 public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
 }
René Winkler
  • 6,508
  • 7
  • 42
  • 69
  • Thank you, but I'm having some trouble with this solution, thanks to Java 1.6. I created a separate class for the first bean and annotated it with @Configuration. Wherever I put the second bean, Eclipse complains "Syntax error on token. Misplaced construct." I thought this meant that I can only have one bean per class but it;s the only bean... – Jim Archer Jul 13 '17 at 20:12
  • Well the syntax is correct. Its the @Bean it does not link, and that annotation is imported. My original thought was that it didn't like two beans in the same controller so I moved one out... – Jim Archer Jul 13 '17 at 20:20
  • I see it... The parameter should not have the parens and needs a type (duh) – Jim Archer Jul 13 '17 at 20:25
  • Yes that's actually how I tried it (after you removed the @Autowored Datasource). Still no joy, same exception. – Jim Archer Jul 13 '17 at 20:35
  • sorry can not say it from here without seeing your code. actually this has to work... – René Winkler Jul 13 '17 at 20:39
  • Okay thanks, I'll keep at it. After I add that code do I need to modify an XML config file as someone else mentioned above? – Jim Archer Jul 13 '17 at 20:40