Vaadin 7 + Spring + VaadinSpringIntegration Add-on - Autowired bean为null

时间:2014-03-21 16:58:17

标签: java spring ejb jaas vaadin7

所以我有一个我一直在研究的Vaadin 7应用程序,但是现在需要集成JAAS并最终将OpenAM集成到身份验证和授权中。我注意到很多人都使用Spring来让它工作,所以我走了那条路。我使用了VaadinSpringIntegration插件,我认为它设置正确,但是当我启动我的应用程序时,我的autowired bean为null。我之前从未使用过Spring,所以我很可能忽略了一些东西。

所以这是我的web.xml的一部分

<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Hoplite Tool</display-name>

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
    <servlet-name>Hoplite</servlet-name>
    <servlet-class>org.hoplite.servlet.HopliteServlet</servlet-class>
    <init-param>
        <param-name>UI</param-name>
        <param-value>org.hoplite.dashboard.DashboardUI</param-value>
    </init-param>
    <init-param>
        <param-name>widgetset</param-name>
        <param-value>org.hoplite.dashboard.DashboardWidgetSet</param-value>
    </init-param>
    <init-param>
        <param-name>UIProvider</param-name>
        <param-value>org.hoplite.dashboard.DashboardUIProvider</param-value>
    </init-param>

    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>BAT3</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

这是我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/lang
 http://www.springframework.org/schema/lang/spring-lang.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config />
<context:component-scan base-package="org.hoplite.dashboard" />       

</beans>

我必须为webapp包含一些javascript,所以我扩展了SpringVaadinServlet

public class HopliteServlet extends SpringVaadinServlet {

/**
 * Attempt to load extra javascript
 * 
 * @throws ServletException
 */
@Override
protected void servletInitialized() throws ServletException {
    super.servletInitialized();
    getService().addSessionInitListener(new SessionInitListener() {

        @Override
        public void sessionInit(SessionInitEvent event) throws ServiceException {
            event.getSession().addBootstrapListener(new BootstrapListener() {

                @Override
                public void modifyBootstrapPage(BootstrapPageResponse response) {

                    response.getDocument().head().appendElement("script").attr("type", "text/javascript").attr("src", "https://www.google.com/jsapi");

                }

                @Override
                public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
                }
            });
        }
    });
}

}

最后我的UI类有一个Autowired bean(抱歉该类很长,所以我只能提供一个代码段)

@Push(PushMode.MANUAL)
@Theme("dashboard")
@Title("Hoplite Tool")
@Component
@Scope("prototype")
public class DashboardUI extends UI {

      @Autowired
      private LoginBean loginBean; 

      ...

      //add listener to button
      signin.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
           String name = loginBean.performLogin(username.getValue().toString(), password.getValue().toString());
           buildMainView();
        }
}

这是loginBean类

@Component
@Scope("prototype")
public class LoginBean {

    @Autowired
    private HttpServletRequest request;

    /**
     * Logs the user in with given username and password.
     * 
     * @param username
     * @param password
     * @return principal name if login is successful
     * @throws ServletException if login fails
     */
    public String performLogin(String username, String password) throws ServletException {

        // If login fails, we throw exception. Otherwise return the principal
        // name of logged in user.
        request.login(username, password);
        return request.getUserPrincipal().getName();
    }

    /**
     * Logs out the current user.
     * 
     * @throws ServletException if logout fails
     */
    public void performLogout() throws ServletException {
        request.logout();
    }
}

我的bean类和UI类在org.hoplite.dashboard

然而,每次我获得主UI时,loginBean都为null ...任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我一次只接受他们一次:

1)除非另有说明,否则SpringVaadinServlet将使用SpringUIProvider从上下文中检索bean。如果您绝对需要覆盖默认值,请确保DashboardUIProvider正在从spring应用程序上下文中检索UI实例,以便&#34; auto-magic&#34;可能发生:

<init-param>
    <param-name>UIProvider</param-name>
    <param-value>org.hoplite.dashboard.DashboardUIProvider</param-value>
</init-param>

2)如果您丢弃DashboardUIProvider,请注意默认的SpringUIProvider需要beanName参数而不是UI所以更改:

<init-param>
    <param-name>UI</param-name>
    <param-value>org.hoplite.dashboard.DashboardUI</param-value>
</init-param>

<init-param>
    <param-name>beanName</param-name>
    <param-value>dashboardUI</param-value>
</init-param>

其中dashboardUI是spring将用于UI的隐式名称。如果您更改它,请确保使用新的

更新您的web.xml