Java 使用ApplicationContext初始化bean时类强制转换异常
我需要您的帮助来解决有关Springbean初始化的问题。详情如下: dao接口Java 使用ApplicationContext初始化bean时类强制转换异常,java,spring,maven,aop,applicationcontext,Java,Spring,Maven,Aop,Applicationcontext,我需要您的帮助来解决有关Springbean初始化的问题。详情如下: dao接口 package com.dao; import com.entity.Employee; public interface IEmployeeDao { Employee create(final Employee aEmployee); Employee fetchEmployeeById(final Integer aEmployeeId); } dao im
package com.dao;
import com.entity.Employee;
public interface IEmployeeDao {
Employee create(final Employee aEmployee);
Employee fetchEmployeeById(final Integer aEmployeeId);
}
dao impl类
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
}
}
我试图获取EmployeeDaoBean的代码的最后一行抛出了以下异常:
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");
bean毫无例外地被注入,但是现在实际的实现方法没有被调用,即现在我调用EmployeedDao的create方法时,如下所示
employeeDao.create(new Employee());
未调用create methods实现,即以下方法:
@Override
public Employee create(Employee aEmployee) {
System.out.println("com.dao.impl.EmployeeDao.create(Employee) callled")
return this.em.merge(aEmployee);
}
我通过放置一条sysout消息确认了这一点
请告诉我我做错了什么,或者可以做些什么来解决这个问题。我还在下面发布了bean配置文件和maven依赖项
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>
是的,你的外貌有问题。它实际上并没有调用插入指令的代码。试试这个:
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
}
但是现在实际的实现方法没有被调用。你能更清楚一点吗?@davidxxx我刚刚编辑了那个部分,我希望它现在清楚了。你的错误是由于在那个bean上使用AOP造成的。尝试从spring配置中删除aop xml并运行初始版本的代码。此外,您正在使用非常旧的spring上下文和spring aop版本。我建议使用最新版本org.springframework-spring aop 4.3.8.release@IvanPronin是的,我知道问题与aop的使用有关,我尝试过不使用它,它可以像你说的那样工作,但是我必须使用aop,所以你能建议实际问题是什么以及如何处理吗?@IvanPronin关于版本,这真的很重要吗?我的意思是,我知道我应该使用最新版本,但你认为问题是因为旧版本造成的吗?非常感谢。它起了作用,我只花了一些时间将你的答案标记为已接受的答案,因为我正在研究为什么需要处理JoinPoint.proceed,很高兴了解到它只需要周围的建议,而不需要其他类型的建议。sring aop文档中也提到了这一点,但我可能没有读到这一部分。
<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>
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");
}
}
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
}