日期:2014-05-17  浏览次数:20689 次

spring+jdbc反射问题
Class cls = Class.forName("NewsDao");
Object obj = cls.newInstance();
Class cls = obj.getClass();
Method m = cls.getDeclaredMethod("getAll");
List val = (List)m.invoke(obj);
上面这些是反射的方法

public class NewsDao implements INewsDao{
public SimpleJdbcTemplate jdbcTemplate;
private static final String sql = "select title ,description,pic_path from news";
@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
public List<News> getAll() {
System.out.println("运行到了dao");
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<News>(News.class));
}
public int getInt(){
return 5;
}
}
上面是NewsDao类
可以输出"运行到了dao"
但是然后就开始报错
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.umessage.framework.util.ClassUtils.call(ClassUtils.java:48)
at com.umessage.framework.cache.init.InitCache.initcache(InitCache.java:21)
at com.umessage.framework.listener.ContextListener.contextInitialized(ContextListener.java:33)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:987)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:909)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:495)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1206)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:314)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.NullPointerException
at com.umessage.mobile.index.dao.impl.NewsDao.getAll(NewsDao.java:37)
... 31 more
但是jdbcTemplate一直没有被实例化
请教一下,这是什么原因

上面的代码是在spring初始化的时候运行的

------解决方案-----