2017-07-08 29 views
1

我的样本项目是基于Maven的结构,下src/main/resources夹中的所有我的应用程序proeprties文件。以下是完整的示例代码。我不明白为什么我的代码无法正确查找配置文件,除非我使用@PropertySource注释。春型材org.springframework.beans.factory.NoSuchBeanDefinitionException

我的实际疑问是:我已经在application.properties文件中很好地配置了弹簧属性,但为什么它找不到配置文件及其各自的属性文件?除非我正在使用@PropertySource注释,IAM不env.getProperty获得价值(“mysql.url”)。我的意思是Environment类无法从配置文件属性文件中获取值。 为什么?下面

荫收到错误为:

Jul 08, 2017 7:54:26 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 
INFO: Refreshing org.spring[email protected]300ffa5d: startup date [Sat Jul 08 19:54:26 IST 2017]; root of context hierarchy 
helloBean 
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'datasource' available 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084) 
    at com.oreilly.datasource.Main2.main(Main2.java:15) 

DatasourceConfig.java

package com.oreilly.datasource; 

import javax.sql.DataSource; 

import org.apache.commons.dbcp.BasicDataSource; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Profile; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.annotation.PropertySources; 
import org.springframework.core.env.Environment; 

@Configuration 
/*@PropertySource("classpath:/application.properties") 
@PropertySource("classpath:/dev/application-dev.properties") 
@PropertySource("classpath:/prod/application-prod.properties")*/ 
public class DatasourceConfig { 

    @Autowired 
    private Environment env; 

    @Bean(name="helloBean") 
    public String helloWorld() { 
     System.out.println("helloBean"); 
     return "helloWorld...."; 
    } 

    @Bean(name="datasource") 
    @Profile("dev") 
    public DataSource datasourceForDev(){ 
     BasicDataSource dataSource = new BasicDataSource(); 
     System.out.println(env.getProperty("mysql.url")); 
     return dataSource; 
    } 

    @Bean(name="datasource") 
    @Profile("prod") 
    public DataSource datasourceForProd(){ 
     BasicDataSource dataSource = new BasicDataSource(); 
     System.out.println(env.getProperty("mysql.url")); 
     return dataSource; 
    } 
} 

Main2.java

package com.oreilly.datasource; 

import javax.sql.DataSource; 

import org.apache.commons.dbcp.BasicDataSource; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

public class Main2 { 


    public static void main(String[] args) { 
     AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DatasourceConfig.class); 

     DataSource dataSource = context.getBean("datasource", DataSource.class); 
     String helloBean = context.getBean("helloBean", String.class); 


    } 

} 

application.properties

spring.profiles.active=prod 
spring.config.name=application 
spring.config.location=classpath:/application.properties,classpath:/dev/application-dev.properties,classpath:/prod/application-dev.properties 

下面是该项目的文件夹结构:

enter image description here

请告诉我哪里出了问题?在相同的位置application.property

回答

0

我刚才通过如下修改解决了我的问题:

@PropertySource("classpath:/${spring.profiles.active}/application-${spring.profiles.active}.properties") 

现在我能够以皮卡application-dev.properties(或)application-prod.properties动态。

注意Environment类需要@PropertySource注释,否则我们为env.get('someproperty')获得null。

0
@Configuration 
@PropertySource("classpath:application.properties") 
public class DatasourceConfig { 
.... 
} 

put属性文件,并按照命名规则应用程序 - {}轮廓像的.properties application-dev.properties,application-prod.properties。


“我想属性文件被自动拾取基于我的个人资料在application.properties声明。”

使用-Dspring.profiles.active = dev/prod运行您的应用程序。 Spring load 1)application.property,2)应用程序-dev/prod.properties文件,其中包含来自application.property的value的值

+0

我想要根据我在application.properties中声明的配置文件自动拾取属性文件。任何想法? – John

+0

不使用@PropertySource应该如何工作,如何?我的意思是它应该自动拾取我的application-dev.properties,如何? – John

1

Spring很聪明,它选择application-x.properties(其中x是环境),具体取决于值分配到application.properties到spring.profiles.active,所以你不必担心注册在不同的@PropertySource标注的所有文件。

您可以在这里更多的信息:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

我建议你删除所有@profile注解,让只有一个数据源,这将是可变的(取决于从application.properties的seleced环境)。您可以与我把这个帖子末尾的例子明白这一点。

如果你想为一个特定的配置文件(让开发者说)定义一个mysql.url,你需要在application-dev.properties文件中添加“mysql.url”,然后设置spring.profiles。在application.properties中开发的活动值。

然后,在你DatasourceConfig.java,你可以这样执行的东西:

@Autowired 
private Environment env; 

//Takes the mysqlUrl from application-x.properties (where x is the value of spring.profiles.active that comes from application.properties) 
@Value("${mysql.url}") 
private String mysqlUrl; 

@Bean(name="helloBean")... 

@Bean(name="datasource") 
public DataSource datasource() { 
    BasicDataSource dataSource = new BasicDataSource(); 
    System.out.println(mySqlUrl); //This value is variable depending of the profile that you're pointing on. 
    return dataSource; 
} 

请让我知道这是对您有用。

+0

附加评论:您需要在资源根目录中定义application.properties和application-x.properties文件,以便Spring自动检测它们。 –

+0

我认为你应该正确理解我的查询。我们应该使用Environment类而不是像“mysql.url”那样声明每个属性变量......我使用@Profile注释来声明2个配置文件(dev和prod)使用2种方法。 。 – John

相关问题