What is IOC Scope?

Property

  • Variable setting value in spring.
  • The Environment role is property source setting and get property value.

Property Priority

  • ServletConfig parameter
  • ServletContext parameter
  • JNDI (java:comp/env)
  • JVM system property (-Dkey=”value)
  • JVM system environment value

Create properties file like below.

base

And write annotation ‘@PropertySource(“classpath:/app.properties”)’.

1
2
3
4
5
6
7
8
@SpringBootApplication
@PropertySource("classpath:/app.properties")
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Then can get anywhere for example ‘environment.getProperty(“app.about”)’.

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component
public class ProRunner implements ApplicationRunner {

@Autowired
ApplicationContext ctx;

@Override
public void run(ApplicationArguments args) throws Exception {
Environment environment = ctx.getEnvironment();

System.out.println(environment.getProperty("app.about"));
}
}

The expected result is like below.

1
spring