Spring bean为null

时间:2011-12-18 12:22:23

标签: hibernate spring tapestry

我正在使用jetty 6.1.26作为web服务器的tapestry,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 app context 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>

Index.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;
}

}

1 个答案:

答案 0 :(得分:1)

由于您的配置代码段中缺少它,我认为它完全不存在:

<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实例,你会没事的。