gpt4 book ai didi

java - Spring 4.0.0 中的 Spring JDBC 模板 java.lang.ClassCastException

转载 作者:行者123 更新时间:2023-12-01 23:31:08 25 4
gpt4 key购买 nike

我是 Springs 的新手。我正在尝试运行 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 萨尔玛

最佳答案

除非需要应用某些特殊的外部行为,否则 Spring bean 通常不会被代理。例如,AOP advice , transaction management , bean scope

您的上下文似乎不完整。如果您声明了某个与 JdbcTemplate 方法匹配的 AOP 连接点,那么该 Bean 将被代理。您可以指定代理设置,for example if Spring should proxy-target-class instead of the interface 。如果您的类路径上有 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]

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

关于java - Spring 4.0.0 中的 Spring JDBC 模板 java.lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19258209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com