Что выведет код?
Задача по aннотации @Transactional, аннотациям @Cacheable и @CacheEvict, симуляция кеша в Spring. Сложность легкая.
Подробный разбор через 30 минут!🫡
#TasksSpring
Задача по aннотации @Transactional, аннотациям @Cacheable и @CacheEvict, симуляция кеша в Spring. Сложность легкая.
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableCaching
public class Task291124_2 {
public static void main(String[] args) {
SpringApplication.run(Task291124_2.class, args);
}
@Bean
public CommandLineRunner demo(UserService2911 userService) {
return args -> {
System.out.println(userService.getUserById(1L));
System.out.println(userService.getUserById(1L));
userService.clearCache();
System.out.println(userService.getUserById(1L));
};
}
}
@Service
class UserService2911 {
private final Map<Long, String> database = new HashMap<>();
public UserService2911() {
database.put(1L, "Alice");
}
@Cacheable("users")
public String getUserById(Long id) {
System.out.println("Fetching user from database...");
return database.get(id);
}
@CacheEvict(value = "users", allEntries = true)
public void clearCache() {
System.out.println("Cache cleared");
}
@Transactional
public void updateUser(Long id, String newName) {
database.put(id, newName);
if (newName == null) {
throw new IllegalArgumentException("Name cannot be null");
}
}
}
#TasksSpring
Что выведет код?
Задача по основам AOP в Spring, понятиям Joinpoint, Pointcut, Advice, аннотациям @Before, @After, @Around. Сложность легкая.
Подробный разбор через 30 минут!🫡
#TasksSpring
Задача по основам AOP в Spring, понятиям Joinpoint, Pointcut, Advice, аннотациям @Before, @After, @Around. Сложность легкая.
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.ProceedingJoinPoint;
@SpringBootApplication
@EnableAspectJAutoProxy
public class Task041224_2 {
public static void main(String[] args) {
SpringApplication.run(Task041224_2.class, args);
}
@Bean
public CommandLineRunner demo(Service0412 service) {
return args -> {
service.performTask();
};
}
}
@Component
class Service0412 {
public void performTask() {
System.out.println("Executing performTask...");
}
}
@Aspect
@Component
class LoggingAspect0412 {
@Before("execution(* Service0412.performTask(..))")
public void logBefore() {
System.out.println("Before advice");
}
@After("execution(* Service0412.performTask(..))")
public void logAfter() {
System.out.println("After advice");
}
@Around("execution(* Service0412.performTask(..))")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Around: Before");
Object result = joinPoint.proceed();
System.out.println("Around: After");
return result;
}
}
#TasksSpring