简体   繁体   English

@Autowired 在 Spring Restful Web 服务中不起作用

[英]@Autowired not working in Spring restful web service

I am developing application using Struts2 + Spring4 + Hibernate4 with RESTful web service.我正在使用 Struts2 + Spring4 + Hibernate4 和 RESTful Web 服务开发应用程序。 I have configured Hibernate in Spring bean file.我已经在 Spring bean 文件中配置了 Hibernate。 For RESTful web service I have excluded URL in struts.xml using对于 RESTful Web 服务,我使用了struts.xml URL

<constant name="struts.action.excludePattern" value="/service/.*"/> 

If I access sessionFactory object in any action class it works fine.如果我在任何操作类中访问sessionFactory对象,它就可以正常工作。 But if I access it in my web service it gives NullPointerException .但是如果我在我的网络服务中访问它,它会给出NullPointerException

After searching on Google I found that if we bypass URL from Struts it does not allow to initialize object using @Autowired annotation.在 Google 上搜索后,我发现如果我们绕过 Struts 中的 URL,则不允许使用@Autowired注释初始化对象。

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 映射:

<bean id="accountService" class="com.xxx.yyy.services.impl.AccountServiceImpl">
    <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>

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.如果您从 Spring 获取对象,则 Autowired 可以工作,并且在从上下文获取对象之前,应将对象配置为 Spring bean。 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.将某个类配置为 Spring bean 有时只需要放置一些@Component注解并确保扫描该类以查找注解。 In you case a @Service annotation is more appropriate在你的情况下, @Service注释更合适

@Service
@Path("service/account-management")
public class AccountServiceImpl implements AccountService { 

In the applicationContext.xml you should haveapplicationContext.xml你应该有

<context:component-scan base-package="com.xxx.yyy.services"/>

Worth noting here that Spring 3.1 introduces LocalSessionFactoryBuilder, which is expressly designed for use within @Bean methods.值得注意的是,Spring 3.1 引入了 LocalSessionFactoryBuilder,它是专门为在 @Bean 方法中使用而设计的。

http://static.springsource.org/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html http://static.springsource.org/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html

In your XML you also can use the hibernate config also :在您的 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>

or can use the java based bean config : @Bean public SessionFactory sessionFactory(){ AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();或者可以使用基于java的bean配置:@Bean public SessionFactory sessionFactory(){ AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml")); sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml")); sessionFactoryBean.afterPropertiesSet(); sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject();返回 sessionFactoryBean.getObject(); } }

and then you can use it inside your Spring bean:然后你可以在你的 Spring bean 中使用它:

    @Autowired
        SessionFactory sessionFactory;

and then inside of your method:然后在你的方法里面:

Session session = sessionFactory.getCurrentSession();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM