[email protected]和@Bean详解

[email protected]和@Bean详解

一、@Configuration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

可以看到在@Configuration注解中是包含@Component注解的,被@Configuration修饰的类被定义为一个Spring容器(应用上下文)

@Configuration就相当于Spring配置文件中的<beans />标签,里面可以配置bean

二、@Bean

@Bean相当于Spring配置文件中的<bean />标签可以在Spring容器中注入一个bean

@Configuration
public class TestConfiguration {

    @Bean
    public TestBean testBean() {
        return new TestBean();
    }

}

上述代码相当于实例化一个TestBean并交给Spring容器管理

ps:

1、@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与方法名相同

2、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为多例

三、依赖注入

@Configuration
public class TestConfiguration {

    @Bean
    public TestBean testBean() {
        return new TestBean();
    }

    @Bean
    public DIBean diBean() {
        return new DIBean(testBean());
    }  

}

如上述代码,通过在@Bean方法中调用其他@Bean注解的方法来实现依赖注入

ps:

当需要强制指定实例化bean的顺序,可以通过@Order或@DependsOn注解来实现

原文地址:https://www.cnblogs.com/javafucker/p/10148464.html

时间: 2024-07-28 14:19:24

[email protected]和@Bean详解的相关文章

$? $* [email&#160;protected] 命令的详解

一条命令创建月份的每一天 [[email protected] work]# mkdir $(echo {20150101..20150131}) [[email protected] work]# ls 20150101  20150104  20150107  20150110  20150113  20150116  20150119  20150122  20150125  20150128  20150131 20150102  20150105  20150108  20150111

Spring二 Bean详解

Bean详解 Spring框架的本质其实是:通过XML配置来驱动Java代码,这样就可以把原本由java代码管理的耦合关系,提取到XML配置文件中管理.这样就实现了系统中各组件的解耦,有利于后期的升级和维护.1.Bean的基本定义和Bean别名<beans>元素是Spring配置文件的根元素,该元素可以指定如下属性:default-lazy-init:指定<beans>元素下配置的所有bean默认的延迟初始化行为default-merge:指定<beans>元素下配置的

对private protected public的详解:

1 #include <iostream> 2 #include <stack> 3 #include <queue> 4 #include <exception> 5 #include <sstream> 6 #include <string> 7 #include <vector> 8 #include<list> 9 #include<cassert> 10 #include<cstdl

spring在IoC容器中装配Bean详解

1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean实例放入bean缓存池: 应用程序使用bean. 1.2.基于xml的配置 (1)xml文件概述 xmlns------默认命名空间 xmlns:xsi-------标准命名空间,用于指定自定义命名空间的schema文件 xmlns:xxx="aaaaa"-------自定义命名空间,xx

(转)java之Spring(IOC)注解装配Bean详解

在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看Annotation的魅力所在吧. 先来看看之前的bean注解装配例子: package com.eco.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import

[email&#160;protected] 和 @Bean

参考:http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean.最简单可行的 @Config

Spring自动装配Bean详解

1.      Auto-Wiring 'no' 2.      Auto-Wiring 'byName' 3.      Auto-Wiring 'byType 4.      Auto-Wiring 'constructor' 5.      Auto-Wiring 'autodetect' Spring Auto-Wiring Beans--Spring自动装配Bean 所谓自动装配,就是将一个Bean注入到其他Bean的Property中,类似于以下: <bean id="cust

Spring探究-----配置Bean详解

声明一个实体类Car package com.spring.configureBean; /** * 车辆类 * * @author yyx 2019年6月11日 */ public class Car { /** * 车辆名称 */ private String carName; /** * 车辆价格 */ private Integer carPrice; public Car() { super(); } public Car(String carName, Integer carPric

Spring探究-----自动装配Bean详解

1.xml配置文件(了解) 1.1 byName 按名称自动装配(推荐,需要get和set方法) 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配 <bean id="student" class="com.spring.autowire.Student" autowire="byName"> <property name="stuName" value