我无法理解Spring MVC配置和初始化。我不确定何时使用注释以及何时不使用注释!为什么我的spring上下文初始化了两次?
我的应用程序由两个战争模块构成。我并不太在意在两者中复制bean容器。但在启动两个模块两次初始化Spring上下文在此日志从一个模块从启动取证据:
[2015-08-15 03:49:51,416] Artifact frontend:war exploded: Deploy took 27,529 milliseconds
15:50:00.051 DEBUG com.ocscommerce.admin.configuration.WebConfig 53 <init> - Initialising WebConfig
15:50:06.409 DEBUG com.ocscommerce.admin.configuration.AppConfig 48 <init> - Loading application context
15:50:06.424 DEBUG com.ocscommerce.admin.configuration.SecurityConfig 54 <init> - Loading security config.
15:50:07.327 DEBUG com.ocscommerce.services.configuration.SolrConfig 43 <init> - Loading SOLR Config.
2015-08-15 15:50:08.194 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-08-15 15:50:08.200 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
15:50:11.269 DEBUG com.ocscommerce.admin.configuration.WebConfig 53 <init> - Initialising WebConfig
15:50:13.498 DEBUG com.ocscommerce.admin.configuration.AppConfig 48 <init> - Loading application context
15:50:13.503 DEBUG com.ocscommerce.admin.configuration.SecurityConfig 54 <init> - Loading security config.
15:50:13.999 DEBUG com.ocscommerce.services.configuration.SolrConfig 43 <init> - Loading SOLR Config.
2015-08-15 15:50:14.336 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-08-15 15:50:14.337 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
为什么有4个memcached的连接,我不知道,但这是另一个问题一天....
为了今天,我只是想弄清楚为什么我的背景下正在为每个模块初始化两次......
web.xml中 - 拥有了AppConfig参考,并到webconfig在XML中的类。每个模块都有单独的web.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/pages/errors/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/pages/errors/500.jsp</location>
</error-page>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.ocscommerce.admin.configuration.AppConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</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>
com.ocscommerce.admin.configuration.WebConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<display-name>springMultipartFilter</display-name>
<filter-name>springMultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>springMultipartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<display-name>springSecurityFilterChain</display-name>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>ERROR</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
然后在AppConfig类中,它会导入一些可能与问题无关的其他bean定义类。签名是这样的:
@Configuration
@PropertySource(value = {"classpath:application.properties"})
@EnableScheduling
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableTransactionManagement
@ComponentScan({"com.ocscommerce.admin", "com.ocscommerce.services"})
@Import(value = {SolrConfig.class, SecurityConfig.class, PersistenceConfig.class})
public class AppConfig {
}
然后WebConfig扩展WebMvcAdapter并具有这样的成分:
@Configuration
@EnableWebMvc
@ComponentScan({"com.ocscommerce.admin", "com.ocscommere.services"})
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
public class WebConfig extends WebMvcConfigurerAdapter {
}
我想知道如果我有太多的组件扫描呢?
有人可以帮我修复这个配置吗?
谢谢...
看到以前的答案,我从webconfig拿出部分扫描,但服务器不会启动,因为它抱怨从服务层缺失的自动装配依赖。我使用Intellij,它抱怨它也没有,我们已经检查过类路径。 –
您不应该从'WebConfig'中删除所有组件扫描,只能删除特定于您的应用程序域的组件扫描。例如,'WebConfig'应该只扫描那些与Spring MVC相关的类,例如与Spring MVC或'@ Controller'类有关的'@ Configuration'。 Spring应该在调度程序上下文中创建这些对象。 (续...) –
(...继续)'AppConfig'应扫描与应用程序域相关的类,并且Spring应该在根上下文中创建这些对象。 Spring调度程序上下文继承了根上下文,因此调度程序上下文中的对象(如控制器)可以访问根上下文中的对象。 –