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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring(四)Bean注入方试

發布時間:2025/4/9 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring(四)Bean注入方试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文出自:http://www.cnblogs.com/liunanjava/p/4401236.html

?

一、構造方法注入

定義:通過構造函數來完成依賴關系的設定

優缺點:

在構造對象的同時,完成依賴關系的建立

如果關聯的對象很多,那就不得不在構造方法上加入過多的參數

基中有index:如果指定索引從0開始,type用來指定類型

實體類:

package com.pb.entity; /*** 班級類* @author Administrator**/ public class Grade {private int id; //班級編號private String name; //班級名稱public Grade() {super();// TODO Auto-generated constructor stub}public Grade(int id, String name) {super();this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;} } package com.pb.entity; /*** 學生類* @author Administrator**/ public class Student {private String name; //學生名稱private Integer age; //學生年齡private Grade grade; //所屬班級public Student() {super();}public Student(String name, Integer age, Grade grade) {super();this.name = name;this.age = age;this.grade = grade;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Grade getGrade() {return grade;}public void setGrade(Grade grade) {this.grade = grade;} }

使用構造方法注入

applicationContext.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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--班級類 --> <bean id="grade" class="com.pb.entity.Grade"> <!-- 使用構造方法注入 --> <constructor-arg> <value>1001</value> </constructor-arg> <constructor-arg> <value>計算應用一班</value> </constructor-arg></bean> <!--學生類 --> <bean id="student" class="com.pb.entity.Student"> <!-- 使用構造方法注入 --> <constructor-arg> <value>張三</value> </constructor-arg> <constructor-arg> <value>23</value> </constructor-arg> <!-- 使用ref注入班級bean --> <constructor-arg ref="grade"> </constructor-arg> </bean>
</beans>

測試類:

package com.pb.demo;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.pb.entity.Student; public class Demo1 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Student stu = context.getBean("student", Student.class); System.out.println("學生姓名:" + stu.getName() + "學生年齡:" + stu.getAge() + "學生班級編號: " + stu.getGrade().getId() + "學生班級名稱: " + stu.getGrade().getName()); } }

?

二、屬性注入

其它不變只更改配置文件

applicationContext.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--班級類 --> <bean id="grade" class="com.pb.entity.Grade"> <property name="id"> <value>1001</value> </property> <property name="name" value="計算機應用一班"></property> </bean> <!-- 學生類 --> <bean id="student" class="com.pb.entity.Student"> <property name="name" value="張三" /> <property name="age" value="18"/> <property name="grade" ref="grade"/> </bean> </beans>

?

三、注入NULL和空字符串值

<value></value>表示空字符串

<null></null>表示為null值

<bean id="student" class="com.pb.entity.Student"> <!-- 姓名注入空字符串 <value></value>表示空字符串 --> <property name="name"><value></value></property> <!--年齡注入為NULL值 --> <property name="age"><null></null></property> <property name="grade" ref="grade"/> </bean>

?

四、使用p 命名空間注入bean

官方推薦的注入方式

需要在XML上加入

xmlns:p="http://www.springframework.org/schema/p" <?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--班級類 使用p命名空間注入 --> <bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外語一班"> </bean> <!-- 學生類 使用p命名空間注入 --> <bean id="student" class="com.pb.entity.Student" p:name="張三" p:age="23" p:grade-ref="grade"> </bean> </beans>

效果一目了然

五、自動裝配

需要使用autowire屬性來配置

可以在每個bean中使用autowire來配置

也可以在<beans>中使用autowire全局配置表示這個beans下的都使用自動裝配,

缺點:不清晰,有問題比較難以查找

autowire:

no(默認值):不進行自動裝配,必須顯示指定依賴對象。

byName: 根據屬性名自動裝配。自動查找與屬性名相同的id,如果找到,則自動注入,否則什么都不做。

byType:根據屬性的類型自動裝配,Spring自動查找與屬性類型相同的Bean,如果剛好找到唯一的那個,則自動注入,如果找到多個與屬性類型相同的Bean,則拋出異常,如果沒有找到就什么都不做。

constructor:和byType類似,不過它針對構造方法,如果找到一個Bean和構造方法的參數類型相匹配,則通過構造注入該依賴對象,如果找不到,就拋出異常。

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--班級類 使用p命名空間注入 --> <bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外語一班" > </bean> <!-- 學生類 使用p命名空間注入 --> <bean id="student" class="com.pb.entity.Student" p:name="張三" p:age="23" autowire="byName"> </bean> </beans>

自動裝配使得配置文件可以非常簡潔,但同時也造成組件之間的依賴關系不明確,容易引發一些潛在的錯誤,謹慎使用。

轉載于:https://www.cnblogs.com/vincent-blog/p/4414738.html

總結

以上是生活随笔為你收集整理的Spring(四)Bean注入方试的全部內容,希望文章能夠幫你解決所遇到的問題。

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