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

In 2018, Telegram’s audience reached 200 million people, with 500,000 new users joining the messenger every day. It was launched for iOS on 14 August 2013 and Android on 20 October 2013. Activate up to 20 bots The creator of the channel becomes its administrator by default. If you need help managing your channel, you can add more administrators from your subscriber base. You can provide each admin with limited or full rights to manage the channel. For example, you can allow an administrator to publish and edit content while withholding the right to add new subscribers. 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. “[The defendant] could not shift his criminal liability,” Hui said.
from us


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