EASY_JAVA_RU Telegram 1988
🤔 Чем отличается компонент от бина?

В Spring оба термина – Bean и Component – связаны с управляемыми объектами, но у них разное предназначение.

🚩Что такое `@Component`?

@Component делает класс Spring-бином автоматически.
Spring сам создаст и зарегистрирует объект в контейнере.
import org.springframework.stereotype.Component;

@Component
public class Car {
public void drive() {
System.out.println("Машина едет...");
}
}


Как получить объект?
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);
car.drive();
}
}


🚩Что такое `@Bean`?

@Bean создаёт Bean вручную в @Configuration-классе.
Можно использовать, если нужно передать параметры или создать Bean из библиотеки.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
@Bean
public Car car() {
return new Car(); // Создаём объект вручную
}
}


Как получить Bean?
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);


Ставь 👍 и забирай 📚 Базу знаний
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4



tgoop.com/easy_java_ru/1988
Create:
Last Update:

🤔 Чем отличается компонент от бина?

В Spring оба термина – Bean и Component – связаны с управляемыми объектами, но у них разное предназначение.

🚩Что такое `@Component`?

@Component делает класс Spring-бином автоматически.
Spring сам создаст и зарегистрирует объект в контейнере.

import org.springframework.stereotype.Component;

@Component
public class Car {
public void drive() {
System.out.println("Машина едет...");
}
}


Как получить объект?
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);
car.drive();
}
}


🚩Что такое `@Bean`?

@Bean создаёт Bean вручную в @Configuration-классе.
Можно использовать, если нужно передать параметры или создать Bean из библиотеки.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
@Bean
public Car car() {
return new Car(); // Создаём объект вручную
}
}


Как получить Bean?
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);


Ставь 👍 и забирай 📚 Базу знаний

BY Java | Вопросы собесов


Share with your friend now:
tgoop.com/easy_java_ru/1988

View MORE
Open in Telegram


Telegram News

Date: |

1What is Telegram Channels? Invite up to 200 users from your contacts to join your channel SUCK Channel Telegram Judge Hui described Ng as inciting others to “commit a massacre” with three posts teaching people to make “toxic chlorine gas bombs,” target police stations, police quarters and the city’s metro stations. This offence was “rather serious,” the court said. A Telegram channel is used for various purposes, from sharing helpful content to implementing a business strategy. In addition, you can use your channel to build and improve your company image, boost your sales, make profits, enhance customer loyalty, and more.
from us


Telegram Java | Вопросы собесов
FROM American