1
resposta

Spring MVC II , No qualifying bean of type 'java.util.Properties' available: expected single matching bean but found 2

A solução para eu publicar no heroku e ter localmente funcionando a aplicação foi meio manual.

Parece que o bean dev properties esta conflitando com o de Prod e ainda diz que encontrou Properties e SystemProperteis.

      @Override public void onStartup(ServletContext servletContext) throws
      ServletException { 
          super.onStartup(servletContext);
          servletContext.addListener(new RequestContextListener());
          servletContext.setInitParameter("spring.profiles.active", "dev"); 
      }
@EnableTransactionManagement
@Profile("prod")
public class JPAProductionConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        return properties;
    }

    @Bean
    private DriverManagerDataSource dataSource() throws URISyntaxException {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");

        URI dbUrl = new URI(environment.getProperty("DATABASE_URL"));

        dataSource.setUrl("jdbc:postgresql://"+dbUrl.getHost()+":"+dbUrl.getPort()+dbUrl.getPath());
        dataSource.setUsername(dbUrl.getUserInfo().split(":")[0]);
        dataSource.setPassword(dbUrl.getUserInfo().split(":")[1]);

        return dataSource;
    }
}

@EnableTransactionManagement
public class JPAConfiguration {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, 
                Properties additionalProperties) {

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setDataSource(dataSource);

        factoryBean.setJpaProperties(additionalProperties);
//        factoryBean.setJpaProperties(aditionalProperties());
        //Configura o pacote de modelos para saber quais entidades o Hibernate irá gerenciar
        factoryBean.setPackagesToScan("br.com.casadocodigo.loja.models");


        return factoryBean;
    }

    @Bean
    @Profile("dev")
    public DataSource dataSource(){

         /* Configura as informações do nosso DataSource que irá ser reponsável 
          por controlar a quantidade de conexões no banco de dados, além de informar 
          qual usuário,senha, url de conexão com o banco e o driver utilizado, 
          driver este que implementas as interfaces da API do JDBC.
         */
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("fordgt90"); 
        dataSource.setUrl("jdbc:mysql://localhost:3306/casadocodigo?useTimezone=true&serverTimezone=UTC");
        //dataSource.setUrl("jdbc:mysql://localhost:3306/casadocodigo");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        //dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }


    @Bean
    @Profile("dev")
    public Properties aditionalProperties() {

         /* Configurações de propriedade do Hibernate, tipo de dialeto do banco de dados utilizado: 
          Mysql, PostGress,SQLServer, Oracle ou DB2, forma de trabalho do banco de dados, 
          update reflete cada alteração da entidade no banco de dados.
        */
        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        props.setProperty("hibernate.show_sql", "true");
        props.setProperty("hibernate.hbm2ddl.auto", "update");
        return props;
    }





    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory emf){
        return new JpaTransactionManager(emf);
    }


}
1 resposta

Oi João,

Seu Procfile está definindo -Dspring.profiles.active=prod conforme abaixo?

web: java $JAVA_OPTS -jar -Dspring.profiles.active=prod target/dependency/webapp-runner.jar --port $PORT target/*.war

Se estiver tendo problema, tenta pegar o projeto por esse link: https://cursos.alura.com.br/course/springmvc-2-integracao-cache-seguranca-e-templates/task/15967