【问题标题】:Spring bean is nullSpring bean 为空
【发布时间】:2011-12-18 12:22:23
【问题描述】:

我正在使用 jetty 6.1.26 作为 web-server 开发挂毯、spring、hibernate web-app,我无法摆脱 userDao bean 的 nullpointexception(在 application_context_dao.xml 中定义: :

public class UserManagerImpl implements UserManager {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public boolean checkLogin (String login, String password) {

        return userDao.checkLogin(login, password);

    }

application_context_dao.xml:

<beans>

<bean id="userDao" class="org.prikic.projektni.domain.dao.hibernate3.UserDaoImpl">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

<!-- sessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionProxy" abstract="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref bean="transactionManager" />
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
        </props>
    </property>
</bean>

web.xml:

<web-app>
<display-name>projektni Tapestry 5 Application</display-name>

<context-param>
    <!-- The only significant configuration for Tapestry 5, this informs Tapestry 
        of where to look for pages, components and mixins. -->
    <param-name>tapestry.app-package</param-name>
    <param-value>org.prikic.projektni</param-value>
</context-param>
<!-- Specify some additional Modules for two different execution modes: 
    development and qa. Remember that the default execution mode is production -->
<context-param>
    <param-name>tapestry.development-modules</param-name>
    <param-value>
        org.prikic.projektni.services.DevelopmentModule
    </param-value>
</context-param>
<context-param>
    <param-name>tapestry.qa-modules</param-name>
    <param-value>
        org.prikic.projektni.services.QaModule
    </param-value>
</context-param>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/application_context_dao.xml
        /WEB-INF/application_context.xml
         <!-- classpath:application_context*.xml --> 
    </param-value>
</context-param>

<filter>
    <filter-name>app</filter-name>
    <!-- Special filter that adds in a T5 IoC module derived from the Spring 
        WebApplicationContext. -->
    <filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>app</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

任何帮助将不胜感激......

我很抱歉没有放其他 spring 应用程序上下文 xml 文件,这里是:

application_context.xml:

<beans>
    <bean id="userManagerTarget" class="org.prikic.projektni.services.impl.UserManagerImpl">
            <property name="userDao">
                    <ref bean="userDao" />
            </property>
    </bean>
    <bean id="userManager" parent="transactionProxy">               
            <property name="target">
                    <ref bean="userManagerTarget"/>
            </property>
            <property name="transactionAttributeSource">
                    <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
            </property>
    </bean>
</beans>

索引.java:

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.prikic.projektni.services.UserManager;

public class Index {

private static final String BAD_CREDENTIALS = "Bad login and/or password. Please retry.";


//private boolean error = false;
@Persist
private boolean error;

@SessionState(create=false)
private String login;

@Inject
@Service("userManager")
private UserManager userManager;

private String password;

public String getLogin() {
    return login;
}

@Validate("required")
public void setLogin(String login) {
    this.login = login;
}

public String getPassword() {
    return password;
}

public String getErrorMessage() {
    String ret = null;
    if (error) {
        ret = BAD_CREDENTIALS;
    }
    return ret;
}

@Validate("required")
public void setPassword(String password) {
    this.password = password;
}

String onSuccess() {

    String ret = "Index";
    error = true;

    boolean s = userManager.checkLogin(login, password);

    if (s) {
        error = false;
        ret = "Home";
    }
    return ret;
}

}

【问题讨论】:

  • UserManagerImpl bean 定义在哪里?如何获取 UserManagerImpl 的实例?

标签: hibernate spring tapestry


【解决方案1】:

由于您的配置 sn-p 中缺少它,我认为它完全不存在:

<bean id="userManagerImpl" class="org.prikic.projektni.domain.dao.hibernate3.UserManagerImpl">
    <property name="userDao" ref="userDao"/>
</bean>

将此 bean 添加到 Spring 配置后,它应该可以工作。当然现在你不能只打电话:

new UserManagerImpl();

因为 Spring 对这个类一无所知,因此无法执行依赖注入。我不知道 Tapestry,但看起来您已经在使用 TapestrySpringFilter,这似乎以某种方式将 Tapestry 与 Spring 结合起来。 JavaDoc 说它是:

将其中的每个 bean 公开为 Tapestry IoC 服务

如果你使用这个所谓的 IoC 服务并以某种方式获得UserManager 实例,你会没事的。

【讨论】:

  • @Borislav:请编辑您的问题,而不是在此处发布。如果你在 XML 中声明userManagerImpl,你如何获得它?这是至关重要的。
猜你喜欢
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2020-11-04
  • 2012-08-27
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 2016-05-09
相关资源
最近更新 更多