日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot使用bean方式详解(附代码)

發布時間:2025/4/17 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot使用bean方式详解(附代码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一章節中介紹了springboot創建bean的幾種方式:注解形式(@Controller/@Service/@Component/@Repository)和@Configuration/@Bean組合注解形式;
本章節主要介紹如何在項目中使用創建的bean。
#####范例一:通過Bean類、xml配置文件創建bean并注入到容器中

//創建bean類 public class Computer {private String name;private String color;private Float price;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Float getPrice() {return price;}public void setPrice(Float price) {this.price = price;}@Overridepublic String toString() {return "Computer{" +"name='" + name + '\'' +", color='" + color + '\'' +", price=" + price +'}';} } <!--------通過applicationContext.xml配置文件實例化bean并注入Spring容器中--------> <beans><bean id="computer" class="com.java.demo.Computer"><property name="name" value="聯想"></property><property name="color" value="黑色"></property><property name="price" value="6500.45"></property></bean> </beans> public class Test{//單元測試:創建context容器并從容器中根據bean的id值獲取bean,并打印出來@Testpublic void testBean(){ApplicationContext context = new ClassPathXmlApplicaitonContext("applicationContext.xml");Computer c = context.getBean("computer",Computer.class);System.out.println(c);} }

#####范例二:通過bean類、@Configuration/@Bean組合注解實現創建bean并注入到容器中

//創建bean類,同范例一 public class Computer {private String name;private String color;private Float price;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Float getPrice() {return price;}public void setPrice(Float price) {this.price = price;}@Overridepublic String toString() {return "Computer{" +"name='" + name + '\'' +", color='" + color + '\'' +", price=" + price +'}';} } //通過該配置類的編寫實現創建bean并注入到spring容器中 @Configuration public class BeanConfig{//Bean注解若不帶name參數,則默認以方法名getComputer為bean的id,用于后續獲取bean;若帶參數則以name參數名用于后續獲取bean@Bean(name="computer")public Computer getComputer(){Computer com = new Computer();com.setName("聯想");com.setColor("紅色");com.setPrice(6500.55);return com;} } public class Test{//單元測試:創建context容器并從容器中根據bean的id值獲取bean,并打印出來@Testpublic void testBean(){ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);//Computer c = context.getBean(Computer.class)該種方式也可以從容器中獲取bean,同下Computer c = context.getBean("computer",Computer.class);System.out.println(c);} }

#####范例三:通過@Component等注解方式創建bean并注入容器,再通過@AutoWired或@Resource注解實現依賴注入,自動裝配(取出bean)。這是我們在springboot項目中最常用的使用bean的方式,前兩種使用bean的方式偏重于原理性和底層,項目中這樣使用的情況不多;話不多說,直接上代碼:

//@Service(是@Component的注解的子類)注解表示當前類對象是一個bean,在程序運行時會被spring容器掃描到并注入到容器中,這一步相當于創建bean的過程 @Service public class ServiceImpl implements Service{@OVerridepublic void print (){System.out.println("我是實現類");} } //進行單元測試,@SpringBootTest注解代表是一個單元測試類的程序入口。 @SpringBootTest public class DemoApplicationTests {//@AutoWired注解可以自動取出容器中的bean(Service接口的實現類的對象),根據類型自動裝配,此處也可使用@Resource注解@Autowiredprivate Service service;@Testpublic void testBean() {service.print();} }

#####范例四:通過@Configuration和@Bean組合注解創建bean并注入容器,再通過@AutoWired或@Resource注解實現依賴注入,自動裝配(取出bean),代碼如下:

//組合注解創建bean并注入容器,前面已經講過了,不再贅述。 @Configuration public class BeanConfig{@Beanpublic Computer getComputer(){Computer computer = new Computer();computer.setName("macBook");computer.setBrand("蘋果")computer.setColor("白色");computer.setPrice(1600050f);return computer;} } //進行單元測試,@SpringBootTest注解代表是一個單元測試類的程序入口。 @SpringBootTest public class DemoApplicationTests {//@AutoWired注解可以自動取出容器中的bean(Service接口的實現類的對象),根據類型自動裝配,此處也可使用@Resource注解@Autowiredprivate Computer computer;@Testpublic void testBean() {System.out.println(computer);} } //注意:@AutoWired、@Resource要實現自動裝配(取出bean)的前提是bean對象所屬的類要 //被spring容器掃描到,如果掃描不到,容器中就不會注入bean,更不用說取出bean了,所以主 //程序入口要放到最外層,這樣才能掃描到它的平級及子級中的被注解修飾的類;單元測試中,也 //要保證測試程序的主類能掃描到所要測試的被注解修飾的類。

原博文鏈接:http://www.54gwz.cn/article/1591256016

總結

以上是生活随笔為你收集整理的Springboot使用bean方式详解(附代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。