2017-04-30 62 views
0

我需要您的帮助来解决有关spring bean初始化的问题。下面是该问题的详细信息:使用ApplicationContext初始化bean时类抛出异常

DAO接口

package com.dao; 

    import com.entity.Employee; 

    public interface IEmployeeDao { 

     Employee create(final Employee aEmployee); 
     Employee fetchEmployeeById(final Integer aEmployeeId); 
    } 

在DAO实现类

package com.dao.impl; 

    import javax.persistence.EntityManager; 

    import com.dao.IEmployeeDao; 
    import com.entity.Employee; 

    public class EmployeeDao implements IEmployeeDao { 

     private EntityManager em; 

     @Override 
     public Employee create(Employee aEmployee) { 
      return this.em.merge(aEmployee); 
     } 

     @Override 
     public Employee fetchEmployeeById(Integer aEmployeeId) { 
     return this.em.find(Employee.class, aEmployeeId); 
     } 
    } 

主类:

package com.client; 

    import java.sql.SQLException; 

    import org.apache.logging.log4j.LogManager; 
    import org.apache.logging.log4j.Logger; 
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 

    import com.dao.impl.EmployeeDao; 

    public class Client { 
     private static final Logger LOGGER = LogManager.getLogger(Client.class); 

     public static void main(String[] args) throws ClassNotFoundException, 
      SQLException { 
     ApplicationContext applicationContext = new 
     ClassPathXmlApplicationContext(new String[] {"applicationcontext.xml"}); 
     LOGGER.info("client invoked"); 
     EmployeeDao employeeDao = 
     (EmployeeDao)applicationContext.getBean("employeeDao");  
     //....some code below 
    } 
    } 

出代码中的最后一行我正试图让员工豆豆抛出以下异常:

Exception in thread "main" java.lang.ClassCastException: 
    com.sun.proxy.$Proxy17 cannot be cast to 
    com.dao.impl.EmployeeDao 
    at com.client.Client.main(Client.java:26) 

在一些我浏览答案的建议,我改变了上面的线转换为一个接口,而不是它的实现类

IEmployeeDao employeeDao = 
     (IEmployeeDao)applicationContext.getBean("employeeDao"); 

豆是越来越没有任何异常注射,但现在实际执行的方法不被现在IE调用当我打电话像下面

employeeDao.create(new Employee()); 

的创建方法的实现是没有得到所谓的即下面的方法创建EmployeedDao的方法:

@Override 
    public Employee create(Employee aEmployee) { 
     System.out.println("com.dao.impl.EmployeeDao.create(Employee) callled") 
     return this.em.merge(aEmployee); 
    } 

我通过将系统输出消息确认它。

请告诉我我做错了什么,或者可以做些什么来摆脱这个问题。我也发布了下面的bean配置文件和maven dependencies。

applicationContext.xml中:

<?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:aop="http://www.springframework.org/schema/aop" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> 
    <import resource="aspects.xml"/> 
    <bean id="employeeDao" class="com.dao.impl.EmployeeDao" /> 

    </beans> 

aspects.xml

<?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:aop="http://www.springframework.org/schema/aop" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> 

     <bean id="logAspect" 
       class="com.logging.LoggingAspect" /> 

     <aop:config> 

      <aop:aspect id="aspectLoggging" ref="logAspect"> 
      <aop:pointcut id="pointCutAround" 
       expression="execution(* 
     com.dao.IEmployeeDao.*(..))" /> 

     aop:around method="logAround" pointcut-ref="pointCutAround" /> 

    </aop:aspect> 
    </aop:config> 
    </beans> 

行家依赖关系:

<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
     <version>2.5.5</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>2.5.5</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>1.7.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.7.3</version> 
    </dependency>  
    <dependency> 
     <groupId>org.apache.derby</groupId> 
     <artifactId>derby</artifactId> 
     <version>10.13.1.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>3.6.0.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>2.8.2</version> 
    </dependency> 
    </dependencies> 

编辑:

LoggingAspect类:

package com.logging; 

    import org.apache.logging.log4j.LogManager; 
    import org.apache.logging.log4j.Logger; 
    import org.aspectj.lang.JoinPoint; 

    public class LoggingAspect { 
     private static final Logger LOGGER = 
      LogManager.getLogger(LoggingAspect.class); 
     public void logAround(JoinPoint aJoinPoint) { 
       LOGGER.info("aspects logging enabled"); 
     } 
} 
+0

“但现在实际的实现方法没有被调用。”你能更清楚吗? – davidxxx

+0

@davidxxx我刚才编辑了那部分,我希望现在清楚了吗? –

+0

您的错误是由在该bean上使用AOP导致的。尝试从spring配置中删除aop xml并运行代码的初始版本。此外,您正在使用非常古老的春季和春季版本。我建议使用最新发布版本''' org.springframework 弹簧AOP 4.3.8。RELEASE ''' –

回答

1

是的,您的看点有问题。它实际上并没有调用检测代码。试试这个:

public void logAround(ProceedingJoinPoint aJoinPoint) { // <<-- don't forget to change the type to ProceedingJoinPoint 
    LOGGER.info("aspects logging enabled"); 
    aJoinPoint.proceed(); // this will continue to the instrumented code 
} 
+0

感谢ton.it工作,它只花了我一些时间来标记你的答案,因为我正在做一些研究,为什么需要proceedingJoinPoint.proceed(),它是唯一需要的围绕建议,而不是其他建议类型。它也在sring-aop文档中提到,但可能我错过了阅读该部分。 –

相关问题