Created
October 3, 2014 11:14
-
-
Save anonymous/bf5b01686f03dadc57c7 to your computer and use it in GitHub Desktop.
Spring Based Java Project - Beans Loading Twice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-4.0.xsd | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> | |
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/> | |
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> | |
<property name="mediaTypes"> | |
<map> | |
<entry key="atom" value="application/atom+xml" /> | |
<entry key="html" value="text/html" /> | |
<entry key="json" value="application/json" /> | |
<entry key="xml" value="application/xml"/> | |
</map> | |
</property> | |
<property name="viewResolvers"> | |
<list> | |
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> | |
<bean | |
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> | |
<property name="prefix" value="/WEB-INF/pages/" /> | |
<property name="suffix" value=".jsp" /> | |
</bean> | |
</list> | |
</property> | |
<property name="defaultViews"> | |
<list> | |
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> | |
</list> | |
</property> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> | |
<display-name>scf-webapp</display-name> | |
<servlet> | |
<servlet-name>mvc-dispatcher</servlet-name> | |
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
<!-- Looks for the file named mvc-dispatcher-servlet.xml - used to load | |
all spring MVC related items --> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>mvc-dispatcher</servlet-name> | |
<url-pattern>/</url-pattern> | |
</servlet-mapping> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
</listener> | |
<context-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>classpath:config/webAppEntryPoint-Context.xml</param-value> | |
</context-param> | |
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-4.0.xsd | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> | |
<context:annotation-config /> | |
<!-- autoscan all the webapp components --> | |
<context:component-scan base-package="com.app.webapp" /> | |
<!-- Load the application configuration file --> | |
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> | |
<property name="locations"> | |
<list> | |
<value>classpath:config/properties/app-${ENVIRONMENT}.properties</value> | |
<value>classpath:config/properties/database/appDB-${ENVIRONMENT}.properties</value> | |
</list> | |
</property> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("rawtypes") | |
@Component | |
public class WebappStartupTasks implements ApplicationListener { | |
/** Local log variable. **/ | |
private static final Logger LOG = LoggerFactory.getLogger(WebappStartupTasks.class); | |
@Override | |
public void onApplicationEvent(ApplicationEvent event) { | |
if (event instanceof ContextRefreshedEvent) { | |
long startTime = System.currentTimeMillis(); | |
LOG.info("StartupTasks - Startup Tasks Initiated"); | |
s | |
long endTime = System.currentTimeMillis(); | |
long totalTime = endTime - startTime; | |
LOG.info("StartupTasks - Startup Tasks Completed in {} milliseconds.", totalTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couple of suggestions for improvement. Make your application listener generic use
ApplicationListener< ContextRefreshedEvent>
saves you an if statement and saves the context from calling the listener with a type it doesn't handle.Use version less xdd so remove
-4.0
from the xsd locations , that will make sure you are always using the version belonging to the version of spring you are using.Your
PropertyPlaceholderConfigurer
is best replaced with a<context:property-placeholder />
as this will register the more powerfulPropertySourcesPlaceholderConfigurer
.You can remove the
<context:annotation-config/>
that is already implied by the<context:component-scan />