I'm attempting to get properties from a .properties file within an object of my application. My only knowledge of how to do this is via the Environment
object.
It works great in my configuration...
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value = {
"classpath:/application.properties",
"classpath:/environment-${FETTER_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repositories")
@EnableTransactionManagement
public class Config {
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USER = "db.user";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
@Resource
Environment environment;
But I try to do it in another class and it's null. I've tried using @Resource
and @Autowired
and manual injection.
Is there something special about Environment
that's preventing this?
@Component
public class Vendor {
private String token;
@Autowired
private Environment environment;
public Vendor() {
//get token from config
//Environment environment = (Environment) ApplicationContextProvider.getApplicationContext().getBean("environment");
setToken(environment.getProperty("api.vendor.token"));
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
Update: I found this resource which highlights the usage of @Value
, but I don't use any XML configuration in my application.
It looks like this might be what I'm looking for: Loading properties in Spring 3.1 programmatically
Instead of getting Environment instance by using ConfigurableApplicationContext#getEnvironment() we can directly inject it as a bean.
Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.
In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.
You need to use @Value annotaion for it. But before it is required to declare <context:property-placeholder
in xml.
See here for details.
Also, another way:
@Inject
ApplicationContext context;
......
Enviroment env = context.getEnvironment();
You can use spring @Autowired
annotation as below, but make sure you annotate your class with @Component
or similar ones.
@Autowired
Environment env;
Also make sure your bean is visible for scanning, ie it should be inside com.production
package in your case.
Component scanning should be enabled like this
<context:component-scan base-package="com.production"/>
or
@ComponentScan(basePackages = {"com.production"} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With