当前位置: 首页 > 知识库问答 >
问题:

spring 4.0.0中的JDBC模板java.lang.ClassCastException

西门安民
2023-03-14

我对Spring不熟悉。我正在尝试运行JDBCTempalte示例。我得到了ClassCastException。对我来说没什么意义。

我的上下文文件

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- I have removed my config, I have tried JDBC code it is working with this datasource-->
</bean>

<bean id="jdbcDAO" class="com.sarma.spring.jdbcEx.JDBCDAOImpl">
<property name="dataSource" ref="dataSource"/><!-- This is my jdbc example it is working-->
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>

我的主要课程

try{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
org.springframework.jdbc.core.JdbcTemplate jdbc = (org.springframework.jdbc.core.JdbcTemplate)context.getBean("jdbcTemplate");
String query = "SELECT ACNT_NBR FROM eit.ACNT WHERE ACNT_ID=13057";
int o= jdbc.queryForInt(query);
}catch(Exception e){
    e.printStackTrace();
}

输出

2013-10-08 16:43:34 INFO  ClassPathXmlApplicationContext:513 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c672d0: startup date [Tue Oct 08 16:43:34 EDT 2013]; root of context hierarchy
2013-10-08 16:43:34 INFO  XmlBeanDefinitionReader:316 - Loading XML bean definitions from class path resource [applicationContext.xml]
2013-10-08 16:43:34 INFO  DriverManagerDataSource:133 - Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
java.lang.ClassCastException: $Proxy12 cannot be cast to org.springframework.jdbc.core.JdbcTemplate
    at com.sarma.spring.jdbcEx.main.JdbcExMain.main(JdbcExMain.java:45)

我的 45 行是

org.springframework.jdbc.core.JdbcTemplate jdbc = (org.springframework.jdbc.core.JdbcTemplate)context.getBean("jdbcTemplate");

我犯了什么错误吗??谢谢你的帮助

谢谢,

CVSR 萨尔马

共有1个答案

许华清
2023-03-14

除非需要应用一些特殊的外部行为,否则Spring bean通常不会被代理。比如AOP建议、事务管理、bean作用域等。

你的上下文似乎不完整。如果您已经声明了一些与< code>JdbcTemplate方法匹配的AOP连接点,那么该bean将被代理。您可以指定代理设置,例如,如果Spring应该< code>proxy-target-class而不是接口。如果您的类路径中有CGLIB库,那么通过设置

<aop:config proxy-target-class="true"> ...

JDK 代理的一个小例子

public static void main(String[] args) throws Exception {
    JdbcTemplate template = new JdbcTemplate();
    Object proxy = Proxy.newProxyInstance(template.getClass().getClassLoader(), template.getClass().getInterfaces(), new ProxyJdbcTemplateHandler(template));
    System.out.println(proxy.getClass());
    System.out.println(proxy.getClass().getSuperclass());
    System.out.println(Arrays.toString(proxy.getClass().getInterfaces()));
}

public static class ProxyJdbcTemplateHandler implements InvocationHandler {
    private JdbcTemplate target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        // do something with target
        return null;
    }

    public ProxyJdbcTemplateHandler(JdbcTemplate target) {
        this.target = target;
    }
}

指纹

class $Proxy0
class java.lang.reflect.Proxy
[interface org.springframework.jdbc.core.JdbcOperations]

代理具有< code>JdbcTemplate的超接口的类型,但没有类型本身。它的父类实际上是< code>Proxy。为此,您需要使用CGLIB代理,方法是在类路径上提供CGLIB jars,并在配置中指定< code > proxy-target-class = " true " 。

 类似资料:
  • 我使用Jdbctemplate从数据库中检索单个记录。这是方法。 在我的场景中,完全有可能不会命中我的查询,所以我的问题是如何绕过以下错误消息。 HTTP状态500 -请求处理失败;嵌套异常为org . spring framework . JDBC . badsqlgrammarexception:PreparedStatementCallback;SQL语法错误[select pet_adv,

  • 我想检索Order对象的列表。每个Order对象可能都有一个OrderRows列表。OrderRows保存在单独的表中。如何将下面的查询与Jdbctemplate一起使用?

  • 我正在尝试调用一个存储过程来在SQL表中创建一个项。运行代码时,运行存储过程并创建项,但抛出此错误。表中存储的值是正确的,generate键生成正确的键。 下面是我创建参数映射的create函数: }

  • 我是Java新手。我只做了大约一年的编程。Spring使用模板是什么意思?在Spring,有jdbc模板、jms模板等。。java中的模板类是什么?它们是一种特殊的设计模式还是什么? 提前谢谢你。

  • 7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式。 JdbcTemplate类通过模板设计模式帮助我们消除了冗长的代码,只做需要做的事情(即可变部分),并且帮我们做哪些固定部分,如连接的创建及关闭。 JdbcTemplate类对