Spring Configuration Examples With XML and Java

Some examples below...

XML Configuration Example

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
 <mvc:annotation-driven/>
 
 <bean class="com.wrox.GreetingServiceImpl" name="greetingServiceImpl"/>
 <bean class="com.wrox.HelloController" name="helloController">
  <property name="greetingService" ref="greetingServiceImpl"/>
 </bean>
 
</beans>

Java Configuration Example

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.wrox.site", 
useDefaultFilters = false, 
includeFilters = @ComponentScan.Filter(Controller.class))
public class ServletContextConfiguration {
}