0

当我尝试使用spring dao开发webapi时,aop。我无法连接依赖项。下面是我的代码,请有人帮我解决这个问题。

项目文件夹结构:-

在此处输入图像描述

EmployeeDAOImpl.java :-

 package com.emp.dao;

 @Repository
 public class EmployeeDAOImpl implements EmployeeDAO {

@Autowired
@Qualifier(value = "datasource")
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

@Override
public int save(Employee emp) throws Exception {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public boolean update(Employee emp) throws Exception {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean delete(Employee emp) throws Exception {
    // TODO Auto-generated method stub
    return false;
}

@Override
public Employee findById(int id) throws Exception {
    // TODO Auto-generated method stub
    return null;
}

@Override
public List<Employee> findAll() throws Exception {
    // TODO Auto-generated method stub
    return null;
}

}

EmployeeBOImpl :-

package com.emp.business;

@Service
public class EmployeeBOImpl implements EmployeeBO{

@Autowired
EmployeeDAOImpl employeeImpl;

public void setEmployeeImpl(EmployeeDAOImpl employeeImpl) {
    this.employeeImpl = employeeImpl;
}


@Override
public int save(Employee emp) throws Exception {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public boolean update(Employee emp) throws Exception {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean delete(Employee emp) throws Exception {
    // TODO Auto-generated method stub
    return false;
}

@Override
public Employee findById(int id) throws Exception {
    // TODO Auto-generated method stub
    return null;
}

@Override
public List<Employee> findAll() throws Exception {
    // TODO Auto-generated method stub
    return null;
}

}

员工控制器:-

package com.emp.controller;
@Controller
public class EmployeeController{

@Autowired
private EmployeeBOImpl employeeBO;

public void setEmployeeBO(EmployeeBOImpl employeeBO) {
    this.employeeBO = employeeBO;
}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.emp.controller" />
<context:component-scan base-package="com.emp.business" />
<context:component-scan base-package="com.emp.aop" />
<context:component-scan base-package="com.emp.dao" />
<context:annotation-config />
<bean id="datasource"    class="org.springframework.jdbc.datasource.DriverManagerDataSource"    autowire-candidate="true">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/student"    />
    <property name="username" value="root" />
    <property name="password" value="password" />
    <!-- <property name="maxTotal" value="20" />
    <property name="maxIdle" value="5" /> -->
    <!-- <property name="maxWaitMillis" value="5000" /> -->
</bean>
<!-- <bean id="empController"     class="com.emp.controller.EmployeeController">

</bean> -->
</beans>

EmployeeTest.java :-

package com.emp.test;

public class EmployeeTest {

public static void main(String args[]){
    ConfigurableApplicationContext cap = new    ClassPathXmlApplicationContext("resources/config.xml");

}
 }

例外 :-

WARNING: Exception encountered during context initialization -      cancelling refresh attempt: 

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: private com.emp.business.EmployeeBOImpl com.emp.controller.EmployeeController.employeeBO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeBOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.emp.dao.EmployeeDAOImpl com.emp.business.EmployeeBOImpl.employeeImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource com.emp.dao.EmployeeDAOImpl.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [resources/config.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
4

2 回答 2

1

看起来你错过了mysql-connector.jar你的类路径:

Could not load JDBC driver class [com.mysql.jdbc.Driver]
于 2016-03-11T08:01:07.310 回答
0

您需要在类路径中包含 mysql jdbc 驱动程序。您可以从https://dev.mysql.com/downloads/connector/j/获取 jar或使用 Maven 依赖项

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

或等效于您的构建系统。

还要避免为您的依赖项自动装配实现类。这将您的应用程序紧密耦合。代替

 @Autowired
EmployeeDAOImpl employeeImpl;

利用

 @Autowired
EmployeeDAO employeeDao;
于 2016-03-11T08:08:38.583 回答