Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2014 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bf5b01686f03dadc57c7 to your computer and use it in GitHub Desktop.
Save anonymous/bf5b01686f03dadc57c7 to your computer and use it in GitHub Desktop.
Spring Based Java Project - Beans Loading Twice
<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>
<?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>
<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>
@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);
}
}
}
@mdeinum
Copy link

mdeinum commented Oct 3, 2014

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.

xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

Your PropertyPlaceholderConfigurer is best replaced with a <context:property-placeholder /> as this will register the more powerful PropertySourcesPlaceholderConfigurer.

<context:property-placeholder location="classpath:config/properties/app-${ENVIRONMENT}.properties,classpath:config/properties/database/appDB-${ENVIRONMENT}.properties" />

You can remove the <context:annotation-config/> that is already implied by the <context:component-scan />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment