Spring autowire在多个层面都失败了

时间:2016-03-11 07:51:26

标签: java spring spring-ioc

虽然我正在尝试使用spring dao开发webapi,但是aop。我无法连接依赖项。我是我的代码,请有人帮我解决这个问题。

项目文件夹结构: -

enter image description here

EmployeeDAOImpl.java: -

Type

EmployeeBOImpl: -

 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;
}

}

EmployeeController: -

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;
}

}

config.xml中

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

@Autowired
private EmployeeBOImpl employeeBO;

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

EmployeeTest.java: -

<?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>

例外: -

package com.emp.test;

public class EmployeeTest {

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

}
 }

2 个答案:

答案 0 :(得分:1)

您似乎错过了类路径中的mysql-connector.jar

Could not load JDBC driver class [com.mysql.jdbc.Driver]

答案 1 :(得分: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;