Java for Beginner
695 subscribers
603 photos
165 videos
12 files
932 links
Канал от новичков для новичков!
Изучайте Java вместе с нами!
Здесь мы обмениваемся опытом и постоянно изучаем что-то новое!

Наш YouTube канал - https://www.youtube.com/@Java_Beginner-Dev

Наш канал на RUTube - https://rutube.ru/channel/37896292/
Download Telegram
Что выведет код?

Задача на #Spring_Container, уровень сложный.

Решение будет опубликовано через 30 минут😉

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