spring通過構(gòu)造函數(shù)注入實現(xiàn)方法分析
本文實例講述了spring通過構(gòu)造函數(shù)注入實現(xiàn)方法。分享給大家供大家參考,具體如下:
一 通過構(gòu)造函數(shù)注入
set注入的缺點是無法清晰表達(dá)哪些屬性是必須的,哪些是可選的,構(gòu)造注入的優(yōu)勢是通過構(gòu)造強制依賴關(guān)系,不可能實例化不完全的或無法使用的bean。
二 舉例
1 Employee
package com.hsp.constructor; public class Employee { private String name; private int age; public Employee(String name) { System.out.println("Employee(String name) 函數(shù)被調(diào)用.."); this.name = name; this.age = age; } public Employee(String name, int age) { System.out.println("Employee(String name, int age) 函數(shù)被調(diào)用.."); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2 beans.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"; xmlns:tx="http://www.springframework.org/schema/tx"; xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";> <!-- 配置一個雇員對象 --> <bean id="employee" class="com.hsp.constructor.Employee"> <!-- 通過構(gòu)造函數(shù)來注入屬性值 --> <constructor-arg index="0" type="java.lang.String" value="大明" /> </bean> </beans>
3 App1
package com.hsp.constructor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/constructor/beans.xml"); Employee ee=(Employee) ac.getBean("employee"); System.out.println(ee.getName()); } }
三 測試結(jié)果
Employee(String name) 函數(shù)被調(diào)用..
大明
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析
這篇文章主要介紹了Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析,Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null,int和integer(無論new否)比,都為true,因為會把Integer自動拆箱為int再去比,需要的朋友可以參考下2023-12-12spring?boot配置dubbo方式(properties)
這篇文章主要介紹了spring?boot配置dubbo方式(properties),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Spring的@ConfigurationProperties注解詳解
這篇文章主要介紹了Spring的@ConfigurationProperties注解詳解,@ConfigurationProperties該注解是用來獲取yml或者properties配置文件的配置信息,下面根據(jù)一些配置信息給出案例代碼進(jìn)行講解,需要的朋友可以參考下2023-11-11java基于Des對稱加密算法實現(xiàn)的加密與解密功能詳解
這篇文章主要介紹了java基于Des對稱加密算法實現(xiàn)的加密與解密功能,結(jié)合實例形式詳細(xì)分析了Des加密算法的功能、原理、使用方法與相關(guān)注意事項,需要的朋友可以參考下2017-01-01Java Socket編程簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java Socket編程簡介的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05Java多線程之synchronized關(guān)鍵字的使用
這篇文章主要介紹了Java多線程之synchronized關(guān)鍵字的使用,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04