Spring from scratch

Table of Contents

1 Init the project

1.1 Project file structure

Generally, we use maven to manage the directory layout. Hence, you will get a standard directory layout like below:

project
    src
        main
            java
            [scala]
            resources
                environment.properties
                environment.test.properties
                environment.prod.properties
                config
                    application.yml
                    application-dev.yml
                    application-[profile].yml
                [other]
        test
            java
            [scala]
    target

1.1.1 Configuration

The files environment.[profile].properties are used to provide different profiles to maven. The files application-[profile].yml are used to provide different configurations for spring.

Since maven is used to manage the package dependencies, different maven profiles enables you to use different dependencies for development and production, e.g. run different tasks. I am not farmiliar with maven, and cannot tell how and when it can be used.

Spring configuration files let you to inject different configurations for development and production. For example, you can use different database for development and production (different host, port, username and password, etc.).

1.1.1.1 Inject configurations

You can get access to these configurations by creating a class with annotation @Configuration.

  • Inject the value of configuration to attribute of this class by @Value("${NAME:DEFAULT_VALUE}").
  • You can also inject Environment object.
    @Configuration
    public class AppConfig {
        @Inject Environment env;
    
        @Bean
        public MyBean myBean() {
            MyBean myBean = new MyBean();
            myBean.setName(env.getProperty("bean.name"));
            return myBean;
        }
    }
    

Get the configuration from configuration class in some where else. I haven't used this now.

public class ConfigurationTest {
    @Test
    public void testHelloworld () {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
        Assert.assertEquals("hello", ctx.getBean("message")); // message is the name of the Bean method in configuration class
    }
}
1.1.1.2 Load configuration files.

Different property sources are loaded in predefined order. You can set the default additional property file by

SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
if (!source.containsProperty("spring.profiles.active")){
    app.setAdditionalProfiles([profile_name])
}

1.1.2 Compile and run

You have to add spring-boot-maven-plugin as one of the plugins under build node in pom.xml.

  • Development
    • Compile the project by maven clean insatll
    • More than Build

      Maven can help running test, web application and produceing reports by plugins. TODO

    • Compile and run: mvn spring-boot:run.
  • Production
    • Package the files
      • Jar file. Run mvn clean package to build the jar file and run it through command line.
      • War file. Add <packaging>war</packaging> in pom.xml file, under the root node. Then run mvn package as building the jar file.
    • Deploy

1.1.3 TODO

Author: Xiao LIU

Created: 2015-02-28 Sat 22:37

Emacs 24.4.1 (Org mode 8.2.10)