Skip to content

Instantly share code, notes, and snippets.

@stevehanson
Last active December 28, 2015 20:29
Show Gist options
  • Save stevehanson/7557296 to your computer and use it in GitHub Desktop.
Save stevehanson/7557296 to your computer and use it in GitHub Desktop.
Spring simple app. Used for tutorial session.
<!-- src/main/resources/applicationContext.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorial"/>
</beans>
package com.tutorial;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppMain {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
GreetService svc = cxt.getBean(GreetService.class);
System.out.println(svc.greet("steve"));
}
}
package com.tutorial;
import org.springframework.stereotype.Component;
@Component
public class Greeter {
public String greet(String name) {
return "Hello " + name;
}
}
package com.tutorial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GreetService {
Greeter greeter;
@Autowired
public GreetService(Greeter greeter) {
this.greeter = greeter;
}
public String greet(String name) {
return greeter.greet(name);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springtut</groupId>
<artifactId>spring-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment