0

我是 Spring 新手,我想注入 sessionFactory 并且它根本不起作用。我正在启动一个 Web 服务器 Jetty,然后加载上下文。在我启动 GWT Web 应用程序后,我在服务器端进行调用并尝试获取一些信息,但我得到一个空指针。根本没有错误,因此很难知道问题出在哪里。我知道它应该可以工作,因为我看到它在我之前工作过的一个项目上工作。任何帮助将不胜感激。(对不起我可能英语不好)

这是 context.xml :

 <?xml version="1.0" encoding="UTF-8" ?>

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:security="http://www.springframework.org/schema/security"
   xsi:schemaLocation="http://www.springframework.org/schema/beans       
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema
    /context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema
    /tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema
    /aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/security http://www.springframework.org/schema
    /security/spring-security-3.0.xsd">


   <!--  Standard spring initialization -->
   <context:component-scan base-package="com.test">

   </context:component-scan>

   <tx:annotation-driven transaction-manager="txManager"/>

   <!-- Connection to the database-->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-
    method="close">

   <property name="driverClassName" value="com.mysql.jdbc.Driver" />
   <property name="url" value="jdbc:mysql://localhost:3306/test" />
   <property name="username" value="root" />
   <property name="password" value="123456789" />
   </bean>

   <!-- Hibernate session factory-->
   <bean id="jpaSessionFactory"   
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.domain"/>
  <property name="namingStrategy" >
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
  </property>
  <property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
  </property>
   </bean>

   <!-- Hibernate session factory -->   
   <bean id="txManager"    
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="jpaSessionFactory" />
   </bean>

 </beans>

这是主要的:

    public static void main(String[] args) {

        ContextHandlerCollection contexts = new ContextHandlerCollection();

        contexts.setHandlers(new Handler[] 
        { new AppContextBuilder().buildWebAppContext()});

        final JettyServer jettyServer = new JettyServer();
        jettyServer.setHandler(contexts);
        Runnable runner = new Runnable() {
            @Override
            public void run() {
                new ServerRunner(jettyServer);
            }
        };
        EventQueue.invokeLater(runner);

        new ClassPathXmlApplicationContext("context.xml");
    }

这是我想要注入的类 Test :

@Component("test")
public class TEST{

    @Resource(name="jpaSessionFactory")
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public  List<Personne> getPersonnes() {

        Session s = sessionFactory.getCurrentSession();

        Query query = s.createQuery("from Person");

        return query.list();
    }
}

这是应用程序的服务器端(未完全显示)

/*** The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {

@Resource
private TEST t;

public String greetServer(String input) throws IllegalArgumentException {
    // Verify that the input is valid. 
    if (!FieldVerifier.isValidName(input)) {
        // If the input is not valid, throw an IllegalArgumentException 
                        back to
        // the client.
        throw new IllegalArgumentException(
                "Name must be at least 4 characters long");
    }

    t.getPersons();  // NULL pointer here

…………………………………………………………………………………………………………

MySQL 表是按应有的方式创建的,因此所有扫描似乎都有效。

谢谢

鲍勃

4

1 回答 1

1

这是因为 GreetingServiceImpl 类不是由 spring 创建的——它是由容器直接初始化的 servlet。请按照本文中的说明解决此问题。我已经从文章中复制了相关代码。

@Override
public void init() throws ServletException {
    super.init();

    final WebApplicationContext ctx =
        WebApplicationContextUtils.getWebApplicationContext(getServletContext());

    if (ctx == null) {
        throw new IllegalStateException("No Spring web application context found");
    }

    ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this,
        AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
}
于 2012-05-10T05:31:36.223 回答