Spring bean 四種注入方式詳解
更新時(shí)間:2021年07月16日 10:34:58 作者:寧在春
這篇文章主要介紹了Spring bean的實(shí)例化和IOC依賴注入詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
一、Set方式注入
pojo層:
/** * @Author: crush * @Date: 2021-06-17 16:57 * version 1.0 * xml 配置注入版本 set 方式 */ public class Student1 { public String name; public String school; public void setName(String name) { this.name = name; } public void setSchool(String school) { this.school = school; } @Override public String toString() { return "Student1{" + "name='" + name + '\'' + ", school='" + school + '\'' + '}'; } }
1.xml 文件
<?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"> <!--set方式注入 id是注入bean中的名字 class 是全限定類名 property 是按照set方式注入 --> <bean id="student1" class="com.crush.pojo.Student1"> <property name="name" value="wyh1"/> <property name="school" value="hngy1"/> </bean> </beans>
test測試
@Test public void student1(){ ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml"); Student1 student1 = context.getBean("student1", Student1.class); System.out.println(student1); }
二、構(gòu)造函數(shù)方式注入
pojo層
/** * @Author: crush * @Date: 2021-06-17 17:02 * version 1.0 * xml 配置 構(gòu)造函數(shù)方式注入 */ public class Student2 { private String name; private String school; public Student2(String name, String school) { this.name = name; this.school = school; } @Override public String toString() { return "Student2{" + "name='" + name + '\'' + ", school='" + school + '\'' + '}'; } }
2.xml文件
<?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"> <!--set方式注入 id是注入bean中的名字 class 是全限定類名 constructor 是按照構(gòu)造方式注入 index 是按照成員變量在構(gòu)造函數(shù)中的參數(shù)的第幾個(gè) name 表示成員變量名 type 表示類型 value 表示值 ref 表示引用 可引用另外一個(gè)注入到Spring的中的值 --> <bean id="student2" class="com.crush.pojo.Student2"> <constructor-arg index="0" name="name" type="java.lang.String" value="wyh2"/> <constructor-arg name="school" value="hngy2"/> </bean> </beans>
test測試
@Test public void student2(){ ApplicationContext context = new ClassPathXmlApplicationContext("student2.xml"); Student2 student2 = context.getBean("student2", Student2.class); System.out.println(student2); }
三、注解注入
pojo層
/** * @Author: crush * @Date: 2021-06-17 17:08 * version 1.0 */ @Component public class Student3 { @Value("wyh3") private String name; @Value("hngy3") private String school; @Override public String toString() { return "Student3{" + "name='" + name + '\'' + ", school='" + school + '\'' + '}'; } }
3.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--注解方式注入 需要掃描注解在的包 注解才會生效 --> <context:component-scan base-package="com.crush.pojo"/> </beans>
test測試
@Test public void student3(){ ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml"); Student3 student3 = context.getBean("student3", Student3.class); System.out.println(student3); }
四、JavaConfig 方式注入
pojo層
/** * @Author: crush * @Date: 2021-06-17 17:16 * version 1.0 * JavaConfig 配置 */ public class Student4 { @Value("wyh4") private String name; @Value("hngy4") private String school; @Override public String toString() { return "Student4{" + "name='" + name + '\'' + ", school='" + school + '\'' + '}'; } }
JavaConfig 類
@Configuration public class Student4Config { @Bean public Student4 student4(){ return new Student4(); } }
xml文件 掃描包
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.crush.config"/> </beans>
測試:
@Test public void student4(){ ApplicationContext context = new ClassPathXmlApplicationContext("student4.xml"); Student4 student4 = context.getBean("student4", Student4.class); System.out.println(student4); }
五、Service層注入詳解
service
/** * @Author: crush * @Date: 2021-06-17 17:27 * version 1.0 * xml 配置 */ public interface StudentService1 { void test(); }
serviceImpl
/** * @Author: crush * @Date: 2021-06-17 17:29 * version 1.0 * xml 配置 */ public class StudentService1Impl implements StudentService1{ @Override public void test() { System.out.println("===StudentDao1Impl==="); } }
xml配置文件
<?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="studentService1" class="com.crush.dao.StudentService1" /> </beans>
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望能夠您能夠關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue+springboot讀取git的markdown文件并展示功能
Markdown-it 是一個(gè)用于解析和渲染 Markdown 標(biāo)記語言的 JavaScript 庫,使用 Markdown-it,你可以將 Markdown 文本解析為 HTML 輸出,并且可以根據(jù)需要添加功能、擴(kuò)展語法或修改解析行為,本文介紹vue+springboot讀取git的markdown文件并展示,感興趣的朋友一起看看吧2024-01-01SpringBoot使用Caffeine實(shí)現(xiàn)內(nèi)存緩存示例詳解
caffeine提供了四種緩存策略:分別為手動加載、自動加載、異步手動加載、異步自動加載,這篇文章主要介紹了SpringBoot使用Caffeine實(shí)現(xiàn)內(nèi)存緩存,需要的朋友可以參考下2023-06-06Spring Security 強(qiáng)制退出指定用戶的方法
本篇文章主要介紹了Spring Security 強(qiáng)制退出指定用戶的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03SpringBoot @Autowired注入為空的情況解讀
這篇文章主要介紹了SpringBoot @Autowired注入為空的情況解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03MybatisPlus 主鍵策略之type=IdType.ASSIGN_ID等詳解
雪花算法(雪花)是微博開源的分布式ID生成算法其核心思想就是:使用一個(gè)64位的長型的數(shù)字作為全局唯一ID,這篇文章主要介紹了MybatisPlus 主鍵策略(type=IdType.ASSIGN_ID等詳解),需要的朋友可以參考下2024-04-04SpringBoot整合JDBC、Druid數(shù)據(jù)源的示例代碼
這篇文章主要介紹了SpringBoot整合JDBC、Druid數(shù)據(jù)源,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05