Что выведет код?
Задача на #Spring_Container, уровень сложный.
Решение будет опубликовано через 30 минут😉
#TasksSpring
Задача на #Spring_Container, уровень сложный.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
public class Task181024_2 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService service1 = context.getBean(MyService.class);
MyService service2 = context.getBean(MyService.class);
System.out.println(service1 == service2);
System.out.println(service1.getDependencyMessage());
}
}
class MyService {
private final Dependency dependency;
public MyService(Dependency dependency) {
this.dependency = dependency;
}
public String getDependencyMessage() {
return dependency.getMessage();
}
}
class Dependency {
private final String message;
public Dependency(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
@Configuration
class AppConfig {
@Bean
public MyService myService() {
return new MyService(dependency());
}
@Bean
@Scope("prototype")
public Dependency dependency() {
return new Dependency("Injected Dependency Message");
}
}
#TasksSpring
🔥1
Что выведет код?
Задача по Spring. Тема: #Spring_Configuration_Annotations. Сложность средняя.
Подробный разбор через 30 минут!🫡
#TasksSpring
Задача по Spring. Тема: #Spring_Configuration_Annotations. Сложность средняя.
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
public class Task211024_2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfigTask.class);
MyServiceTest myService = context.getBean(MyServiceTest.class);
myService.printMessage();
}
}
@Component
class MyServiceTest {
private final MyRepository myRepository;
private final String prefix;
public MyServiceTest(MyRepository myRepository, @Value("CustomPrefix") String prefix) {
this.myRepository = myRepository;
this.prefix = prefix;
}
public void printMessage() {
System.out.println(prefix + ": " + myRepository.getData());
}
}
@Component
class MyRepository {
public String getData() {
return "Repository Data";
}
}
@Configuration
@ComponentScan()
class AppConfigTask {}
#TasksSpring
Что выведет код?
Задача по Spring. Тема: #Spring_Bean_LifeCicle. Сложность средняя.
Подробный разбор через 30 минут!🫡
#TasksSpring
Задача по Spring. Тема: #Spring_Bean_LifeCicle. Сложность средняя.
public class Task221024_2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig221024.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();
context.close();
}
}
@Component
class MyBean implements InitializingBean, DisposableBean {
public MyBean() {
System.out.println("Constructor called");
}
@Override
public void afterPropertiesSet() {
System.out.println("afterPropertiesSet (init) called");
}
public void doSomething() {
System.out.println("Doing something");
}
@Override
public void destroy() {
System.out.println("destroy (cleanup) called");
}
}
@Configuration
@ComponentScan()
class AppConfig221024 {}
#TasksSpring
👍1
Что выведет код?
Задача по Spring. Тема: #Spring_Prototype и #Spring_Singleton. Сложность средняя.
Подробный разбор через 30 минут!🫡
#TasksSpring
Задача по Spring. Тема: #Spring_Prototype и #Spring_Singleton. Сложность средняя.
import org.springframework.context.annotation.*;
@Configuration
class Config {
@Bean
@Scope("singleton")
public MyBean2310 singletonBean() {
return new MyBean2310("singleton");
}
@Bean
@Scope("prototype")
public MyBean2310 prototypeBean() {
return new MyBean2310("prototype");
}
}
class MyBean2310 {
public MyBean2310(String scope) {
System.out.println("MyBean created for "+scope);
}
}
public class Task231024_2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
MyBean2310 singleton1 = context.getBean("singletonBean", MyBean2310.class);
MyBean2310 singleton2 = context.getBean("singletonBean", MyBean2310.class);
MyBean2310 prototype1 = context.getBean("prototypeBean", MyBean2310.class);
MyBean2310 prototype2 = context.getBean("prototypeBean", MyBean2310.class);
context.close();
}
}
#TasksSpring
👍2