If you getting from application.properties file you can use Environment class.
Like that
Autowired
private Environment environment;
...
environment.getProperty("propertyName")
or you can define your own property file. then you can get from it with
@PropertySource(name = "myProperties", value = "example.properties")
annotation
You need to use @Value annotation to get a specific value from the property file which you defined.
@Value("${propertyNameInYourPropertFile}")
private String url;
And You want to start something when Application is just started, you can use this before a method
@EventListener(ApplicationReadyEvent.class)
But need to use @Service or @Component Annotation, which Class has the method.
Totally, You can use this.
example.properties :
url=yourValue
userName=yourDBUserName
password=yourDBPassword
example class :
@Service
@PropertySource(name = "myProperties", value = "example.properties")
public class Start{
@Value("${url}")
private String url;
@Value("${userName}")
private String userName;
@Value("${password}")
private String password;
//Run this method when application started
@EventListener(ApplicationReadyEvent.class)
public ResultSet getConnection()
{
//Connect to Database
Connection connection = null;
String QUERY="your sql query";
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connection = DriverManager.getConnection(url, userName, password );
} catch (SQLException e) {
}
//Run your query
Statement stmt = null;
try {
stmt = connection.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
ResultSet rs = null;
try {
rs = stmt.executeQuery(QUERY);
} catch (SQLException e1) {
e1.printStackTrace();
}
return rs;
}
}