EASY_JAVA_RU Telegram 1974
🤔 Чем отличается обычный объект от Bean?

В Spring термин Bean (бин) означает объект, управляемый Spring-контейнером.

🟠Обычный объект (Java POJO)
Создаётся вручную, Spring о нём ничего не знает
class Car {
void drive() {
System.out.println("Машина едет...");
}
}

public class Main {
public static void main(String[] args) {
Car car = new Car(); // Создаём объект вручную
car.drive();
}
}


🟠Spring Bean (управляемый объект)
Spring создаёт и управляет бином через аннотации.
import org.springframework.stereotype.Component;

@Component // Сообщает Spring, что этот класс - Bean
class Car {
void drive() {
System.out.println("Spring-машина едет...");
}
}


Теперь объект создаётся Spring-контейнером
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); // Получаем Bean из Spring-контейнера
car.drive();
}
}


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



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

🤔 Чем отличается обычный объект от Bean?

В Spring термин Bean (бин) означает объект, управляемый Spring-контейнером.

🟠Обычный объект (Java POJO)
Создаётся вручную, Spring о нём ничего не знает

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

public class Main {
public static void main(String[] args) {
Car car = new Car(); // Создаём объект вручную
car.drive();
}
}


🟠Spring Bean (управляемый объект)
Spring создаёт и управляет бином через аннотации.
import org.springframework.stereotype.Component;

@Component // Сообщает Spring, что этот класс - Bean
class Car {
void drive() {
System.out.println("Spring-машина едет...");
}
}


Теперь объект создаётся Spring-контейнером
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); // Получаем Bean из Spring-контейнера
car.drive();
}
}


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

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


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

View MORE
Open in Telegram


Telegram News

Date: |

Telegram Android app: Open the chats list, click the menu icon and select “New Channel.” Ng was convicted in April for conspiracy to incite a riot, public nuisance, arson, criminal damage, manufacturing of explosives, administering poison and wounding with intent to do grievous bodily harm between October 2019 and June 2020. Select: Settings – Manage Channel – Administrators – Add administrator. From your list of subscribers, select the correct user. A new window will appear on the screen. Check the rights you’re willing to give to your administrator. Informative 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