Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autowire a Spring Environment object

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

like image 557
Webnet Avatar asked Mar 13 '13 14:03

Webnet


People also ask

How do we inject the environment in Spring?

Instead of getting Environment instance by using ConfigurableApplicationContext#getEnvironment() we can directly inject it as a bean.

How do I get environment properties in Spring boot?

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.

How do you Autowire in Spring?

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.


2 Answers

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();
like image 141
Michail Nikolaev Avatar answered Oct 21 '22 11:10

Michail Nikolaev


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"} )
like image 24
slashron Avatar answered Oct 21 '22 13:10

slashron