下面列出了怎么用javax.servlet.annotation.WebListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Is this a web annotation.
*
* @param annotation the annotation.
* @return true if it is, false otherwise.
*/
private boolean isWebAnnotation(Annotation annotation) {
return annotation instanceof WebServlet
|| annotation instanceof WebListener
|| annotation instanceof WebInitParam
|| annotation instanceof WebFilter
|| annotation instanceof ServletSecurity
|| annotation instanceof MultipartConfig;
}
@Override
public void init(ServletContextHandler context) {
String pkgx = CoreConfig.getInstance("defines" )
.getProperty("apply.serv");
if ( pkgx != null ) {
String[] pkgs = pkgx.split(";");
for(String pkgn : pkgs) {
pkgn = pkgn.trim ( );
if ( pkgn.length( ) == 0 ) {
continue;
}
Set<String> clss = getClss(pkgn);
for(String clsn : clss) {
Class clso = getClso(clsn);
WebFilter wf = (WebFilter ) clso.getAnnotation(WebFilter.class );
if (null != wf) {
addFilter (context, clso, wf);
}
WebServlet wb = (WebServlet ) clso.getAnnotation(WebServlet.class );
if (null != wb) {
addServlet (context, clso, wb);
}
WebListener wl = (WebListener) clso.getAnnotation(WebListener.class);
if (null != wl) {
addListener(context, clso, wl);
}
}
}
}
}
@Override
public Collection<ClasspathScanRequest> classpathScanRequests() {
return classpathScanRequestBuilder()
.annotationType(WebFilter.class)
.annotationType(WebServlet.class)
.annotationType(WebListener.class)
.build();
}
@Override
public InitState initialize(InitContext initContext) {
if (servletContext != null) {
listenerDefinitions.addAll(
detectListeners(initContext.scannedClassesByAnnotationClass().get(WebListener.class)));
filterDefinitions.addAll(detectFilters(initContext.scannedClassesByAnnotationClass().get(WebFilter.class)));
servletDefinitions.addAll(
detectServlets(initContext.scannedClassesByAnnotationClass().get(WebServlet.class)));
}
return InitState.INITIALIZED;
}
@SuppressWarnings("unchecked")
private Class<? extends EventListener> getTargetListener(AnnotationInfo<WebListener> annotationInfo) {
return (Class<? extends EventListener>) annotationInfo.getTargetType();
}
@Override
protected void webConfig() {
if (context.getServletContext().getAttribute("meecrowave.configuration") == null) { // redeploy
context.getServletContext().setAttribute("meecrowave.configuration",
Meecrowave.Builder.class.isInstance(configuration) ? configuration : new Meecrowave.Builder(configuration));
context.addServletContainerInitializer(intializer, emptySet());
}
if (!configuration.isTomcatScanning()) {
super.webConfig();
return;
}
// eagerly start CDI to scan only once and not twice (tomcat+CDI)
final ClassLoader loader = context.getLoader().getClassLoader(); // should already be started at that point
final Thread thread = Thread.currentThread();
final ClassLoader old = thread.getContextClassLoader();
thread.setContextClassLoader(loader);
try {
final OWBTomcatWebScannerService scannerService = OWBTomcatWebScannerService.class.cast(WebBeansContext.getInstance().getScannerService());
scannerService.setFilter(ofNullable(context.getJarScanner()).map(JarScanner::getJarScanFilter).orElse(null), context.getServletContext());
scannerService.setDocBase(context.getDocBase());
scannerService.setShared(configuration.getSharedLibraries());
if (configuration.getWatcherBouncing() > 0) { // note that caching should be disabled with this config in most of the times
watcher = new ReloadOnChangeController(context, configuration.getWatcherBouncing());
scannerService.setFileVisitor(f -> watcher.register(f));
}
scannerService.scan();
finder = scannerService.getFinder();
finder.link();
final CdiArchive archive = CdiArchive.class.cast(finder.getArchive());
Stream.of(WebServlet.class, WebFilter.class, WebListener.class)
.forEach(marker -> finder.findAnnotatedClasses(marker).stream()
.filter(c -> !Modifier.isAbstract(c.getModifiers()) && Modifier.isPublic(c.getModifiers()))
.forEach(webComponent -> webClasses.computeIfAbsent(
archive.classesByUrl().entrySet().stream()
.filter(e -> e.getValue().getClassNames().contains(webComponent.getName()))
.findFirst().get().getKey(), k -> new HashSet<>())
.add(webComponent)));
} finally {
thread.setContextClassLoader(old);
}
try {
super.webConfig();
} finally {
webClasses.clear();
finder = null;
}
}
@Override
public boolean matches(final Class<?> type) {
return FeatureUtils.is(type, EventListener.class)
&& FeatureUtils.hasAnnotation(type, WebListener.class)
&& hasMatch(type, SUPPORTED);
}
public void findListeners(@Observes @WithAnnotations({WebListener.class})
ProcessAnnotatedType<? extends ServletContextListener> pat) {
listeners.add(pat.getAnnotatedType().getJavaClass());
}