[英]Error creating bean with name 'employeeController': Injection of autowired dependencies failed
[英]Error creating bean with name 'homeController': Injection of autowired dependencies failed
我正在嘗試在春季創建電子商務。 在項目中包含“ Hibernate”和“ H2”數據庫后,出現錯誤。 錯誤在下面給出。 我正在非常努力,但未找到任何解決方案。
錯誤:
org.springframework.beans.factory.BeanCreationException:創建名稱為'homeController'的bean時出錯:自動連接依賴項的注入失敗; 嵌套的異常是org.springframework.beans.factory.BeanCreationException:無法自動裝配字段:私有com.home.dao.ProductDao com.home.controller.homeController.productDao; 嵌套的異常是org.springframework.beans.factory.BeanCreationException:創建名稱為'productDaoImpl'的bean時出錯:自動連接依賴項的注入失敗; 嵌套的異常是org.springframework.beans.factory.BeanCreationException:無法自動連線字段:私有org.hibernate.SessionFactory com.home.dao.impl.ProductDaoImpl.sessionFactory; 嵌套的異常是org.springframework.beans.factory.BeanCreationException:創建在ServletContext資源[/WEB-INF/applicationContext.xml]中定義的名稱為'sessionFactory'的bean時出錯:調用init方法失敗; 嵌套的異常是org.hibernate.exception.GenericJDBCException:無法打開JDBC連接以執行DDL
applicationContext.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/test" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.home</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
home-servlet.xml
<context:component-scan base-package="com.home">
<context:include-filter type="aspectj" expression="com.home.*" />
</context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**"
location="/WEB-INF/resources/" cache-period="31556926" />
<tx:annotation-driven />
web.xml
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/home-servlet.xml,
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
homeController.java
@Controller
@Configuration
public class homeController {
@Autowired
private ProductDao productDao;
@RequestMapping("/")
public String home() {
return "views/home";
}
@RequestMapping("/productList")
public String getProducts(Model model) {
List<Product> products = productDao.getAllProducts();
model.addAttribute("products", products);
return "views/productList";
}
@RequestMapping("/productList/viewProduct/{productId}")
public String viewProduct(@PathVariable String productId, Model model) throws IOException{
Product product = productDao.getProductById(productId);
model.addAttribute(product);
return "views/viewProduct";
}
}
產品DaoImpl.java
@Repository
@Transactional
public class ProductDaoImpl implements ProductDao {
@Autowired
private SessionFactory sessionFactory;
public void addProduct(Product product) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(product);
session.flush();
}
public Product getProductById(String id) {
Session session = sessionFactory.getCurrentSession();
Product product = (Product) session.get(Product.class, id);
session.flush();
return product;
}
public List<Product> getAllProducts() {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Product");
List<Product> products = query.list();
session.flush();
return products;
}
public void deleteProduct (String id) {
Session session = sessionFactory.getCurrentSession();
session.delete(getProductById(id));
session.flush();
}
}
Product.java代碼:
@Entity
公共類產品{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String productId;
private String productName;
private String productCategory;
private String productDescription;
private double productPrice;
private String productCondition;
private String productStatus;
private int unitInStock;
private String productManufacturer;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductCategory() {
return productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public String getProductCondition() {
return productCondition;
}
public void setProductCondition(String productCondition) {
this.productCondition = productCondition;
}
public String getProductStatus() {
return productStatus;
}
public void setProductStatus(String productStatus) {
this.productStatus = productStatus;
}
public int getUnitInStock() {
return unitInStock;
}
public void setUnitInStock(int unitInStock) {
this.unitInStock = unitInStock;
}
public String getProductManufacturer() {
return productManufacturer;
}
public void setProductManufacturer(String productManufacturer) {
this.productManufacturer = productManufacturer;
}
}
ProductDao.java代碼:公共接口ProductDao {
void addProduct(Product product);
Product getProductById(String id);
List<Product> getAllProducts();
void deleteProduct(String id);
}
項目結構或目錄圖像: 我的Eclipse Oxygen IDE上的項目結構或目錄
ProductDao不是bean。這就是原因。 倉庫,控制器,服務都是bean的類型。 確保這是哪種類型的豆.....謝謝。
最后,當我使用IntelliJ IDEA IDE時發現了自己的問題。 問題如下:
我的問題發生在pom.xml文件中。 在這里,我使用了不支持import org.hibernate.Query的 hibernate-core最新版本( 5.4.0.Final )依賴關系; 包,也不支持Query query = session.createQuery(“ from Product”); 和產品product =(Product)session.get(Product.class,id); ProductDaoImpl.java類中的代碼。
我還在pom.xml文件中使用了spring-webmvc , spring-core和spring-orm依賴關系的最新版本。 為此,它會發生版本沖突。
解:
忘記Eclipse並避免使用它。 請使用IntelliJ IDEA 。 這是用於Java Spring MVC框架的非常用戶友好的IDE,並且還顯示您做錯了什么。
創建一個新項目,並在pom.xml文件中使用hibernate-core 4.0.1.Final版本依賴性,還使用4.2.8.RELEASE版本的自己, spring-core和spring-orm依賴性。
謝謝 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.