Java for Beginner
679 subscribers
577 photos
158 videos
12 files
878 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
Что выведет код?

Задача по 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
Что выведет код?

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

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

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
Что выведет код?

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

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

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
Что выведет код?

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

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

import org.springframework.context.annotation.*;

@Configuration
class Config2410 {
@Bean
public MyService2410 myService2410() {
return new MyService2410();
}

@Bean
public MyBean2410 setterInjectedBean2410(MyService2410 myService) {
MyBean2410 bean = new MyBean2410();
bean.setMyService2410(myService);
return bean;
}

@Bean
public MyBean2410 constructorInjectedBean2410(MyService2410 myService) {
return new MyBean2410(myService);
}
}

class MyService2410 {
public void serve() {
System.out.println("Service called");
}
}

class MyBean2410 {
private MyService2410 myService;

public MyBean2410(MyService2410 myService) {
this.myService = myService;
System.out.println("Constructor injection");
}

public MyBean2410() {
System.out.println("No-arg constructor");
}

public void setMyService2410(MyService2410 myService) {
this.myService = myService;
System.out.println("Setter injection");
}

public void useService2410() {
myService.serve();
}
}

public class Task241024_2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config2410.class);

MyBean2410 setterBean = context.getBean("setterInjectedBean2410", MyBean2410.class);
MyBean2410 constructorBean = context.getBean("constructorInjectedBean2410", MyBean2410.class);

setterBean.useService2410();
constructorBean.useService2410();

context.close();
}
}


#TasksSpring