آماده سازی جاوا – قسمت 3
آیا میتوانیم رفتار پیشفرض فنر را با مثال توضیح دهیم غیرفعال کنیم
بله، در Spring Framework، میتوانید رفتارهای پیشفرض را با لغو تنظیمات یا استفاده از حاشیهنویسی خاص برای تغییر نحوه مدیریت اجزا و پردازش درخواستها، غیرفعال یا سفارشی کنید. بیایید چند مثال را بررسی کنیم که در آن ممکن است بخواهید رفتارهای پیشفرض را در Spring غیرفعال کنید:
مثال 1: غیرفعال کردن تنظیمات امنیتی بهار پیش فرض
بهطور پیشفرض، Spring Security مجموعهای از تنظیمات امنیتی را برای برنامه شما اعمال میکند، از جمله قوانین اساسی احراز هویت و مجوز. اگر الزامات امنیتی سفارشی دارید، ممکن است بخواهید این رفتارهای پیشفرض را غیرفعال کنید.
مثال گام به گام:
-
غیرفعال کردن تنظیمات خودکار امنیتی: برای غیرفعال کردن پیکربندی پیشفرض Spring Security، میتوانید آن را حذف کنید
SecurityAutoConfiguration
کلاس از پیکربندی خودکار در کلاس برنامه اصلی یا کلاس پیکربندی.
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);
}
}
در این مثال:
-
@SpringBootApplication
با حاشیه نویسی شده استexclude = {SecurityAutoConfiguration.class}
، که پیکربندی خودکار Spring Security را غیرفعال می کند.
-
پیکربندی امنیتی را سفارشی کنید: پس از غیرفعال کردن پیکربندی پیشفرض، میتوانید تنظیمات امنیتی را با ایجاد تنظیمات خود سفارشی کنید
SecurityConfig
کلاس و پیکربندی آن بر اساس نیازهای خاص خود با استفاده از@EnableWebSecurity
و گسترش دادن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();
}
}
این SecurityConfig
class پیکربندیهای امنیتی سفارشیسازی شده را فراهم میکند و رفتار پیشفرض غیرفعال شده را نادیده میگیرد.
مثال 2: غیرفعال کردن بنر بوت بهار
Spring Boot یک بنر را به طور پیش فرض در هنگام راه اندازی برنامه نمایش می دهد. ممکن است بخواهید این بنر را به دلایل خاصی غیرفعال کنید، مانند خروجی گزارش پاکتر.
مثال گام به گام:
-
غیرفعال کردن بنر به صورت برنامه ریزی شده: با تنظیم می توانید بنر را به صورت برنامه نویسی غیرفعال کنید
spring.main.banner-mode
دارایی بهOFF
در شماapplication.properties
یاapplication.yml
.
spring.main.banner-mode=OFF
این پیکربندی به Spring Boot دستور می دهد که بنر را در هنگام راه اندازی برنامه نمایش ندهد.
-
غیرفعال کردن بنر از طریق کلاس پیکربندی: همچنین می توانید با ایجاد یک کلاس پیکربندی و استفاده از بنر را غیرفعال کنید
SpringApplication.setBannerMode(Banner.Mode.OFF)
در کلاس برنامه اصلی شما
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);
}
}
اینجا، SpringApplication.setBannerMode(Banner.Mode.OFF)
بنر را به صراحت برای MyApplication
.
نتیجه:
Spring Framework برای غیرفعال کردن رفتارهای پیشفرض با حذف تنظیمات خودکار، نادیده گرفتن کلاسهای پیکربندی یا پیکربندی ویژگیها، انعطافپذیری را فراهم میکند. این رویکرد به توسعهدهندگان اجازه میدهد تا برنامههای Spring را بر اساس نیازهای خاص سفارشی کنند و اطمینان حاصل شود که رفتارهای پیشفرض چارچوب با نیازهای برنامهها همسو هستند.
نحوه کار کش کردن با مثال / نمودار معماری به عنوان کد
حافظه پنهان در سیستمهای نرمافزاری، از جمله آنهایی که با Spring Boot ساخته شدهاند، شامل ذخیره دادههای با دسترسی مکرر در یک ذخیرهسازی دسترسی سریعتر برای بهبود عملکرد و کاهش بار روی سیستمهای باطن است.
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;
}
}
# Enable caching
spring.cache.type=ehcache
با مثال ردیس
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 + "\'' +
'}';
}
}
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);
}
}