相关文章推荐
果断的围巾  ·  SQL ...·  6 月前    · 
彷徨的马铃薯  ·  DataTable 类 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  • an @Configuration class: I use this to generate the entitymanagerfactory
  • an @Entity class: I use this to map to a mysql table instance
  • an @Repository crudrepository interface: this is built in to spring
  • an @Controller class: this is used to test auto-wiring (when I can inject, I will move this to the service layer)
  • an @SpringBootApplication class: this runs my application
  • an application.properties file
  • a pom.xml: this is used for maven and includes mysql jar
  • an entity class mapped to a mysql table and a UserRepository class(annotated with @Repository). I am trying to inject the crudrepository class via @Autowire annotation, but spring is having difficulty creating the datasource for it because it cannot find the mysql driver. What confuses me is that I have the mysql dependency in maven and I am deploying this on a glassfish 4 server, but Spring is trying to use apache tomcat to connect to the database. Below is my stack trace, code snippets, and mysql-connector:

    Relevant Stack Trace (inner exception)

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.driver
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ... 62 common frames omitted
    Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.driver
    

    notice how this is trying to use org.apache.tomcat to create datasource, even though I am using a glassfish server. Does this matter?

    Source Code: the first relevant exception occurs when spring tries to inject the datasource bean that is a parameter to this method and can't create it

    @Configuration
    @EnableAutoConfiguration
    public class AppConfigUtil {
    @Autowired
    @Bean 
    public EntityManagerFactory entityManagerFactory(DataSource dataSource) { 
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); 
        bean.setPackagesToScan("com.connor"); 
        bean.setDataSource(dataSource); 
        return bean.getObject(); 
    

    Maven (pom.xml) for mysql jar:

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    

    application.properties: this is the entirety of my application.properties file spring uses to generate datasource

    spring.datasource.url=jdbc:mysql://localhost:3307/craigslist
    spring.datasource.username=dbadmin
    spring.datasource.password=password
    spring.datasource.driver-class-name=com.mysql.jdbc.driver
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.show_sql=true
    hibernate.format_sql=true
    

    Here is picture of my maven dependencies: the mysql jar is present: downloaded maven dependencies: including mysql jar

    Any ideas on how to fix this?

    You don't need the AppConfigUtil Spring Boot configures all that for you. You don't need the spring.datasource.driver-class-name Spring (and hibernate) will automatically know the driver to load. Also the driver should be com.mysql.cj.jdbc.Driver and not com.mysql.jdbc.driver. – M. Deinum Apr 8, 2019 at 7:12

    Assuming you are using Spring boot.
    Here are some things you can do to make it work (You might have done some of these, I added anything I can think of just to make sure):

  • Remove your AppConfigUtil class because Spring boot will "magically" find the datasource for you if your application.properties is configured right.
  • Add @SpringBootApplication to your ConnerApplication.java class
  • Add spring-boot-starter-data-jpa dependency in your pom
  • Some side notes:
    @Autowire is not intended to be marked with @Bean annotation. @Autowire CAN be used to autowire bean which is annotated with @Bean.

    EDIT :
    I realized that OP wants to deploy it on Glassfish. In that case, you will need to tweak Spring to produce a deployable war file and configure your application container (glassfish) to include a jdbc driver. All of it is doable but requires a lot of efforts.
    I suggested that you go with embedded tomcat approach if you use Spring-boot for new project. It is basically battle-ready. You can hardly go wrong with this.

    Thanks for the advice Minjun. I have taken your advice and "removed" (commented out the whole) appconfig file. I then checked, and the class that runs the program is annotated with the following annotations: @SpringBootApplication @ComponentScan(basePackages={"com.connor"}) @EntityScan(basePackages={"com.connor.model"}) @EnableJpaRepositories(basePackages={"com.connor.dao"}) @EnableAutoConfiguration(). My pom.xml also contains the specified dependency. I clean/install my project again, and still no luck. Is there anything else I should look at? – Connor Butch Jan 4, 2018 at 0:00 If your SpringbootApplication class is at the root package which is com.conner (looks like it is from your picture), then only @SpringBootApplication is sufficient. This might not directly solve the problem though. Is there any new error message? – Minjun Yu Jan 4, 2018 at 0:06 I followed your suggestion and commented out all the annotations but the @SpringBootApplication one on my driver class. However, I am still getting the same error. Should I look at somehow including the mysql jar in the tomcat directory itself? – Connor Butch Jan 4, 2018 at 0:12 I re-read your question and realized you want to deploy the spring application on Glassfish. That means you will tweak Spring to produce a war file instead of "embedded-tomcat-jar" file. You also need to configure glassfish to include a jdbc driver so that when war file is deployed, it will find the jdbc-driver at runtime. All of this requires a lot of efforts, mind let me know why you want to use glassfish for spring while spring already provide a "standard-production-ready-embedded-tomcat" solution? – Minjun Yu Jan 4, 2018 at 0:16 Hi Minjun, I was unaware that spring tries to package my application for deployment on a tomcat server (I had assumed that I could choose whatever server I liked to deploy it, as I have done with regular jee apps in the past). How should I configure my tomcat installation so that it has a mysql jdbc jar; should this be done in maven or via dropping the jar file in the tomcat installation directory? – Connor Butch Jan 4, 2018 at 0:39

    You don't need declare in pom.xml

    spring.datasource.driver-class-name=com.mysql.jdbc.driver

    You can read my configuration in a simple app using Spring Boot here

    Beside, if you're using Spring Boot you don't need use codes "Source Code: the first relevant exception occurs when spring tries to inject the datasource bean that is a parameter to this method and can't create it..."

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.