Reputation: 2004
I am developing application using Struts2 + Spring4 + Hibernate4 with RESTful web service. I have configured Hibernate in Spring bean file. For RESTful web service I have excluded URL in struts.xml
using
<constant name="struts.action.excludePattern" value="/service/.*"/>
If I access sessionFactory
object in any action class it works fine. But if I access it in my web service it gives NullPointerException
.
After searching on Google I found that if we bypass URL from Struts it does not allow to initialize object using @Autowired
annotation.
How to sort out this thing? I have searched on Google but nothing useful found.
This is my service:
@Path("service/account-management")
public class AccountServiceImpl implements AccountService {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@POST
@PermitAll
@Path("/accounts")
public Response getAllAccounts() {
System.out.println(sessionFactory);
Session session = this.sessionFactory.getCurrentSession();
List<VbarAccount> personList = session.createQuery("from TEST").list();
System.out.println(personList);
session.close();
return Response.status(200).build();
}
}
This is my bean mapping:
<bean id="accountService" class="com.xxx.yyy.services.impl.AccountServiceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
Upvotes: 1
Views: 1283
Reputation: 93
Worth noting here that Spring 3.1 introduces LocalSessionFactoryBuilder, which is expressly designed for use within @Bean methods.
In your XML you also can use the hibernate config also :
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="yourGivenName">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
or can use the java based bean config : @Bean public SessionFactory sessionFactory(){ AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml")); sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject(); }
and then you can use it inside your Spring bean:
@Autowired
SessionFactory sessionFactory;
and then inside of your method:
Session session = sessionFactory.getCurrentSession();
Upvotes: 0
Reputation: 1
Autowired works if you get the object from Spring, and the object should be configured as a Spring bean before you get it from the context. To configure some class as a Spring bean sometimes only needed to put some @Component
annotation and ensure that the class is scanned for annotations. In you case a @Service
annotation is more appropriate
@Service
@Path("service/account-management")
public class AccountServiceImpl implements AccountService {
In the applicationContext.xml
you should have
<context:component-scan base-package="com.xxx.yyy.services"/>
Upvotes: 1