gpt4 book ai didi

java - Vaadin 7 + Spring + VaadinSpringIntegration 附加组件 - Autowired bean 为空

转载 作者:行者123 更新时间:2023-11-30 09:10:23 24 4
gpt4 key购买 nike

所以我有一个我一直在使用的 Vaadin 7 应用程序,但现在需要集成 JAAS 并最终集成 OpenAM 以进行身份​​验证和授权。我注意到很多人都在使用 Spring 来让它工作,所以我选择了那条路。我使用了 VaadinSpringIntegration 插件,我认为它设置正确,但是当我启动我的应用程序时,我的 Autowiring bean 为空。我以前从未使用过 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 都是空的...有人可以帮我解决这个问题吗?

最佳答案

我一次带他们 1 个:

1) 除非另有说明,SpringVaadinServlet将使用 SpringUIProvider从上下文中检索 bean。如果您确实需要覆盖默认值,请确保 DashboardUIProvider 正在从 spring 应用程序上下文中检索 UI 实例,以便“自动魔术”发生:

<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

关于java - Vaadin 7 + Spring + VaadinSpringIntegration 附加组件 - Autowired bean 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com