您好,登錄后才能下訂單哦!
這篇文章主要講解了“Spring中如何區(qū)分每一個Bean”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Spring中如何區(qū)分每一個Bean”吧!
萬事萬物都有名字,一個人可能有很多名字,比如朱元璋,可以使用朱重八來區(qū)分。對于bean來說也是一樣。本文主要探究,spring中是如何區(qū)分每一個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來區(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é)果了。圖片
聲明Bean的注解:
(1)@Component組件,沒有明確的角色。
(2)@Service在業(yè)務(wù)邏輯層(Service層)使用。
(3)@Repository在數(shù)據(jù)訪問層(dao層)使用。
(4)@Controller在展現(xiàn)層使用。
它們默認(rè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"。下面通過實(shí)例演示一波:
注意:若不同的包下有兩個名字相同的類,而這兩個類都聲明成spring的bean,這時候就會產(chǎn)成沖突。因為bean的名字就是bean的唯一標(biāo)示,是不允許重復(fù)的。
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(jié)果
@Bean 注解聲明的方法名是放入spring容器的bean的name。
Java配置是通過@Configuration和@Bean來實(shí)現(xiàn)的。
@Configuration聲明當(dāng)前類是一個配置類,相當(dāng)于一個Spring配置的xml文件。
@Bean注解在方法上,聲明當(dāng)前方法的返回值為一個Bean。
案例驗證:
@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進(jìn)行bean的獲取和測試。
結(jié)果依然如此。其他的方式待補(bǔ)充。
感謝各位的閱讀,以上就是“Spring中如何區(qū)分每一個Bean”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Spring中如何區(qū)分每一個Bean這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。