Standalone Spring Example with Java Configuration and Command Line Compile & Run

Like a boss!

This is the directory structure I have:

Here is the source code..
package biz.tugay.learnspring;
 
public interface Service {
    int getServiceNumber();
}
 
package biz.tugay.learnspring;
 
public class ServiceImpl implements Service {
    public int getServiceNumber() {
        return 5;
    }
}
 
package biz.tugay.learnspring;
 
public interface ServiceConsumer {
    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.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class BeanConfiguration {
 
    @Bean
    public Service service() {
        Service service = new ServiceImpl();
        return service;
    }
 
    @Bean
    public ServiceConsumer serviceConsumer() {
        ServiceConsumerImpl serviceConsumer = new ServiceConsumerImpl();
        serviceConsumer.setService(service());
        return serviceConsumer;
    }
}
 
package biz.tugay.learnspring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
 
public class TestClass{
 
    public static void main(String[] args) {
        final ApplicationContext applicationContext 
         = new AnnotationConfigApplicationContext(BeanConfiguration.class);
 
        final ServiceConsumer serviceConsumer 
         = (ServiceConsumer) applicationContext.getBean("serviceConsumer");
        final int serviceNumber = serviceConsumer.consumeService();
        System.out.println(serviceNumber);
    }
}

Lets compile and run:
Korays-MacBook-Pro:learnspring koraytugay$ 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
Korays-MacBook-Pro:learnspring koraytugay$ java -cp ./lib/spring-aop-4.2.5.jar:./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 11:54:19 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Fri Mar 04 23:54:19 EET 2016]; root of context hierarchy
5