@Autowired 在 Spring restful 网络服务中不工作
@Autowired not working in Spring restful web service
我正在使用 Struts2 + Spring4 + Hibernate4 和 RESTful Web 服务开发应用程序。我在 Spring bean 文件中配置了 Hibernate。对于 RESTful 网络服务,我使用
在 struts.xml
中排除了 URL
<constant name="struts.action.excludePattern" value="/service/.*"/>
如果我在任何操作中访问 sessionFactory
对象 class 它工作正常。但是如果我在我的网络服务中访问它,它会给出 NullPointerException
.
在 Google 上搜索后我发现如果我们从 Struts 绕过 URL 它不允许使用 @Autowired
注释初始化对象。
这个东西怎么解决?我在 Google 上搜索过,但没有找到任何有用的信息。
这是我的服务:
@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();
}
}
这是我的 bean 映射:
<bean id="accountService" class="com.xxx.yyy.services.impl.AccountServiceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
如果您从 Spring 获取对象,Autowired 会工作,并且在从上下文获取对象之前,应将其配置为 Spring bean。要将某些 class 配置为 Spring bean,有时只需要放置一些 @Component
注释并确保扫描 class 以查找注释。在你的情况下,@Service
注释更合适
@Service
@Path("service/account-management")
public class AccountServiceImpl implements AccountService {
在applicationContext.xml
你应该有
<context:component-scan base-package="com.xxx.yyy.services"/>
这里值得注意的是 Spring 3.1 引入了 LocalSessionFactoryBuilder,它专门设计用于 @Bean 方法。
在您的 XML 中,您也可以使用休眠配置 :
<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>
或者可以使用基于 java 的 bean 配置:
@豆
public SessionFactory sessionFactory(){
AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
sessionFactoryBean.setConfigLocation(新类路径资源("hibernate.cfg.xml"));
sessionFactoryBean.afterPropertiesSet();
return sessionFactoryBean.getObject();
}
然后您可以在 Spring bean 中使用它:
@Autowired
SessionFactory sessionFactory;
然后在您的方法内部:
Session session = sessionFactory.getCurrentSession();
我正在使用 Struts2 + Spring4 + Hibernate4 和 RESTful Web 服务开发应用程序。我在 Spring bean 文件中配置了 Hibernate。对于 RESTful 网络服务,我使用
在struts.xml
中排除了 URL
<constant name="struts.action.excludePattern" value="/service/.*"/>
如果我在任何操作中访问 sessionFactory
对象 class 它工作正常。但是如果我在我的网络服务中访问它,它会给出 NullPointerException
.
在 Google 上搜索后我发现如果我们从 Struts 绕过 URL 它不允许使用 @Autowired
注释初始化对象。
这个东西怎么解决?我在 Google 上搜索过,但没有找到任何有用的信息。
这是我的服务:
@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();
}
}
这是我的 bean 映射:
<bean id="accountService" class="com.xxx.yyy.services.impl.AccountServiceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
如果您从 Spring 获取对象,Autowired 会工作,并且在从上下文获取对象之前,应将其配置为 Spring bean。要将某些 class 配置为 Spring bean,有时只需要放置一些 @Component
注释并确保扫描 class 以查找注释。在你的情况下,@Service
注释更合适
@Service
@Path("service/account-management")
public class AccountServiceImpl implements AccountService {
在applicationContext.xml
你应该有
<context:component-scan base-package="com.xxx.yyy.services"/>
这里值得注意的是 Spring 3.1 引入了 LocalSessionFactoryBuilder,它专门设计用于 @Bean 方法。
在您的 XML 中,您也可以使用休眠配置 :
<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>
或者可以使用基于 java 的 bean 配置: @豆 public SessionFactory sessionFactory(){ AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setConfigLocation(新类路径资源("hibernate.cfg.xml")); sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject(); }
然后您可以在 Spring bean 中使用它:
@Autowired
SessionFactory sessionFactory;
然后在您的方法内部:
Session session = sessionFactory.getCurrentSession();