這篇文章主要講解了“Spring中如何區分每一個Bean”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring中如何區分每一個Bean”吧!
萬事萬物都有名字,一個人可能有很多名字,比如朱元璋,可以使用朱重八來區分。對于bean來說也是一樣。本文主要探究,spring中是如何區分每一個bean的。主要是方式,其核心原理主要在bean的生命周期中去管理。
主要是通過以下三種:
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的方法。
<?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(); } }
在控制臺就可以看到結果了。圖片
聲明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的唯一標示,是不允許重復的。
public class UserService { public void hello() { System.out.println("hello world"); } }
@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果
@Bean 注解聲明的方法名是放入spring容器的bean的name。
Java配置是通過@Configuration和@Bean來實現的。
@Configuration聲明當前類是一個配置類,相當于一個Spring配置的xml文件。
@Bean注解在方法上,聲明當前方法的返回值為一個Bean。
案例驗證:
@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的獲取和測試。
結果依然如此。其他的方式待補充。
感謝各位的閱讀,以上就是“Spring中如何區分每一個Bean”的內容了,經過本文的學習后,相信大家對Spring中如何區分每一個Bean這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。