gpt4 book ai didi

java - 如何解决BeanCreationException?

转载 作者:行者123 更新时间:2023-12-02 10:40:18 28 4
gpt4 key购买 nike

正在开发 Spring 4 CRUD 应用程序,该应用程序完全基于注释,并且不使用 XML 配置。

下面是异常的堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.springrest.annotationbased.crudexampleapplication.service.EmployeeService com.springrest.annotationbased.crudexampleapplication.controller.EmployeeController.employeeService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springrest.annotationbased.crudexampleapplication.dao.EmployeeDao com.springrest.annotationbased.crudexampleapplication.service.EmployeeServiceImpl.employeeDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.springrest.annotationbased.crudexampleapplication.dao.EmployeeDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class com.springrest.annotationbased.crudexampleapplication.configuration.HibernateConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.springrest.annotationbased.crudexampleapplication.configuration.HibernateConfiguration.sessionFactory()] threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4851)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

下面是java文件

HibernateConfiguration.java

package com.springrest.annotationbased.crudexampleapplication.configuration;

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.springrest.annotationbased.crudexampleapplication" })
@PropertySource(value = "classpath:application.properties")
public class HibernateConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
props.put("mysql.driver", env.getProperty("mysql.driver"));
props.put("mysql.url", env.getProperty("mysql.url"));
props.put("mysql.user", env.getProperty("mysql.user"));
props.put("mysql.password", env.getProperty("mysql.password"));
// Setting Hibernate properties
props.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
props.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
props.put("hibernate.dialect", env.getProperty("org.hibernate.dialect.MySQLDialect"));
sessionFactory.setPackagesToScan(new String[] { "com.springrest.annotationbased.crudexampleapplication.model" });
sessionFactory.setHibernateProperties(props);
return sessionFactory;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;

}}

MyWebAppInitializer.java

  package com.springrest.annotationbased.crudexampleapplication.configuration;

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {



@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[] { HibernateConfiguration.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[] { WebConfig.class };
}

@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub

return new String[] { "/" };
}
}

WebConfig.java

     package com.springrest.annotationbased.crudexampleapplication.configuration;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.springrest.annotationbased.crudexampleapplication")
public class WebConfig extends WebMvcConfigurerAdapter {



}

EmployeeController.java

@RestController

public class EmployeeController {

@Autowired
EmployeeService employeeService;

//logic goes here

}

EmployeeServiceImpl

  @Service("employeeService")
@Transactional

public class EmployeeServiceImpl implements EmployeeService {

@Autowired
private EmployeeDao employeeDao;

// business logic

}

EmployeeDaoImpl.java

 @Repository("employeeDao")
public class EmployeeDaoImpl implements EmployeeDao {

@Autowired
private SessionFactory sessionFactory;

// persistence logic

}

EmployeeEntity.java

 @Entity
@Table(name="employee")
public class EmployeeEntity implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="empid")
private Integer employeeId;

@Column(name="empname")
private String employeeName;

@Column(name="empAddress")
private String employeeAddress;

@Column(name="salary")
private Long salary;

@Column(name="empAge")
private Integer employeeAge;

// getters and setters

}

应用程序属性

mysql.driver = com.mysql.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/mydatabase
mysql.user = root
mysql.password = root

#Hibernate properties
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.hbm2ddl.auto = update

#C3P0 properties
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150

最佳答案

HibernateConfiguration.java中,您在put方法中给出了自己的一组变量作为键。这是错误的,您必须将其提及为

// Setting JDBC properties
props.put(DRIVER, env.getProperty("mysql.driver"));
props.put(URL, env.getProperty("mysql.jdbcUrl"));
props.put(USER, env.getProperty("mysql.username"));
props.put(PASS, env.getProperty("mysql.password"));

// Setting Hibernate properties
props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
props.put(DIALECT, env.getProperty("hibernate.dialect"));

在这里, DRIVER、URL、USER 等都是静态变量,请确保您已导入以下包,

import static org.hibernate.cfg.Environment.*;

关于java - 如何解决BeanCreationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52965637/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com