Very Very Very Simple Spring Managed Java Application Example.

Ah this simple ones..

This is my Directory Layout..

The jar files you see in the lib directory are our dependencies. I think these are the minimum dependencies for a Spring - managed Java application.

Lets move on to source code..
package biz.tugay.learnspring;
public interface Service {
    public int getServiceNumber();
package biz.tugay.learnspring;
public class ServiceImpl implements Service {
    public int getServiceNumber() {
        return 5;
package biz.tugay.learnspring;
public interface ServiceConsumer {
    public int consumeService();
package biz.tugay.learnspring;
public class ServiceConsumerImpl implements ServiceConsumer {
    private Service service;
    public int consumeService() {
        return service.getServiceNumber();
    public void setService(Service service) {
        this.service = service;
package biz.tugay.learnspring;
import org.springframework.context.ApplicationContext;
public class TestClass {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        ServiceConsumer serviceConsumer = (ServiceConsumer) applicationContext.getBean("serviceConsumer");
        int serviceNumber = serviceConsumer.consumeService();
And of course, we have the infamous beans.xml..
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean class="biz.tugay.learnspring.ServiceImpl" id="service" />
    <bean class="biz.tugay.learnspring.ServiceConsumerImpl" id="serviceConsumer">
        <property name="service" ref="service"/>
This is how I compiled my code:
$ javac -d ./target -cp ./lib/spring-core-4.2.5.jar:./lib/spring-context-4.2.5.jar:./lib/spring-beans-4.2.5.jar biz/tugay/learnspring/*.java
Please note: You must create a directory with name target before compiling, or the compiler will complain saying: "javac: directory not found: ./target"

Now, lets run our application..
java -cp ./lib/spring-expression-4.2.5.jar:./lib/commons-logging-1.2.jar:./:./lib/spring-core-4.2.5.jar:./lib/spring-context-4.2.5.jar:./lib/spring-beans-4.2.5.jar:./target biz.tugay.learnspring.TestClass
Mar 04, 2016 9:37:31 PM prepareRefresh
INFO: Refreshing startup date [Fri Mar 04 21:37:31 EET 2016]; root of context hierarchy
Mar 04, 2016 9:37:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]

So what is so cool about this? The cool thing is the fact that, Spring Container actually injected ServiceImpl to ServiceConsumerImpl. We did not have to do it. Well maybe we did. In a way. In the beans.xml file..