這麼多的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的獲取和測試。


結果依然如此。其他的方式待補充。

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