這么多的Bean,Spring是如何區(qū)分的?
萬事萬物都有名字,一個人可能有很多名字,比如朱元璋,可以使用朱重八來區(qū)分。對于bean來說也是一樣。本文主要探究,spring中是如何區(qū)分每一個bean的。主要是方式,其核心原理主要在bean的生命周期中去管理。
主要是通過以下三種:
1、XML中的name或者是id屬性
第一步:創(chuàng)建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的方法。
第二步:創(chuàng)建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來區(qū)分不同的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();
- }
- }
在控制臺就可以看到結(jié)果了。圖片
2、通過注解的方式
聲明Bean的注解:
(1)@Component組件,沒有明確的角色。
(2)@Service在業(yè)務邏輯層(Service層)使用。
(3)@Repository在數(shù)據(jù)訪問層(dao層)使用。
(4)@Controller在展現(xiàn)層使用。
它們默認沒有直接指定bean的name,所以bean的name是spring自動生成的。bean的name生成規(guī)則如下:
(1)class的simpleName如果大于1個字符且第二個字符是大寫字母,則simpleName就是bean name,
(2)如果class的simpleName是1個字符或者第2個字符是小寫,則將首字母轉(zhuǎn)為小寫的字符串作為bean name,
舉例 "FooBah"的bean名稱變成了"fooBah","X" 變成了 "x","URL" 依然是"URL"。下面通過實例演示一波:
注意:若不同的包下有兩個名字相同的類,而這兩個類都聲明成spring的bean,這時候就會產(chǎn)成沖突。因為bean的名字就是bean的唯一標示,是不允許重復的。
第一步:創(chuàng)建UserService
- public class UserService {
- public void hello() {
- System.out.println("hello world");
- }
- }
第二步:創(chuàng)建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??唇Y(jié)果
3、Java配置
@Bean 注解聲明的方法名是放入spring容器的bean的name。
Java配置是通過@Configuration和@Bean來實現(xiàn)的。
@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注解到了方法上,根據(jù)上面的定義,此時的bean是返回類型UserService,因此返回的結(jié)果也是這。
第二步:基于上面的進行測試
- 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測試環(huán)境下,我們依然使用userService進行bean的獲取和測試。
結(jié)果依然如此。其他的方式待補充。
本文轉(zhuǎn)載自微信公眾號「愚公要移山」,可以通過以下二維碼關注。轉(zhuǎn)載本文請聯(lián)系愚公要移山公眾號。