Monday, November 11, 2019

Spring Boot Interview Questions - Part 2

How to enable debug logging?
To enable debug logging,

we can start the application with the --debug switch.
we can set the logging.level.root=debug property in application.properties file.
We can set the logging level of root logger in supplied logging configuration file.

How to enable HTTPS/SSL support in Spring boot?
The SSL support in spring boot project can be added via application.properties and by adding the below entries.

application.properties
server.port=8443
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=classpath:ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS

Datasource configuration

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


Spring Boot Configure and Use Two DataSources

#Database
database1.datasource.url=jdbc:mysql://localhost/testdb
database1.datasource.username=root
database1.datasource.password=root
database1.datasource.driver-class-name=com.mysql.jdbc.Driver

database2.datasource.url=jdbc:mysql://localhost/testdb2
database2.datasource.username=root
database2.datasource.password=root
database2.datasource.driver-class-name=com.mysql.jdbc.Driver
Then define them as providers (@Bean) like this:

@Bean(name = "datasource1")
@ConfigurationProperties("database1.datasource")
@Primary
public DataSource dataSource(){
    return DataSourceBuilder.create().build();
}

@Bean(name = "datasource2")
@ConfigurationProperties("database2.datasource")
public DataSource dataSource2(){
    return DataSourceBuilder.create().build();
}
Note that I have @Bean(name="datasource1") and @Bean(name="datasource2"), then you can use it when we need datasource as @Qualifier("datasource1") and @Qualifier("datasource2") , for example

@Qualifier("datasource1")
@Autowired
private DataSource dataSource;
If you do care about transaction, you have to define DataSourceTransactionManager for both of them, like this:

@Bean(name="tm1")
@Autowired
@Primary
DataSourceTransactionManager tm1(@Qualifier ("datasource1") DataSource datasource) {
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;
}

@Bean(name="tm2")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("datasource2") DataSource datasource) {
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;
}
Then you can use it like

@Transactional //this will use the first datasource because it is @primary
or

@Transactional("tm2")


How Spring Auto Configuration Works

https://dzone.com/articles/how-springboot-autoconfiguration-magic-works

How can you enable auto reload of application with Spring Boot?
You can enable auto-reload/LiveReload of spring boot application by adding the spring-boot-devtools dependency in the pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true
</dependency>
Note: please restart your application for immediate effects.

How to enable HTTP/2 support in Spring Boot?
You can enable HTTP/2 support in Spring Boot as follows:
server.http2.enabled=true


How do you Enable HTTP response compression in spring boot?
To enable HTTP response compression in spring boot using GZIP you have to add below settings in your application.properties file.

# Enabling HTTP response compression in spring boot
server.compression.enabled=true
server.compression.min-response-size=2048
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain

How do you configure error logging/debugging in Spring boot application?
You can configure error logging/debugging in Spring boot application by applying the following settings in application.properties or application.yml file.

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

logging.level.org.springframework=DEBUG
logging.level.com.demo=INFO

 How to Register a Custom Auto-Configuration?
To register an auto-configuration class, we must have its fully-qualified name listed under the EnableAutoConfiguration key in the META-INF/spring.factories file:

1
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration


How to Tell an Auto-Configuration to Back Away When a Bean Exists?
To instruct an auto-configuration class to back off when a bean is already existent, we can use the @ConditionalOnMissingBean annotation. The most noticeable attributes of this annotation are:

value: The types of beans to be checked
name: The names of beans to be checked
When placed on a method adorned with @Bean, the target type defaults to the method's return type:

@Configuration
public class CustomConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public CustomService service() { ... }
}


How to Deploy Spring Boot Web Applications as Jar and War Files?
Traditionally, we package a web application as a WAR file, then deploy it into an external server. Doing this allows us to arrange multiple applications on the same server. During the time that CPU and memory were scarce, this was a great way to save resources.


However, things have changed. Computer hardware is fairly cheap now, and the attention has turned to server configuration. A small mistake in configuring the server during deployment may lead to catastrophic consequences.

Spring tackles this problem by providing a plugin, namely spring-boot-maven-plugin, to package a web application as an executable JAR. To include this plugin, just add a plugin element to pom.xml:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
With this plugin in place, we'll get a fat JAR after executing the package phase. This JAR contains all the necessary dependencies, including an embedded server. Thus, we no longer need to worry about configuring an external server.

We can then run the application just like we would an ordinary executable JAR.

Notice that the packaging element in the pom.xml file must be set to jar to build a JAR file:

<packaging>jar</packaging>
If we don't include this element, it also defaults to jar.

In case we want to build a WAR file, change the packaging element to war:

<packaging>war</packaging>
And leave the container dependency off the packaged file:


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
After executing the Maven package phase, we'll have a deployable WAR file.

What does it mean that Spring Boot supports relaxed binding?
Relaxed binding in Spring Boot is applicable to the type-safe binding of configuration properties.

With relaxed binding, the key of an environment property doesn't need to be an exact match of a property name. Such an environment property can be written in camelCase, kebab-case, snake_case, or in uppercase with words separated by underscores.

For example, if a property in a bean class with the @ConfigurationProperties annotation is named myProp, it can be bound to any of these environment properties: myProp, my-prop, my_prop, or MY_PROP.

How to disable Actuator endpoint security in Spring Boot?
By default all sensitive HTTP endpoints are secured such that only users that have an ACTUATOR role may access them. Security is enforced using the standard HttpServletRequest.isUserInRole method.
We can disable security using -
management.security.enabled=false
It is suggested to disable security only if the actuator endpoints are accessed behind firewall.

Exception Handling

https://www.javainuse.com/spring/boot-exception-handling

For Handling Security in Actuators

management.security.enabled = true
management.security.roles = ADMIN

security.basic.enabled = true
security.user.name = admin
security.user.password = admin

No comments:

Post a Comment