0

アノテーションを使用してJSFManagedBeanのSpringContainerで作成されたBeanを使用しようとしていますが、そのBeanを使用すると@ManagedPropertynullポインターが取得されます。サーバーを起動すると、Beanがここで作成されていることがわかります。

Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9d532ae: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,userBean,userService];

HomepageBean.java

package come.test.backingbean

 @ManagedBean
    @sessionScoped

        public Class HomepageBean{

        @ManagedProperty(value="#{userBean}")
        private UserBean userBean;// getters and setters


       public String doLogin() {
            String url = "login.xhtml";
            LoginBean manager = new LoginBean();  // This bean has a condition which check for Username and password entered by user.
            if (manager.auth(username, password)) {
                isLoggedIn = true;
                url = "homepage";
                String username=sample;
                userBean.getUserInfo(username);
            } else {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(username, new FacesMessage(
                        "Invalid Username and or Password"));
            }
            return url;
        }

UserBean.java

package com.test.mypackage

@Component
Public Class UserBean{

@Autowired
private UserService userServie  // getters and setters.

     public void getUserInfo(String userId){
      userService.findByUserId(userId)
 }
}
}

UserService.java

package com.test.service;

public interface UserService {

    public void save(User User);
    public void update(User user);
    public void delete(User user);
    public User findByUserId(String userId);

}

サーバーが起動したときに、使用しようとしているBeanが事前にインスタンス化されていることがわかります。inをとして定義applicationContext.xmlしています。 そして、私はこのように私のすべての豆を定義していますweb.xmlContext-paramSpring.xml

spring.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />

<context:component-scan base-package="com.test" />

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

</beans> 

これは私のクラスパスにあり、それをリソースとしてインポートしapplicationContext.xmlます。

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    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">

    <import resource="classpath:database/DataSource.xml" />

    <import resource="classpath:database/Hibernate.xml" />

   <import resource="classpath:config/Spring.xml" />

</beans>

私の顔-confi.xml

<application>
   <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    <resource-bundle>
        <base-name>com.test.boundles.messages</base-name>
        <var>msg</var>
    </resource-bundle>
    </application>

私のアプローチに問題があります。

4

1 に答える 1

1

より適切に処理されるべきであったことが複数あります。

<context:component-scan base-package="com.test" />1)必要なのは、annotation-configとAutowiredAnnotationBeanPostProcessorを削除することだけです。

理由:<context:annotation-config>と<context:component-scan>の違いドキュメントを参照してください。

2)UserBeanにスコープがありません。スコープについて言及しない場合、デフォルトのスコープはシングルトンになりますが、このコンテキストでは望ましくないと思います。

3)このインターフェースを実装するインスタンス化可能なクラスの代わりにインターフェースを使用しようとしています。

4)次に、自動配線される@Serviceで実装クラスをマークする必要があります。

5)コメントだけでなく、ゲッターとセッターがいることを願っています。

良い例については、このリンクを参照してください

参照:

于 2012-10-19T22:12:08.327 に答える