简体   繁体   中英

How to use @PropertySource with Java Spring 4.0.4?

I would like to add the source of database.properties which is in ProjectName/src/database.properties to AppConfig.class which is in ProjectName/src/device/spring/config according to https://www.journaldev.com/17053/spring-jdbctemplate-example

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import tekranchecklist.model.*;



@Configuration
@ComponentScan("device.spring.dao","device.model","device.spring.config","device.Main")

public class AppConfig {
   @Autowired
    Environment environment;

        private final String URL = "URL";
    private final String USER = "root";
    private final String DRIVER = "DRIVER";
    private final String PASSWORD = "PASSWORD";

        @Bean
    DataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setUrl(environment.getProperty(URL));
        driverManagerDataSource.setUsername(environment.getProperty(USER));
        driverManagerDataSource.setPassword(environment.getProperty(PASSWORD));
        driverManagerDataSource.setDriverClassName(environment.getProperty(DRIVER));
        return driverManagerDataSource;
    }

}

I tried to use @PropertySource("classpath:database.properties") but it is syntax error that: class, interface or enum expected . Can someone help me how I should add my .properties file path with @PropertySource ?

@PropertySource is an annotation that can be used only on types, that is interface, class, enum :

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {...}

This class, interface or enum expected message is a compilation error that means that you specified the annotation on a target that doesn't match to a type.

So move it at the correct place :

@PropertySource("classpath:database.properties")
public class AppConfig {
     ....
}

You can use @PropertySource with either @Value or Environment as shown below.

Assuming this is your application property file.

app.value.example=v1
app.environment.example=e1

Using @PropertySource with @Value

@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationContig {

     @Value("${app.value.example:defaultValueCanBeHere}")
     private String propertyValue;

     public void usePropertyValue() {
        // You can use it here
     }
}

Using @PropertySource with Environment

@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationContig {

    @Autowired
    private Environment environmentValue;

    private void useEnvironmentValue() {
       String value = environmentValue.getProperty("app.environment.example");
       // You can then use it here.
     }
}

With Spring >= 4

@Configuration
@PropertySources({
    @PropertySource(value = "classpath:application.properties"),
    @PropertySource(value = "classpath:another.properties"),
    @PropertySource(value = "classpath:missing-file.properties", 
                    ignoreResourceNotFound = true)})
public class ApplicationContig {
      // You can either use @Value or Environment as demonstrated above
}

I hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM