Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro ao acessar URL heroku

Consegui colocar a aplicação no heroku, porem ao acessa-la recebo um erro no console do heroku. heroku logs --tail

2018-08-01T14:06:19.150157+00:00 heroku[web.1]: Starting process with command `java $JAVA_OPTS -jar -Dspring.profiles.active=prod target/dependency/webapp-runner.jar --port 19688 target/*.war`
2018-08-01T14:06:21.624368+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-08-01T14:06:21.627979+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -Dfile.encoding=UTF-8
2018-08-01T14:06:22.617887+00:00 app[web.1]: Expanding casadocodigo.war into /app/target/tomcat.19688/webapps/expanded
2018-08-01T14:06:22.617958+00:00 app[web.1]: Adding Context  for /app/target/tomcat.19688/webapps/expanded
2018-08-01T14:06:23.365392+00:00 app[web.1]: Aug 01, 2018 2:06:23 PM org.apache.coyote.AbstractProtocol init
2018-08-01T14:06:23.365413+00:00 app[web.1]: INFO: Initializing ProtocolHandler ["http-nio-19688"]
2018-08-01T14:06:23.399515+00:00 app[web.1]: Aug 01, 2018 2:06:23 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
2018-08-01T14:06:23.399519+00:00 app[web.1]: INFO: Using a shared selector for servlet write/read
2018-08-01T14:06:23.412200+00:00 app[web.1]: Aug 01, 2018 2:06:23 PM org.apache.catalina.core.StandardService startInternal
2018-08-01T14:06:23.412204+00:00 app[web.1]: INFO: Starting service [Tomcat]
2018-08-01T14:06:23.413397+00:00 app[web.1]: Aug 01, 2018 2:06:23 PM org.apache.catalina.core.StandardEngine startInternal
2018-08-01T14:06:23.413400+00:00 app[web.1]: INFO: Starting Servlet Engine: Apache Tomcat/9.0.8
2018-08-01T14:06:23.801276+00:00 app[web.1]: Aug 01, 2018 2:06:23 PM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
2018-08-01T14:06:23.801290+00:00 app[web.1]: INFO: No global web.xml found
2018-08-01T14:06:23.938240+00:00 heroku[web.1]: State changed from starting to up
2018-08-01T14:06:31.908872+00:00 app[web.1]: Aug 01, 2018 2:06:31 PM org.apache.jasper.servlet.TldScanner scanJars
2018-08-01T14:06:31.908898+00:00 app[web.1]: INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2018-08-01T14:06:32.083848+00:00 app[web.1]: Aug 01, 2018 2:06:32 PM org.apache.catalina.core.ApplicationContext log
2018-08-01T14:06:32.083853+00:00 app[web.1]: INFO: 2 Spring WebApplicationInitializers detected on classpath
2018-08-01T14:06:32.437975+00:00 app[web.1]: Aug 01, 2018 2:06:32 PM org.apache.catalina.core.ApplicationContext log
2018-08-01T14:06:32.437989+00:00 app[web.1]: INFO: Initializing Spring root WebApplicationContext
2018-08-01T14:06:32.439500+00:00 app[web.1]: log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
2018-08-01T14:06:32.439582+00:00 app[web.1]: log4j:WARN Please initialize the log4j system properly.
2018-08-01T14:06:32.439658+00:00 app[web.1]: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2018-08-01T14:06:34.410697+00:00 app[web.1]: Aug 01, 2018 2:06:34 PM org.apache.catalina.core.StandardContext listenerStart
2018-08-01T14:06:34.410708+00:00 app[web.1]: SEVERE: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
2018-08-01T14:06:34.410715+00:00 app[web.1]: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in br.com.casadocodigo.loja.conf.JPAConfiguration: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'java.util.Properties' available: expected single matching bean but found 2: aditionalProperties,systemProperties
2018-08-01T14:06:34.410717+00:00 app[web.1]: at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
2018-08-01T14:06:34.410795+00:00 app[web.1]: Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'java.util.Properties' available: expected single matching bean but found 2: aditionalProperties,systemProperties
2018-08-01T14:06:34.410797+00:00 app[web.1]: at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173)
2018-08-01T14:06:34.410798+00:00 app[web.1]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
...
SEVERE: Context [] startup failed due to previous errors
2 respostas

JPAProductionConfiguration

@Profile("prod")
public class JPAProductionConfiguration {
    @Autowired
    // org.springframework.core.env.Environment
    private Environment environment;

    @Bean
    public DataSource 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;
    }
    @Bean
    private Properties aditionalProperties() {
        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        props.setProperty("hibernate.show_sql", "true");
        props.setProperty("hibernate.hbm2ddl.auto", "update");
        return props;
    }
}

JPAConfiguration


@EnableTransactionManagement
public class JPAConfiguration {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
            Properties additionalProperties) {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setPackagesToScan("br.com.casadocodigo.loja.models");
        factoryBean.setDataSource(dataSource);

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(vendorAdapter);

        factoryBean.setJpaProperties(additionalProperties);

        return factoryBean;
    }

    @Bean
    @Profile("dev")
    public Properties additionalProperties() {
        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
    @Profile("dev")
    private DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("mySQLpassword123*");
        dataSource.setUrl("jdbc:mysql://localhost/casadocodigo?useTimezone=true&serverTimezone=UTC&useSSL=false");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }

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

Servlet


public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { SecurityConfiguration.class, AppWebConfiguration.class, JPAConfiguration.class,
                JPAProductionConfiguration.class };
    .....

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

solução!

Faltava o @EnableTransactionManagement no JPAProductionConfiguration,