如何在Java Spring 4.0.4中使用@PropertySource?

时间:2018-08-29 08:44:59

标签: java spring

我想根据https://www.journaldev.com/17053/spring-jdbctemplate-exampledatabase.properties中的ProjectName/src/database.properties的源添加到AppConfig.class中的ProjectName/src/device/spring/config的源

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;
    }

}

我尝试使用@PropertySource("classpath:database.properties"),但是它是语法错误:class, interface or enum expected。有人可以帮我如何使用@PropertySource添加.properties文件路径吗?

2 个答案:

答案 0 :(得分:1)

@PropertySource是一个注释,只能用于类型,即接口,类,枚举:

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

class, interface or enum expected消息是编译错误,表示您在与类型不匹配的目标上指定了注释。

因此将其移动到正确的位置:

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

答案 1 :(得分:0)

您可以将@PropertySource与@Value或Environment一起使用,如下所示。

假设这是您的应用程序属性文件。

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

使用@PropertySource和@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
     }
}

在环境中使用@PropertySource

@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.
     }
}

使用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
}

我希望这会有所帮助。