Spring Framework Hello World - 2

Annotation configuration - Java Bootstrap in a Standalone Java App!!

Directory Layout

pom.xml
<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/maven-v4_0_0.xsd">
 
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>biz.tugay</groupId>
     <artifactId>begsprch01</artifactId>
     <packaging>jar</packaging>
 
     <version>1.0-SNAPSHOT</version>
 
     <name>begsprch01</name>
     <url>http://www.tugay.biz</url>
 
     <dependencies>
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context</artifactId> 
            <version>4.0.5.RELEASE</version>
        </dependency>
    </dependencies>
 
     <build>
        <finalName>begsprch01</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.begsprch01.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Foo.java
package biz.tugay.begsprch01.service;
 
public interface Foo {
    void foo();
}

FooImpl.java
package biz.tugay.begsprch01.service;
 
public class FooImpl implements Foo {
    public void foo() {
        System.out.println("FooImpl#foo()");
    }
}

Config.java
package biz.tugay.begsprch01;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
 
import biz.tugay.begsprch01.service.Foo;
import biz.tugay.begsprch01.service.FooImpl;
 
@Configuration
public class Config {
 
    @Bean
    public Foo foo() {
        final Foo foo = new FooImpl();
        return foo;
    }
}

App.java
package biz.tugay.begsprch01;
 
import biz.tugay.begsprch01.service.Foo;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class App {
    public static void main(String[] args) {
        final ApplicationContext appContx = new AnnotationConfigApplicationContext(Config.class);
        final Foo foo = (Foo) appContx.getBean("foo");
        foo.foo();
    }
}

Maven Build & Exec
MacBook-Pro:begsprch01 koraytugay$ mvn -q clean install
MacBook-Pro:begsprch01 koraytugay$ mvn -q exec:java
Sep 09, 2017 8:34:50 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@70b7ca0: startup date [Sat Sep 09 20:34:50 CEST 2017]; root of context hierarchy
FooImpl#foo()
MacBook-Pro:begsprch01 koraytugay$