Jak Mogę dostać wiosenną fasolkę w filtrze servlet?
Zdefiniowałem javax.servlet.Filter
i mam klasę Java z adnotacjami Spring.
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
@Configuration
public class SocialConfig {
// ...
@Bean
public UsersConnectionRepository usersConnectionRepository() {
// ...
}
// ...
}
Chcę dostać fasolkę UsersConnectionRepository
w moim Filter
, więc spróbowałem:
public void init(FilterConfig filterConfig) throws ServletException {
UsersConnectionRepository bean = (UsersConnectionRepository) filterConfig.getServletContext().getAttribute("#{connectionFactoryLocator}");
}
Ale zawsze zwraca null
. Jak Mogę dostać wiosenną fasolkę w Filter
?
4 answers
Try:
UsersConnectionRepository bean =
(UsersConnectionRepository)WebApplicationContextUtils.
getRequiredWebApplicationContext(filterConfig.getServletContext()).
getBean("usersConnectionRepository");
Gdzie usersConnectionRepository
jest nazwą / id twojej fasoli w kontekście aplikacji. Albo jeszcze lepiej:
UsersConnectionRepository bean = WebApplicationContextUtils.
getRequiredWebApplicationContext(filterConfig.getServletContext()).
getBean(UsersConnectionRepository.class);
Spójrz także na GenericFilterBean i jego podklasy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-24 21:30:22
Są trzy sposoby:
-
Użycie
WebApplicationContextUtils
:public void init(FilterConfig cfg) { ApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(cfg.getServletContext()); this.bean = ctx.getBean(YourBeanType.class); }
Korzystanie z
DelegatingFilterProxy
- mapujesz ten filtr i deklarujesz swój filtr jako bean. Delegujący serwer proxy wywoła wszystkie beansy implementujące interfejsFilter
.-
Użyj
@Configurable
na filtrze. Wolałbym jednak jedną z dwóch pozostałych opcji. (Ta opcja używa aspectj)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-24 21:25:48
[[2]}Wiosna ma narzędzie właśnie do tego.
W kodzie filtra Nadpisz metodę init w następujący sposób:
public void init(FilterConfig cfg) {
super.init(cfg);
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
Następnie po prostu wstrzyknij fasolę do tego filtra, tak samo jak każdą inną fasolę, którą wstrzykniesz.
@Inject
private UsersConnectionRepository repository;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-12 08:28:24
Extends this below class.
abstract public class SpringServletFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//must provide autowiring support to inject SpringBean
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext());
}
@Override
public void destroy() { }
abstract public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-10 05:25:24