用代理方式执行时jdbcTemplate为NULL,高手帮看一下怎么回事呀????
Exception in thread "main" 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.okgj.core.MessageExecutor.invoke(MessageExecutor.java:46)
at com.okgj.core.MessageExecutor.main(MessageExecutor.java:64)
Caused by: java.lang.NullPointerException
at com.okgj.service.dao.impl.UserDaoImpl.getUserInfo(UserDaoImpl.java:42)
at com.okgj.service.impl.UserServiceImpl.getUserInfo(UserServiceImpl.java:43)
at com.okgj.service.impl.UserServiceImplEnhancerByCGLIB
... 6 more
package com.okgj.service.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.okgj.service.dao.UserDao;
import com.okgj.service.entity.userInfo;
@Repository
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
@Override
public List getUserInfo(String condition) {
String sql = "select username,userpwd from tb_user";
List userList = jdbcTemplate.queryForList(sql);
return userList;
}
}
debug下面类时不会出错,
package com.okgj.service.impl;
import java.lang.reflect.Method;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.okgj.service.UserService;
import com.okgj.service.dao.UserDao;
import com.okgj.service.dao.impl.UserDaoImpl;
import org.springframework.transaction.annotation.Propagation;
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao = new UserDaoImpl();
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List getUserInfo(String condition) {
return userDao.getUserInfo(condition);
}
public static void main(String[] args) throws Throwable {
ApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:spring-service.xml");
UserService userService = (UserService)
context.getBean("userService");
List list = userService.getUserInfo("");
System.out.println(list.toString());
}
}
而通过代理的方式执行却报上面的错,为什么 呀?急呀!!!!!!
package com.okgj.service.impl;
import java.lang.reflect.Method;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.okgj.service.UserService;
import com.okgj.service.dao.UserDao;
import com.okgj.service.dao.impl.UserDaoImpl;
import org.springframework.transaction.annotation.Propagation;
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao = new UserDaoImpl();
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List getUserInfo(String condition) {
return userDao.getUserInfo(condition);
}
public static void main(String[] args) throws Throwable {
ApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:spring-service.xml");
Class<?> c = (Class<?>) context.getBean("userService").getClass();
Object obj = c.newInstance();
Method m = c.getMethod("getUserInfo",
new Class[] { java.lang.String.class });
Object result = m.invoke(obj, new Object[] { "" });
}
}
spring的配置文件如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<import resource="spring-db-connectionpool.xml" />
<bean id="userService" class="com.okgj.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="orderService" class="com.okgj.service.impl.OrderServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.okgj.service.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
<!--数据源配置,使用应用内的C3P0数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost/projectmanagesystem" />
<property name="user" value="root" />
<property name="password" value="" />
<property name="numHelperThreads" value="5" />
<property name="maxPoolSize" value="20" />
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="5" />
</bean>
<!-- 添加AnnotationSessionFactoryBean,提供注解注入支持 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.okgj.service.*</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.format_sql">false</prop>
<!-- <prop key="hibernate.connection.autocommit">true </prop>-->
<prop key="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>