这么多的bean,Spring是如何区分的?

简介: 万事万物都有名字,一个人可能有很多名字,比如朱元璋,可以使用朱重八来区分。对于bean来说也是一样。本文主要探究,spring中是如何区分每一个bean的。

1、XML中的name或者是id属性


第一步:创建User类


public class User {
    private String name;
    public User(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void hello() {
        System.out.println("name:" +"hello world");
    }
}

User是作为一个bean,里面定义了一个hello的方法。


第二步:创建spring.xml文件,在xml文件中配置bean


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.example.demo.beam.User">
        <constructor-arg value="愚公要移山"/>
    </bean>
</beans>

我在resource目录下面新建了一个META-INF目录用于存放spring.xml文件。这个文件中使用了id来区分不同的bean。


第三步:验证


public class Main {
    public static void main(String[] args) {
        String xmlPath = "META-INF/spring.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //从spring容器获得
        User user = (User) applicationContext.getBean("user");
        user.hello();
    }
}

在控制台就可以看到结果了。

v2-5761dc4e2d26f117d3fbd97570db9fb0_1440w.jpg

2、通过注解的方式


声明Bean的注解:


(1)@Component组件,没有明确的角色。

(2)@Service在业务逻辑层(Service层)使用。

(3)@Repository在数据访问层(dao层)使用。

(4)@Controller在展现层使用。


它们默认没有直接指定bean的name,所以bean的name是spring自动生成的。bean的name生成规则如下:


(1)class的simpleName如果大于1个字符且第二个字符是大写字母,则simpleName就是bean name,


(2)如果class的simpleName是1个字符或者第2个字符是小写,则将首字母转为小写的字符串作为bean name,


举例 "FooBah"的bean名称变成了"fooBah","X" 变成了 "x","URL" 依然是"URL"。下面通过实例演示一波:


注意:若不同的包下有两个名字相同的类,而这两个类都声明成spring的bean,这时候就会产成冲突。因为bean的名字就是bean的唯一标示,是不允许重复的。


第一步:创建UserService


public class UserService {
    public void hello() {
        System.out.println("hello world");
    }
}

第二步:创建config


@Configuration
@ComponentScan("com.example.demo.beam")
public class JavaConfig {
}

第三步:验证

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
        UserService teacherService = (UserService) context.getBean("userService");
        teacherService.hello();
        context.close();
    }
}

在验证的时候可以看到,获取bean的时候输入的名字是userService。看结果

v2-cca8c15b0465f515da33e3a280bb1ea0_1440w.jpg

3、Java配置


@Bean 注解声明的方法名是放入spring容器的bean的name。


Java配置是通过@Configuration和@Bean来实现的。


@Configuration声明当前类是一个配置类,相当于一个Spring配置的xml文件。


@Bean注解在方法上,声明当前方法的返回值为一个Bean。

案例验证:


第一步:改变JavaConfig


@Configuration
@ComponentScan("com.example.demo.beam")
public class JavaConfig {
    @Bean
    public UserService getStudentService() {
        UserService userService = new UserService();
        return userService;
    }
}

此时的bean注解到了方法上,根据上面的定义,此时的bean是返回类型UserService,因此返回的结果也是这。


第二步:基于上面的进行测试


public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
        UserService teacherService = (UserService) context.getBean("userService");
        teacherService.hello();
        context.close();
    }
}

在Main测试环境下,我们依然使用userService进行bean的获取和测试。

v2-3c22ddee0036ff09a9aa46c591b6582b_1440w.jpg

结果依然如此。其他的方式待补充。

相关文章
|
9天前
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
15 2
|
9天前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
20 3
|
2天前
|
Java Spring 容器
解读spring5源码中实例化单例bean的调用链
解读spring5源码中实例化单例bean的调用链
9 1
|
4天前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
13 2
|
9天前
|
Java 开发者 Spring
Spring 中 Bean 的生命周期
Spring 中 Bean 的生命周期
12 2
|
18小时前
|
Java Spring
Spring注解内容----用来替代Bean
Spring注解内容----用来替代Bean
|
1天前
|
Java Linux 程序员
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
|
1天前
|
Java Spring
Spring的BeanPostProcessor后置处理器与bean的生命周期
Spring的BeanPostProcessor后置处理器与bean的生命周期
|
2天前
|
Java Spring
聊聊Spring中两种创建Bean的方式:BeanDefinition.setInstanceSupplier() 和 FactoryBean
聊聊Spring中两种创建Bean的方式:BeanDefinition.setInstanceSupplier() 和 FactoryBean
5 0
|
2天前
|
Java 开发者 Spring
Spring的Bean的生命周期各个阶段扩展方法
Spring的Bean的生命周期各个阶段扩展方法
3 0