简体   繁体   English

在Spring中使用批注(注入,自动装配)或getBean创建对象

[英]Create an Object in Spring with annotations (Inject, Autowired) or with getBean

I try to understand DI in spring. 我试图在春天理解DI。 Where should I use an object with context.getBean and where with @inject anntotation? 我应该在哪里将context.getBean和@inject注释一起使用?

public class App {
    public static void main(String[] args) {
        new Controller().iniController();

    }
}

public class Controller {

    public void iniController() {       

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/beans/beans.xml");
        Address address = context.getBean("address", Address.class);
        Person person = context.getBean("person", Person.class);
        Employee employee = context.getBean("employee", Employee.class);

        address.setCity("my city");
        person.setName("my name");

        System.out.println(employee);

        context.close();
    }

}

Is this correct to get address, person and employee objects with context.getBean method? 使用context.getBean方法获取地址,人员和雇员对象是否正确?


@Component
public class Employee {

    @Inject
    private Person person;
    @Inject
    private Address address;

    @Override
    public String toString() {
        return "Employee: "+ person.getName() +" from "+ address.getCity();
    }
}

Here I got person and address objects with Inject, can I also get these with getBean method? 在这里,我使用Inject获得人员和地址对象,也可以使用getBean方法获得这些对象吗?


@Component
public class Address {

    private String city;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

@Component
public class Person {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.model"></context:component-scan>
</beans>

You should always prefer dependency injection, over getBean, or should i say avoid using getBean at all.. 您应该始终比getBean更喜欢依赖项注入,或者我应该说完全避免使用getBean。

Dependency injection is very effective at associating loosely coupled components, and at configuring these components. 依赖注入在关联松耦合的组件和配置这些组件时非常有效。 Especially if the association between the components lasts throughout the lifetime of the components. 特别是如果组件之间的关联在组件的整个生命周期中持续存在。

More specifically, dependency injection is effective in these situations: 更具体地说,依赖项注入在以下情况下有效:

  1. You need to inject configuration data into one or more components. 您需要将配置数据注入一个或多个组件。
  2. You need to inject the same dependency into multiple components. 您需要将相同的依赖项注入多个组件。 You
  3. You need to inject different implementations of the same dependency. 您需要注入相同依赖项的不同实现。 You
  4. You need to inject the same implementation in different configurations. 您需要在不同的配置中注入相同的实现。
  5. You need some of the services provided by the container. 您需要容器提供的一些服务。

Defining DI helps you to easily change underlying implementations, without actually worrying about those who use this implementation.. 定义DI可帮助您轻松更改基础实现,而不必担心使用该实现的人。

And if this all still leaves you puzzled, you can find dozens of reasons for using DI on google 如果这仍然让您感到困惑,那么您可以在Google上找到使用DI的数十种原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM