2014-04-10 49 views
0

我正在寫一個使用Hibernate訪問數據庫的簡單項目。爲什麼我無法使用@Inject注入SessionFactory,而@Autowired可以?

我有一類叫做:ContactServiceImpl.java

package hi;  

import javax.inject.Inject; 
...  

@Service 
@Transactional 
public class ContactServiceImpl implements ContactService { 
    @Inject private SessionFactory sessionFactory; 
... 

我已經宣佈在配置文件中的sessionFactory

... 
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
    p:dataSource-ref="dataSource" 
    p:packagesToScan="hi" 
    p:hibernateProperties-ref="hibernateProperties" /> 

<context:component-scan base-package="hi" /> 
.... 

但是當程序運行時,發生以下情況例外:

SEVERE: Servlet.service() for servlet [jsp] in context with path [/HelloHibernate3] threw exception [java.lang.NullPointerException] with root cause 
java.lang.NullPointerException 
    at hi.ContactServiceImpl.getSession(ContactServiceImpl.java:60) 
    at hi.ContactServiceImpl.getContacts(ContactServiceImpl.java:29) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
... 

其中ContactServiceImpl 60行是:

return sessionFactory.getCurrentSession(); 

表明該sessionFactory未正確初始化。

當我改變@Inject@Autowired,一切工作正常:

... 
import org.springframework.beans.factory.annotation.Autowired; 

@Service 
@Transactional 
public class ContactServiceImpl implements ContactService { 
    @Autowired private SessionFactory sessionFactory; 
... 

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?指出@Autowired@Inject工作彈簧下面是相同的。

編輯:

pom.xml文件是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>HelloHibernate3</groupId> 
    <artifactId>HelloHibernate3</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>HelloHibernate3</name> 
    <description/> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <dependencies> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>bean-validator</artifactId> 
     <version>3.0-JBoss-4.0.2</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.enterprise.deploy</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.jms</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.management.j2ee</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.eclipse.persistence</groupId> 
     <artifactId>javax.persistence</artifactId> 
     <version>2.0.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.resource</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.security.auth.message</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.security.jacc</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.servlet</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.servlet.jsp</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish</groupId> 
     <artifactId>javax.servlet.jsp.jstl</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.xml.bind</groupId> 
     <artifactId>jaxb-api-osgi</artifactId> 
     <version>2.2.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.ws.rs</groupId> 
     <artifactId>jsr311-api</artifactId> 
     <version>1.1.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish.web</groupId> 
     <artifactId>jstl-impl</artifactId> 
     <version>1.2</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.mail</groupId> 
     <artifactId>mail</artifactId> 
     <version>1.4.3</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.xml</groupId> 
     <artifactId>webservices-api-osgi</artifactId> 
     <version>2.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.jboss.weld</groupId> 
     <artifactId>weld-osgi-bundle</artifactId> 
     <version>1.0.1-SP3</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish.web</groupId> 
     <artifactId>javax.servlet.jsp.jstl</artifactId> 
     <version>1.2.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-asm</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aspects</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context-support</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-expression</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-instrument</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-instrument-tomcat</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.6.9</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-pool</groupId> 
     <artifactId>commons-pool</artifactId> 
     <version>1.5.3</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-collections</groupId> 
     <artifactId>commons-collections</artifactId> 
     <version>3.2</version> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.16</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>c3p0</groupId> 
     <artifactId>c3p0</artifactId> 
     <version>0.9.1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jms</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-oxm</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc-portlet</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-struts</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-httpclient</groupId> 
     <artifactId>commons-httpclient</artifactId> 
     <version>3.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>taglibs</groupId> 
     <artifactId>standard</artifactId> 
     <version>1.1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <version>5.10</version> 
     <classifier>jdk15</classifier> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-acl</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-aspects</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-cas</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-core</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-crypto</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-ldap</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-openid</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-remoting</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-taglibs</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.webflow</groupId> 
     <artifactId>spring-faces</artifactId> 
     <version>2.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.webflow</groupId> 
     <artifactId>spring-webflow</artifactId> 
     <version>2.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.webflow</groupId> 
     <artifactId>spring-binding</artifactId> 
     <version>2.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.webflow</groupId> 
     <artifactId>spring-js</artifactId> 
     <version>2.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.webflow</groupId> 
     <artifactId>spring-js-resources</artifactId> 
     <version>2.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>ognl</groupId> 
     <artifactId>ognl</artifactId> 
     <version>2.6.9</version> 
    </dependency> 
    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javassist</groupId> 
     <artifactId>javassist</artifactId> 
     <version>3.9.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>antlr</groupId> 
     <artifactId>antlr</artifactId> 
     <version>2.7.6</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>3.3.2.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.transaction</groupId> 
     <artifactId>jta</artifactId> 
     <version>1.1</version> 
    </dependency> 
    <dependency> 
     <groupId>net.sf.ehcache</groupId> 
     <artifactId>ehcache</artifactId> 
     <version>1.2.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.5.8</version> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>1.5.8</version> 
    </dependency> 
    <dependency> 
     <groupId>dom4j</groupId> 
     <artifactId>dom4j</artifactId> 
     <version>1.6.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-annotations</artifactId> 
     <version>3.4.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-commons-annotations</artifactId> 
     <version>3.1.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>3.4.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <version>3.1.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.persistence</groupId> 
     <artifactId>persistence-api</artifactId> 
     <version>1.0</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <resources> 
     <resource> 
     <directory>src</directory> 
     <excludes> 
      <exclude>**/*.java</exclude> 
     </excludes> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>2.3.2</version> 
     <configuration> 
      <source>1.7</source> 
      <target>1.7</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.2</version> 
     <configuration> 
      <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory> 
      <version>3.0</version> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

你可以附上你的pom.xml嗎 –

+0

這是MyEclipse生成的一個很大的。 –

回答

1

從我可以告訴,這依賴

<dependency> 
    <groupId>org.jboss.weld</groupId> 
    <artifactId>weld-osgi-bundle</artifactId> 
    <version>1.0.1-SP3</version> 
    <scope>provided</scope> 
</dependency> 

是提供包括@Inject在編譯時庫中。但是,由於它提供了<scope>,因此您的應用程序取決於提供它的容器。如果沒有,Spring將不會在您的字段上查找@Inject註釋。

你有兩個選擇:

  1. 更改依賴的<scope>或直接添加javax.inject依賴。
  2. 更改您的容器,以便它確實提供包含在該依賴項中的那些庫。
0

如果您在鏈接here看到的,各國在pom.xml中javax.inject依賴性爲:

<dependency> 
     <groupId>javax.inject</groupId> 
     <artifactId>javax.inject</artifactId> 
     <version>1</version> 
    </dependency> 

包括這一點,並檢查。

相關問題