A Standalone Java Program with Spring Container..

.. configured with a class, bootstrapped in code!
  • Standalone!
  • Maven!
  • Spring!
  • Java Configuration! (no XML!)
  • Programmatically booted Spring! (no XML!)

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>com.wiley.beginningspring</groupId>
    <version>0.0.1-SNAPSHOT</version>
 
    <artifactId>java-based-configuration</artifactId>
 
    <dependencies>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
 
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
        </dependency>
 
    </dependencies>
 
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.wiley.beginningspring.ch2.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Ch2BeanConfiguration.java
@Configuration
public class Ch2BeanConfiguration {
 
    @Bean
    public AccountService accountService() {
        AccountServiceImpl bean = new AccountServiceImpl();
        bean.setAccountDao(accountDao());
        return bean;
    }
 
    @Bean
    public AccountDao accountDao() {
        AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
        return bean;
    }
}

AccountServiceImplTest.java
@RunWith(MockitoJUnitRunner.class)
public class AccountServiceImplTest {
 
    @InjectMocks
    AccountServiceImpl accountServiceImpl = new AccountServiceImpl();
 
    @Mock
    private AccountDao accountDao;
 
    @Test
    public void testTransferMoney() {
        Account source = new Account();
        source.setId(1L);
        source.setBalance(10);
 
        Account target = new Account();
        target.setId(2L);
        target.setBalance(0);
 
        Mockito.when(accountDao.find(1L)).thenReturn(source);
        Mockito.when(accountDao.find(2L)).thenReturn(target);
 
        double amount = 10;
        accountServiceImpl.transferMoney(source.getId(), target.getId(), amount);
 
        Assert.assertTrue(source.getBalance() == 0);
        Assert.assertTrue(target.getBalance() == 10);
 
        Mockito.verify(accountDao).update(source);
    }
 
    @Test
    public void testDepositMoney() throws Exception {
        Account account = new Account();
        account.setId(1L);
        account.setBalance(10);
 
        Mockito.when(accountDao.find(1L)).thenReturn(account);
 
        accountServiceImpl.depositMoney(1L, 10);
 
        Assert.assertTrue(account.getBalance() == 20);
    }
 
    @Test
    public void testGetAccount() {
        Account account = new Account();
        Mockito.when(accountDao.find(1L)).thenReturn(account);
        Account returned = accountServiceImpl.getAccount(1L);
        Assert.assertTrue(account.equals(returned));
    }
}

Main.java
public class Main {
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = 
            new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);
        AccountService accountService 
            = applicationContext.getBean("accountService", AccountService.class);
 
        System.out.println("Before money transfer");
        System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());
        System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());
 
        accountService.transferMoney(1, 2, 5.0);
 
        System.out.println("After money transfer");
        System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());
        System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());
    }
}


In Action

Korays-MacBook-Pro:java-based-configuration koraytugay$ mvn exec:java
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building java-based-configuration 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Before money transfer
Account 1 balance :10.0
Account 2 balance :20.0
After money transfer
Account 1 balance :5.0
Account 2 balance :25.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.594 s
[INFO] Finished at: 2015-06-30T12:15:39+03:00
[INFO] Final Memory: 7M/309M
[INFO] ------------------------------------------------------------------------