java prep - part 3

mallikarjunht

Mallikarjun H T

Posted on June 18, 2024

java prep - part 3

can we disable the default behaviour of spring explain with example

Yes, in Spring Framework, you can disable or customize default behaviors by overriding configurations or using specific annotations to alter how Spring manages components and processes requests. Let's explore a couple of examples where you might want to disable default behaviors in Spring:

Example 1: Disabling Default Spring Security Configuration

By default, Spring Security applies a set of security configurations to your application, including basic authentication and authorization rules. You might want to disable these default behaviors if you have custom security requirements.

Step-by-step Example:

  1. Disable Security Auto-Configuration: To disable Spring Security's default configuration, you can exclude the SecurityAutoConfiguration class from auto-configuration in your main application class or configuration class.
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;
   import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;

   @SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
   public class MyApplication {

       public static void main(String[] args) {
           SpringApplication.run(MyApplication.class, args);
       }
   }
Enter fullscreen mode Exit fullscreen mode

In this example:

  • @SpringBootApplication is annotated with exclude = {SecurityAutoConfiguration.class}, which disables Spring Security's auto-configuration.
  1. Customize Security Configuration: After disabling default configuration, you can then customize security settings by creating your own SecurityConfig class and configuring it based on your specific requirements using @EnableWebSecurity and extending WebSecurityConfigurerAdapter.
   import org.springframework.context.annotation.Configuration;
   import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
   import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

   @Configuration
   @EnableWebSecurity
   public class SecurityConfig extends WebSecurityConfigurerAdapter {

       @Override
       protected void configure(HttpSecurity http) throws Exception {
           // Customize your security configuration here
           http
               .authorizeRequests()
                   .antMatchers("/public/**").permitAll()
                   .anyRequest().authenticated()
                   .and()
               .formLogin()
                   .loginPage("/login")
                   .permitAll()
                   .and()
               .logout()
                   .permitAll();
       }
   }
Enter fullscreen mode Exit fullscreen mode

This SecurityConfig class provides customized security configurations, overriding the default behavior that was disabled.

Example 2: Disabling Spring Boot Banner

Spring Boot displays a banner by default during application startup. You may want to disable this banner for specific reasons, such as cleaner log output.

Step-by-step Example:

  1. Disable Banner Programmatically: You can disable the banner programmatically by setting spring.main.banner-mode property to OFF in your application.properties or application.yml.
   spring.main.banner-mode=OFF
Enter fullscreen mode Exit fullscreen mode

This configuration instructs Spring Boot not to display the banner during application startup.

  1. Disable Banner via Configuration Class: Alternatively, you can disable the banner by creating a configuration class and using SpringApplication.setBannerMode(Banner.Mode.OFF) in your main application class.
   import org.springframework.boot.Banner;
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;

   @SpringBootApplication
   public class MyApplication {

       public static void main(String[] args) {
           SpringApplication app = new SpringApplication(MyApplication.class);
           app.setBannerMode(Banner.Mode.OFF);
           app.run(args);
       }
   }
Enter fullscreen mode Exit fullscreen mode

Here, SpringApplication.setBannerMode(Banner.Mode.OFF) disables the banner explicitly for the MyApplication.

Conclusion:

Spring Framework provides flexibility to disable default behaviors by excluding auto-configurations, overriding configuration classes, or configuring properties. This approach allows developers to customize Spring applications according to specific requirements, ensuring that the framework's default behaviors align with application needs.

How caching works with example/ archtecture diagram as code

Caching in software systems, including those built with Spring Boot, involves storing frequently accessed data in a faster access storage to improve performance and reduce load on backend systems.

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Cacheable(value = "usersCache", key = "#userId")
    public User getUserDetails(Long userId) {
        // Simulate fetching user details from a database
        User user = userRepository.findById(userId);
        return user;
    }
}
Enter fullscreen mode Exit fullscreen mode
# Enable caching
spring.cache.type=ehcache
Enter fullscreen mode Exit fullscreen mode

with Redis example

import java.io.Serializable;

public class Book implements Serializable {
    private String isbn;
    private String title;
    private String author;

    // Getters and setters (omitted for brevity)

    @Override
    public String toString() {
        return "Book{" +
                "isbn='" + isbn + '\'' +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
Enter fullscreen mode Exit fullscreen mode
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class BookService {

    private static final String REDIS_KEY = "books";

    @Autowired
    private RedisTemplate<String, Book> redisTemplate;

    public void saveBook(Book book) {
        redisTemplate.opsForHash().put(REDIS_KEY, book.getIsbn(), book);
    }

    public Book getBook(String isbn) {
        return (Book) redisTemplate.opsForHash().get(REDIS_KEY, isbn);
    }

    public void deleteBook(String isbn) {
        redisTemplate.opsForHash().delete(REDIS_KEY, isbn);
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
mallikarjunht
Mallikarjun H T

Posted on June 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024