A Standalone Java Application with Spring Framework!

Building with Maven, running manually!

This is how it all started:
mvn archetype:generate
And when asked, I picked:
700: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

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/xsd/maven-4.0.0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>backtoschool</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>backtoschool</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

copy-dependencies will make sure the dependencies are copied to target folder when we do maven build..

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="myNeedyClass" class="biz.tugay.MyNeedyClassImpl">
        <property name="myCrazyService" ref="myCrazyService"/>
    </bean>
 
    <bean id="myOtherNeedyClass" class="biz.tugay.MyNeedyClassImpl">
        <property name="myCrazyService" ref="myOtherService"/>
    </bean>
 
    <bean id="myCrazyService" class="biz.tugay.MyCrazyServiceImpl"/>
 
    <bean id="myOtherService" class="biz.tugay.MyOtherCrazyServiceImpl"/>
 
</beans>

*.java
public interface MyNeedyClass {
    void needyMethod();
}
 
public interface MyCrazyService {
    void someMethodHereThatPrintSomeStuff();
}
 
public class MyCrazyServiceImpl implements MyCrazyService {
    public void someMethodHereThatPrintSomeStuff() {
        System.out.println("Hello from Service Impl One!");
    }
}
 
import org.springframework.beans.factory.annotation.Required;
 
public class MyNeedyClassImpl implements MyNeedyClass {
 
    private MyCrazyService myCrazyService;
 
    public void needyMethod() {
        myCrazyService.someMethodHereThatPrintSomeStuff();
    }
 
    @Required
    public void setMyCrazyService(MyCrazyService myCrazyService) {
        this.myCrazyService = myCrazyService;
    }
}
 
public class MyOtherCrazyServiceImpl implements MyCrazyService {
    public void someMethodHereThatPrintSomeStuff() {
        System.out.println("Hello from Service Impl Two");
    }
}
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
 
    public static void main(String[] args) {
        // Initialize Spring Container:
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml");
 
        // Get bean from the Application Context:
        MyNeedyClass myNeedyClass = ctx.getBean("myNeedyClass", MyNeedyClass.class);
        myNeedyClass.needyMethod();
 
        MyNeedyClass myOtherNeedyClass = ctx.getBean("myOtherNeedyClass", MyNeedyClass.class);
        myOtherNeedyClass.needyMethod();
    }
}
Please note: All the classes have package biz.tugay; in the first line..


In Action

Korays-MacBook-Pro:backtoschool koraytugay$ ls
backtoschool.iml pom.xml   src
Korays-MacBook-Pro:backtoschool koraytugay$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building backtoschool 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ backtoschool ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ backtoschool ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ backtoschool ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to /Users/koraytugay/Development/backtoschool/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ backtoschool ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/koraytugay/Development/backtoschool/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ backtoschool ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ backtoschool ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ backtoschool ---
[INFO] Building jar: /Users/koraytugay/Development/backtoschool/target/backtoschool-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ backtoschool ---
[INFO] Installing /Users/koraytugay/Development/backtoschool/target/backtoschool-1.0-SNAPSHOT.jar to /Users/koraytugay/.m2/repository/biz/tugay/backtoschool/1.0-SNAPSHOT/backtoschool-1.0-SNAPSHOT.jar
[INFO] Installing /Users/koraytugay/Development/backtoschool/pom.xml to /Users/koraytugay/.m2/repository/biz/tugay/backtoschool/1.0-SNAPSHOT/backtoschool-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default) @ backtoschool ---
[INFO] Copying commons-logging-1.1.3.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/commons-logging-1.1.3.jar
[INFO] Copying spring-expression-4.0.5.RELEASE.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/spring-expression-4.0.5.RELEASE.jar
[INFO] Copying aopalliance-1.0.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/aopalliance-1.0.jar
[INFO] Copying spring-context-4.0.5.RELEASE.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/spring-context-4.0.5.RELEASE.jar
[INFO] Copying spring-aop-4.0.5.RELEASE.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/spring-aop-4.0.5.RELEASE.jar
[INFO] Copying spring-core-4.0.5.RELEASE.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/spring-core-4.0.5.RELEASE.jar
[INFO] Copying spring-beans-4.0.5.RELEASE.jar to /Users/koraytugay/Development/backtoschool/target/classes/lib/spring-beans-4.0.5.RELEASE.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.356 s
[INFO] Finished at: 2015-12-02T23:01:44+02:00
[INFO] Final Memory: 22M/304M
[INFO] ------------------------------------------------------------------------
Korays-MacBook-Pro:backtoschool koraytugay$ cd target
Korays-MacBook-Pro:target koraytugay$ ls 
backtoschool-1.0-SNAPSHOT.jar classes    maven-archiver   maven-status
Korays-MacBook-Pro:target koraytugay$ cd classes
Korays-MacBook-Pro:classes koraytugay$ java -cp "./:./lib/*" biz.tugay.App
Dec 02, 2015 11:02:02 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@439f5b3d: startup date [Wed Dec 02 23:02:02 EET 2015]; root of context hierarchy
Dec 02, 2015 11:02:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Hello from Service Impl One!
Hello from Service Impl Two