这么多的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();
    }
}

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


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。看结果


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的获取和测试。


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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章