Java for Beginner
697 subscribers
606 photos
165 videos
12 files
942 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
Что выведет код?

Задача по Spring. Тема: #Spring_Configuration_Annotations. Сложность средняя.

Подробный разбор через 30 минут!🫡

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