java - @Autowired inside @Entity not working properly -


when log in applications nullpointerexception @ @autowired field hashprovider. strange thing is, happens sometimes! when tried debug mode, field autowired, when run normally, not.

@configurable(preconstruction=true) @entity @table(name="db_user") @inheritance(strategy = inheritancetype.joined) @discriminatorcolumn(name = "user_type", discriminatortype = discriminatortype.string) public abstract class user extends abstractentity {       //...      @autowired(required = true)     private transient hashprovider hashprovider;        //...      public boolean haspassword(string password){         log.info("is null "+(hashprovider==null));   //sometimes null not         return hashprovider.computehash(password + salt).equals(this.password);  //null pointer     } }  @component("hashprovider") public class sha1provider implements hashprovider{     //...     @override     public string computehash(string s) {         //...     } } 

also logging different users throws exception or not. while logging in doctor, field null, adminuser, field not

@entity public class adminuser extends user {      @override     public string tostring() {         return super.tostring() + "adminuser{" + '}';     }      @override     public boolean isadmin() {         return true;     }     }  @entity @discriminatorvalue("doctor") public class doctor extends user {      private string address;      @column(length = 10, unique = true, name = "doctor_bcn", nullable = false)     private long birthnumber;      private integer phone;      @onetomany(mappedby = "doctor")     @orderby("name asc")     private list<patient> patients;      //setters , getters             @override     public string tostring() {         return super.tostring() + "doctor{}";     }      @override     public boolean isadmin() {         return false;     } } 

this applicationcontext.xml file:

<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:mvc="http://www.springframework.org/schema/mvc"        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/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"         >     <!-- use annotation configure spring context -->     <context:annotation-config />     <mvc:annotation-driven />     <!-- search beans under com.wpa package (using annotation @component, @repository, @service) -->     <context:component-scan base-package="com.wpa"/>     <!-- @configurable support -->     <context:spring-configured/>     <!-- property files -->     <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">         <property name="locations">             <list>                 <value>/web-inf/properties/jdbc.properties</value>                 <value>/web-inf/properties/jpa.properties</value>             </list>         </property>     </bean>     <!-- connection pool -->     <bean id="datasource" class="com.jolbox.bonecp.bonecpdatasource" destroy-method="close">         <property name="driverclass" value="${jdbc.driverclassname}"/>         <property name="jdbcurl" value="${jdbc.url}"/>         <property name="username" value="${jdbc.username}"/>         <property name="password" value="${jdbc.password}"/>     </bean>      <!-- defines entitymanagerfactory bean, provides application entitymanager instances = persistence context -->     <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">         <property name="datasource" ref="datasource"/>         <property name="jpavendoradapter">             <bean class="org.springframework.orm.jpa.vendor.eclipselinkjpavendoradapter">                 <property name="databaseplatform" value="${jpa.platform}"/>                 <property name="generateddl" value="true"/>                 <property name="showsql" value="true"/>             </bean>          </property>         <property name="jpapropertymap">             <map>                 <entry key="eclipselink.weaving" value="static"/>                 <entry key="eclipselink.ddl-generation" value="create-or-extend-tables" />             </map>         </property>         <property name="packagestoscan" value="com.wpa"/>         <property name="jtadatasource" ref="datasource"/>     </bean>      <!-- transaction manager declarative transactional demarcation -->     <bean id="txmanager" class="org.springframework.orm.jpa.jpatransactionmanager">         <property name="entitymanagerfactory" ref="entitymanagerfactory"/>         <property name="datasource" ref="datasource"/>     </bean>      <!-- use declared transaction manager manage transaction using @transactional annotation -->     <!-- proxy-target-class=true enables use of concrete classes without interfaces beans @transactional annotation -->     <tx:annotation-driven transaction-manager="txmanager" proxy-target-class="true" />         <bean id="transactiontemplate" class="org.springframework.transaction.support.transactiontemplate">         <property name="transactionmanager">             <ref bean="txmanager"/>         </property>     </bean>    </beans> 

@autowire can used on classes marked spring. example @service, @component ,@repostitory

edit: missed @configurable make spring ioc also.

another thing know if write transient on member after deserialize null.. seems similar case. make sense?

edit: spring beans shouldn't used in jpa entities.. bad practice. best practice achieve create wrapper contain pojo additional functionality. think best thing leave logic in service if small logic..


Comments