Override default Spring-Boot application.properties settings in Junit Test

You can use @TestPropertySource to override values in application.properties. From its javadoc: test property sources can be used to selectively override properties defined in system and application property sources For example: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = ExampleApplication.class) @TestPropertySource(locations=”classpath:test.properties”) public class ExampleApplicationTests { }

Spring Boot – Loading Initial Data

You can create a data.sql file in your src/main/resources folder and it will be automatically executed on startup. In this file you can add some insert statements, eg.: INSERT INTO users (username, firstname, lastname) VALUES (‘lala’, ‘lala’, ‘lala’), (‘lolo’, ‘lolo’, ‘lolo’); Similarly, you can create a schema.sql file (or schema-h2.sql) as well to create your … Read more

Difference between Spring MVC and Spring Boot [closed]

Spring MVC is a complete HTTP oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack. The most popular elements in it are classes annotated with @Controller, where you implement methods you can access using different HTTP requests. It has an equivalent @RestController … Read more

How can I add a filter class in Spring Boot?

If you want to setup a third-party filter you can use FilterRegistrationBean. For example, the equivalent of web.xml: <filter> <filter-name>SomeFilter</filter-name> <filter-class>com.somecompany.SomeFilter</filter-class> </filter> <filter-mapping> <filter-name>SomeFilter</filter-name> <url-pattern>/url/*</url-pattern> <init-param> <param-name>paramName</param-name> <param-value>paramValue</param-value> </init-param> </filter-mapping> These will be the two beans in your @Configuration file: @Bean public FilterRegistrationBean someFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(someFilter()); registration.addUrlPatterns(“/url/*”); registration.addInitParameter(“paramName”, “paramValue”); registration.setName(“someFilter”); … Read more

Setting active profile and config location from command line in spring boot

There are two different ways you can add/override spring properties on the command line. Option 1: Java System Properties (VM Arguments) It’s important that the -D parameters are before your application.jar otherwise they are not recognized. java -jar -Dspring.profiles.active=prod application.jar Option 2: Program arguments java -jar application.jar –spring.profiles.active=prod –spring.config.location=c:\config

Spring Boot configure and use two data sources

Here you go. Add in your application.properties file: #first db spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver #second db … spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver Add in any class annotated with @Configuration the following methods: @Bean @Primary @ConfigurationProperties(prefix=”spring.datasource”) public DataSource primaryDataSource() { return … Read more

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set

First remove all of your configuration Spring Boot will start it for you. Make sure you have an application.properties in your classpath and add the following properties. spring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=LATIN1 spring.datasource.username=klebermo spring.datasource.password=123 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=create If you really need access to a SessionFactory and that is basically for the same datasource, then you can do the following … Read more

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

I have just asked the Spring Cloud guys and thought I should share the info I have here. bootstrap.yml is loaded before application.yml. It is typically used for the following: when using Spring Cloud Config Server, you should specify spring.application.name and spring.cloud.config.server.git.uri inside bootstrap.yml some encryption/decryption information Technically, bootstrap.yml is loaded by a parent Spring … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)