Java注解中@Component和@Bean的区别

 更新时间:2024年03月08日 08:59:53   作者:李云蹊  
这篇文章主要介绍了@Component和@Bean的区别,在这给大家简单介绍下作用对象不同:@Component 注解作用于类,而 @Bean 注解作用于方法,具体实例代码参考下本文

个人回答:
1.作用对象不同:@Component 注解作用于类,而 @Bean 注解作用于方法、
2.@Component 通常是通过路径扫描来自动侦测以及自动装配到 Spring 容器中(我们可以使用 @ComponentScan 注解定义要扫描的路径从中找出标识了需要装配的类自动装配到 Spring 的 bean 容器中)。@Bean 注解通常是我们在标有该注解的方法中定义产生这个 bean,
@Bean 告诉了 Spring 这是某个类的实例,当我们需要用它的时候还给我。
3.@Bean 注解比 @Component 注解的自定义性更强,而且很多地方我们只能通过 @Bean 注解来注册 bean。比如当我们引用第三方库中的类需要装配到 Spring 容器时,只能通过 @Bean 来实现。
@bean的作用范围singleton prototype request session global session后面几个和当初servlet作用域差不多
生命周期:init-method destroy-method

注解中@Component和@Bean的区别

实体 POJO

public class Person {
    private String name;
    private Integer age;
    public Person() {
    }
    public Person(String name, Integer age) {
    this.name = name;
    this.age = age;
    }
    // 省略 getter setter
  // 后续会省略 toString 方法, 使用 IDE 自动生成就可以了
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

xml 配置方式

<bean id="person" class="me.sjl.bean.Person">
    <property name="age" value="18"/>
    <property name="name" value="sjl"/>
</bean>

测试代码

public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
    Person person = (Person) ctx.getBean("person");
    System.out.println(person);
}

测试结果

bean 创建成功

以上是我们使用传统的 xml 配置方式创建一个 bean 的方式,下面我们使用注解来创建一个 bean。

@Bean

JavaConfig 方式

@Configuration
public class BeanConfig {
    @Bean
    public Person person() {
        return new Person("SJL01", 20);
    }
}

在配置类上打个一个 @Configuration 注解,表示声明该类为 Spring 的配置类。在创建一个方法,方法返回对象即我们要创建的对象 Person , 返回该对象的实例。在方法上打上注解 @Bean即表示声明该方法返回的实例是受 Spring 管理的 Bean。

测试代码

    @Test
    public void test2() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class);
        Person person = ctx.getBean(Person.class);
        System.out.println(person);
    }

结果

使用注解方法创建

bean

成功

这里使用的是 AnnotationConfigApplicationContext() 类,传入参数就是 配置类

@Bean 注解的属性解析

value

name

name 和 value 两个属性是相同的含义的, 在代码中定义了别名。 为 bean 起一个名字,如果默认没有写该属性,那么就使用方法的名称为该 bean 的名称

autowire

装配方式 有三个选项
Autowire.NO (默认设置)
Autowire.BY_NAME
Autowire.BY_TYPE
指定 bean 的装配方式, 根据名称 和 根据类型 装配, 一般不设置,采用默认即可

initMethod

bean 的初始化方法, 直接指定方法名称即可,不用带括号

    @Test
    public void test2() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class);
        Person person = ctx.getBean(Person.class);
        System.out.println(person);
    }

Person 类添加 init() 方法

    public void init() {
        System.out.println("init ............");
    }

destroyMethod

bean 的销毁方法, 在调用 IoC 容器的 close() 方法时,会执行到该属性指定的方法。不过,只是单实例的 bean 才会调用该方法,如果是多实例的情况下,不会调用该方法

    @Bean(destroyMethod = "destroy")
    public Person person() {
        return new Person("SJL01", 20);
    }

Person 类添加 destroy() 方法

    public void destroy() {
        System.out.println(" destroy ...............");
    }

两者的目的是一样的,都是注册bean到Spring容器中
1、@Component注解表明一个类会作为组件类,并告知Spring要为这个类创建bean。
2、@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑。

区别:
1、@Component(@Controller、@Service、@Repository)通常是通过类路径扫描来自动侦测以及自动装配到Spring容器中。
2、而@Bean注解通常是我们在标有该注解的方法中定义产生这个bean的逻辑。
3、@Component 作用于类,@Bean作用于方法

Spring帮助我们管理Bean分为两个部分

一个是注册Bean(@Component , @Repository , @ Controller , @Service , @Configration),一个装配Bean(@Autowired , @Resource,可以通过byTYPE(@Autowired)、byNAME(@Resource)的方式获取Bean)。
完成这两个动作有三种方式,一种是使用自动配置的方式、一种是使用JavaConfig的方式,一种就是使用XML配置的方式。

@Compent 作用就相当于

XML配置

@Component
public class Student {
    private String name = "lkm";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

@Bean 需要在配置类中使用,即类上需要加上@Configuration注解

@Configuration
public class WebSocketConfig {
    @Bean
    public Student student(){
        return new Student();
    }
}

两者都可以通过@Autowired装配

@Autowired
Student student;

那为什么有了@Component,还需要@Bean呢?
如果你想要将第三方库中的组件装配到你的应用中,在这种情况下,是没有办法在它的类上添加@Component注解的,因此就不能使用自动化装配的方案了,但是我们可以使用@Bean,当然也可以使用XML配置。

到此这篇关于@Component和@Bean的区别的文章就介绍到这了,更多相关@Component和@Bean的区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论