2013-12-19 70 views
0

我試圖將spring hibernate和struts 2一起使用。應用程序部署成功。但是當試圖訪問一個spring bean時,我得到了NullPointerException。 下面是代碼UsersBo是一類Struts + spring + hibernate Spring bean空指針異常

@Autowired 
private UsersBo ubo; 

public void setUbo(UsersBo ubo) { 
    this.ubo = ubo; 
} 

public UsersBo getUbo() { 
    return ubo; 
} 

我檢查,找出UDO爲空。以下是配置文件。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

<!-- Hibernate session factory --> 
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton"> 

    <property name="dataSource"> 
     <ref bean="dataSource"/> 
    </property> 

    <property name="annotatedClasses"> 
    <list> 
     <value>com.riteshsangwan.ossoc.entities.Users</value> 
     <value>com.riteshsangwan.ossoc.entities.Files</value> 
    </list> 
    </property> 

    <property name="hibernateProperties"> 
     <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     <prop key="hibernate.cache.use_second_level_cache">true</prop> 
     <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
     <prop key="hibernate.cache.use_query_cache">true</prop> 
     <prop key="cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> 
     </props> 
    </property> 

    </bean> 
</beans> 

的DataSource豆

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/ossoc;create=true" /> 
    <property name="username" value="root" /> 
    <property name="password" value="deflection" /> 
    </bean> 

</beans> 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

    <!-- Database Configuration --> 
    <import resource="DataSource.xml"/> 
    <import resource="HibernateSessionFactory.xml"/> 

    <!-- Beans Declaration --> 
    <import resource="UsersBean.xml"/> 

</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     id="WebApp_ID" version="3.0"> 

     <display-name>ossoc</display-name> 
     <welcome-file-list> 
      <welcome-file>/index.jsp</welcome-file> 
     </welcome-file-list> 
     <!-- Filter Start --> 
     <filter> 
      <filter-name>struts2</filter-name> 
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
     </filter> 


     <!-- Filter End --> 



     <!-- Filter Mapping Start --> 
     <filter-mapping> 
      <filter-name>struts2</filter-name> 
      <url-pattern>/*</url-pattern> 
     </filter-mapping> 


     <!-- Filter Mapping End --> 

     <!-- Listener Start --> 

<!-- Spring Start --> 
     <listener> 
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
     </listener> 
</web-app> 

用戶豆

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

    <bean id="loginAction" class="com.riteshsangwan.ossoc.actions.LoginAction"> 
     <property name="ubo" ref="usersBo" /> 
    </bean> 

    <bean id="usersBo" class="com.riteshsangwan.ossoc.business.UsersBoImpl" > 
     <property name="udao" ref="usersDAOImpl" /> 
    </bean> 

    <bean id="usersDAOImpl" class="com.riteshsangwan.ossoc.business.UsersDAOImpl" > 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

</beans> 

我加了註釋@Repository級以上defintation

+0

您已添加> @Repository以上哪個類?第一個代碼是來自LoginAction的代碼嗎? - 如果是這樣,你不需要設置屬性 - > @Autowire負責。 – HellishHeat

+0

@HellishHeat https://drive.google.com/file/d/0B0wH8cXp0qN4SU1rNnhyYW5Pcmc/edit?usp=sharing 看一下我正在上傳zip文件。我被困在過去的4天。 – user3108790

回答

0

啓用@Autowired註解,你必須在Spring配置文件(用戶豆XML文件)添加<context:annotation-config />。你要添加的context命名空間以及

xmlns:context="http://www.springframework.org/schema/context" 

所以,你的XML將

<?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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

    <context:annotation-config /> 

    <bean id="loginAction" class="com.riteshsangwan.ossoc.actions.LoginAction"> 
     <property name="ubo" ref="usersBo" /> 
    </bean> 

    <bean id="usersBo" class="com.riteshsangwan.ossoc.business.UsersBoImpl" > 
     <property name="udao" ref="usersDAOImpl" /> 
    </bean> 

    <bean id="usersDAOImpl" class="com.riteshsangwan.ossoc.business.UsersDAOImpl" > 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

</beans> 

希望這有助於!我還看到setter和getters方法應遵循Java命名約定。所以,spring可以在依賴注入期間找到合適的setter。請更改/遵循代碼中的Java命名約定。

+0

是的setter和getter遵循命名約定。你會看看zip文件嗎?我陷入了這個問題4天。 https://drive.google.com/file/d/0B0wH8cXp0qN4SU1rNnhyYW5Pcmc/edit?usp=sharing – user3108790