Mit*_*ani 68 java spring spring-annotations spring-3
我在web应用程序中一起使用jsf和spring.我已经在一个使用注释@Configuration, @ComponentScan等的配置类中配置了数据源和会话工厂.我的项目中没有任何applicationContext.xml文件,因为我正在处理Configuration类中的每个上下文xml条目.测试用例成功运行但是当我部署我的Web应用程序时,它给了我错误
java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener?
现在如果我在web.xml中给出listener类,
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
它给了我错误,
找不到/WEB-INF/applicationContext.xml
按照的文件ContextLoaderListener,这是真的,如果我不给contextConfigLocation在参数web.xml明确,它会搜索名为默认的Spring上下文文件applicationContext.xml中web.xml.现在,如果我不想使用spring上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册监听器类,ContextLoaderListener以便不使用xml文件和仅使用注释,我能够使用spring和jsf运行我的Web应用程序?
tol*_*ius 132
在web.xml您需要使用以下内容引导上下文AnnotationConfigWebApplicationContext:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
org.package.YouConfigurationAnnotatedClass
</param-value>
</init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)
并且不要忘记使用@EnableWebMvcMVC注释来启动.
进一步阅读:
是的,你当然需要一个听众.虽然上面完全回答了" 如何在web.xml中注册Spring @Configuration带注释的类而不是applicationContext.xml文件 "这一问题,但这里有一个Spring官方文档的例子,它完整地布局web.xml:
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
Ang*_*gad 11
在这里提出一个老问题,但是最新版本的Spring(v3.0 +)现在你可以完全摆脱web.xml,只要你在支持Servlet 3.0+的Web容器上部署你的应用程序.
可以实现Spring的WebApplicationInitializer接口来执行与web.xml中相同的配置.在Servlet 3.0+容器上运行的Spring 3.0+应用程序将自动检测此实现类.
如果设置相当简单,您可以使用Spring提供的另一个类,如下所示.这里所做的就是设置@Configuration类并列出servlet映射.保持设置非常简单.
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {AppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {
"*.html"
,"*.json"
,"*.do"};
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85213 次 |
| 最近记录: |