Spring Boot org springframework beans factory beancreationexception Error creating bean with name

you are scanning the same packages twice resulting in multiple creation of your beans . First in the RootConfig.class which is the main applicationContext

(@ComponentScan(basePackages = "spitter", excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class) }))

and another scan on the servletContext class WebConfig.class;

@ComponentScan({"spitter.web","spitter.data"})

You should seperate your application Config from your webconfig, do the scan this way :

Root:

@ComponentScan(basePackages = "spitter.Data")

WebConfig

@ComponentScan(basePackages = "spitter.web")

and Change the JdbcTemplace config from :

@Bean public JdbcOperations jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }

to

@Bean public JdbcTemplate getJdbcTemplate() { return new JdbcTemplate(dataSource()); }

I have Spring Boot web application. It’s centered around RESTful approach. All configuration seems in place but for some reason MainController fails to handle request. It results in 404 error. How to fix it?

@Controller public class MainController { @Autowired ParserService parserService; @RequestMapping(value="/", method= RequestMethod.GET) public @ResponseBody String displayStartPage(){ return "{hello}"; } }

Application

@Configuration @ComponentScan(basePackages = "") @EnableAutoConfiguration public class Application extends SpringBootServletInitializer{ public static void main(final String[] args) { SpringApplication.run(Application.class, args); } @Override protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) { return application.sources(Application.class); } }

ParserController

@RestController public class ParserController { @Autowired private ParserService parserService; @Autowired private RecordDao recordDao; private static final Logger LOG = Logger.getLogger(ParserController.class); @RequestMapping(value="/upload", method= RequestMethod.POST) public @ResponseBody String fileUploadPage( } }

UPDATE

Seems like MySQL cannot be initialized by Spring….

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.

UPDATE2

application.properties

# Database spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/logparser spring.datasource.username = root spring.datasource.password = root spring.jpa.database = MYSQL spring.jpa.show-sql = true # Hibernate hibernate.dialect: org.hibernate.dialect.MySQL5Dialect hibernate.show_sql: true hibernate.hbm2ddl.auto: update entitymanager.packagesToScan: /

UPDATE4

Seems lite controllers not responding eventhough @RequestMapping are set. Why might it be?

PS. It occurs when I run Maven’s lifecycle test. When running in degub mode in IntelliJ there is no error outputted.

UPDATE5

Also I use this DAO as explained in tutorial….

public interface RecordDao extends CrudRepository<Record, Long> { }

http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/

UPDATE6

I did changed my application properties. And tried every single combination but it refuses to work. ;(

Maven output:

------------------------------------------------------- T E S T S ------------------------------------------------------- Running IntegrationTest Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.365 sec <<< FAILURE! - in IntegrationTest saveParsedRecordsToDatabase(IntegrationTest) Time elapsed: 2.01 sec <<< ERROR! java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.

Answer

Looks like the initial problem is with the auto-config.

If you don’t need the datasource, simply remove it from the auto-config process:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

What is org Springframework beans factory BeanCreationException?

Cause: org.springframework.beans.factory.CannotLoadBeanClassException. Spring throws this exception when it can't load the class of the defined bean. This may occur if the Spring XML Configuration contains a bean that simply doesn't have a corresponding class.

What is error creating bean with name?

BeanCreationException: Error creating bean with name happens when a problem occurs when the BeanFactory creates a bean. If the BeanFactory encounters an error when creating a bean from either bean definition or auto-configuration, the BeanCreationException will be thrown.

How do you fix bean instantiation through factory method failure?

Solution 1 It is necessary to create a new class that extends the abstract class. If the bean factory is attempting to get the bean by using the abstract class name, it can find and associate the implemented class. The exception would then be resolved.

Can not create bean Spring?

There could be numerous reasons why Spring could not able to create a bean with name X, but clue always lies on the detailed stack trace. This error always has some underlying cause e.g. a ClassNotFoundException or a NoClassDefFoundError, which potentially signal a missing JAR file in the classpath.