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: |

Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be: For crypto enthusiasts, there was the “gm” app, a self-described “meme app” which only allowed users to greet each other with “gm,” or “good morning,” a common acronym thrown around on Crypto Twitter and Discord. But the gm app was shut down back in September after a hacker reportedly gained access to user data. Write your hashtags in the language of your target audience. Hashtags are a fast way to find the correct information on social media. To put your content out there, be sure to add hashtags to each post. We have two intelligent tips to give you:
from us


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