Am使用Java 1.7和Spring Framework 4.3.4.RELEASE。
/src/main/resources/database-config.xml:
<bean id="dataSourceProducts" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.urlProducts}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="initialSize" value="${db.initialSize}"/>
<property name="maxTotal" value="${db.maxTotal}"/>
<property name="maxIdle" value="0"/>
<property name="minIdle" value="0"/>
<property name="maxWaitMillis" value="10000"/>
<property name="validationQuery" value="SELECT NOW();"/>
<property name="timeBetweenEvictionRunsMillis" value="10000"/>
<property name="removeAbandonedOnMaintenance" value="true"/>
<property name="maxConnLifetimeMillis" value="10000"/>
<property name="minEvictableIdleTimeMillis" value="1000"/>
/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<import resource="classpath:database-config.xml" />
<context:component-scan base-package="com.myapp.dao,com.myapp.manager" />
<mvc:annotation-driven />
/src/main/java/com/myapp/dao/ProductDao.java:
package com.myapp.dao;
@Repository
public class ProductDao {
public JdbcTemplate jdbcTemplateProduct = null;
@Autowired
@Qualifier("dataSourceProducts")
DataSource dataSourceProducts;
ProductDao() { }
/**
* constructor to create object and to use for test cases class.
* @param jdbcTemplateObject
*/
@VisibleForTesting
public ProductDao(JdbcTemplate jdbcTemplateObject) {
this.jdbcTemplateProduct = jdbcTemplateObject;
}
@PostConstruct
public void init() {
jdbcTemplateProduct = new JdbcTemplate(dataSourceProducts);
}
public boolean runValidationQuery() {
boolean retValue = false;
SqlRowSet rst = null;
rst = jdbcTemplateProduct.queryForRowSet("select now()");
if (rst != null) {
retValue = true;
}
return retValue;
}
}
/ src / main / java / com / myapp / manager / ProductManager:
package com.myapp.manager;
@Controller
public class ProductManager {
@Autowired
ProductDao productDao;
boolean checkValue = false;
public void check() throws IOException {
checkValue = productDao.runValidationQuery();
}
public static void main(String args []) {
ProductManager productManager = new ProductManager();
productManager.check();
}
}
当main()方法进入时,这会不断抛出NullPointerException
productManager.check();
甚至无法进入productManager.check();
同一个NPE马上出现:
Exception in thread "main" java.lang.NullPointerException
at com.myapp.manager.ProductManager.check(ProductManager.java:12)
at com.myapp.manager.ProductManager.main(ProductManager.java:17)
遵循此项目中其他DAO的这种模式,但不了解当前的问题是什么?
是否怀疑其配置问题或缺少注释问题?