Thursday, 8 February 2018

How to inject properties and data using annotations ?


Use @Value annotation

@Value("Hello")
private String greeting1;
   
@Value("${greeting}")
private String greeting2;


public void draw() {
        System.out.println(greeting1 + " | " + greeting2);

}

Provide your messages in application.properties
greeting=Bonjour

If you have same property in 2 different files, application.properties will override the value.


You can also use Environment, to get the message.
Useful when any message is used once only in the code.
@Autowired
Environment env;

public void draw() {
        System.out.println(env.getProperty("greeting"));

}



NESTED MESSAGES (in application.properties)

app=MyApp
greeting=Hello ${app}


SYSTEM PROPERTIES

ver=${java.version}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.