本人新建了一个java project工程,使用spring hibernate等技术。
spring applicationContext.xml配置文件如下:
<!-- autodetect在byType和constructor之间自动的选择注入方式 --> <!-- 定义实体管理器工厂--> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!-- 指定META-INF下persistence.xml所配置的--> <property name="persistenceUnitName" value="VMS" /> <!-- <property name="persistenceXmlLocation" value="classpath:maps-persistence.xml" /> --> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 启用 annotation事务注解--> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置Spring Data JPA扫描目录扫描全部的数据操作类--> <jpa:repositories base-package="com.cnpc.vms.dao" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
数据库配置文件persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <!-- transaction-type 可选值有: JTA、RESOURCE_LOCAL ; 在Java EE 环境下默认值为JTA, 在Java SE 环境下默认值为RESOURCE_LOCAL; 假如值为JTA的话, 则必须要指定<jta-data-source>的值 --> <persistence-unit name="VMS" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/> <property name="hibernate.connection.username" value="c5web"/> <property name="hibernate.connection.password" value="c5web"/> <property name="hibernate.show_sql" value="true" /> <!-- 服务模式 <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/test"/> --> <!-- 内钳与TCP服务模式混用 <property name="hibernate.connection.url" value="jdbc:h2:file:D:/data/smartcodeDB;AUTO_SERVER=TRUE"/> --> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@10.185.161.74:1521:c5testdb"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <!-- 最小连接数 --> <property name="c3p0.min_size" value="5"/> <!-- 最大连接数 --> <property name="c3p0.max_size" value="30"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="c3p0.maxIdleTime" value="60"/> <!-- 获得连接的超时时间,假如超过这个时间,会抛出异常,单位毫秒 --> <property name="c3p0.timeout" value="1800"/> <!-- 最大的PreparedStatement的数量 --> <property name="c3p0.max_statements" value="50"/> <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒--> <property name="c3p0.idle_test_period" value="120"/> <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 --> <property name="c3p0.acquire_increment" value="1"/> <!-- 能否每次都验证连接能否可用 --> <property name="c3p0.validate" value="false"/> </properties> </persistence-unit> </persistence>
DAO文件:AlcorTCitysDAO
/**********************************************************
* Dao类请继承BaseRepository接口
* 类名规范:{实体名}DAO
* 扩展的数据操作方法请按照spring data jpa规范在此接口中完成
*********************************************************/
public interface AlcorTCitysDAO extends BaseRepository<AlcorTCitys, Long>{ @Transactional(readOnly=true) public List<AlcorTCitys> findByCityName(String name); } service实现类:AlcorTCitysServiceImpl @Service("Service") public class AlcorTCitysServiceImpl implements IAlcorTCitysService { @Autowired public AlcorTCitysDAO alcorTCitysDAO; public List<AlcorTCitys> findByName(String name) { return alcorTCitysDAO.findByCityName(name); }
实体类:AlcorTCitys
@Entity @Table(name = "alcor_t_citys") public class AlcorTCitys implements java.io.Serializable{ private static final long serialVersionUID = 1731325041814156320L; @Id @GeneratedValue(strategy = GenerationType.TABLE) private Long id; @Column(name = "cityCode") private String cityCode; @Column(name = "cityName") private String cityName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getCityCode() { return cityCode; } public void setCityCode(String cityCode) { this.cityCode = cityCode; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } }
action类:AlcorTCitysAction
@Controller public class AlcorTCitysAction { @Autowired @Qualifier("Service") private IAlcorTCitysService service; public List<AlcorTCitys> getList(String name){ List<AlcorTCitys> ac = service.findByName(name); return ac; } public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath:applicationContext.xml" }); context.start(); //System.in.read(); AlcorTCitysAction aca = new AlcorTCitysAction(); List<AlcorTCitys> ac = aca.getList("北京"); if(ac.size() > 0){ System.out.println(ac.get(0).getCityName()); System.out.println(ac.get(0).getCityCode()); System.out.println(ac.get(0).getId()); } } }
运行main方法:IAlcorTCitysService service 这个是null,不知道怎么回事?spring没有注入成功?
java.lang.NullPointerException
at com.cnpc.vms.main.AlcorTCitysAction.getList(AlcorTCitysAction.java:31)
at com.cnpc.vms.main.AlcorTCitysAction.main(AlcorTCitysAction.java:40)
解决方案:80分
看你Spring配置文件,没有自动装配Bean的配置,例如:<context:component-scan base-package=”com.cnpc.vms”/>