国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java的Spring框架中bean的繼承與內部bean的注入

Java的Spring框架中bean的繼承與內部bean的注入

2020-03-08 15:23goldensun JAVA教程

這篇文章主要介紹了Java的Spring框架中bean的繼承與內部bean的注入,Spring框架是Java的SSH三大web開發框架之一,需要的朋友可以參考下

bean的定義繼承
bean定義可以包含很多的配置信息,包括構造函數的參數,屬性值,比如初始化方法,靜態工廠方法名等容器的具體信息。

子bean定義從父定義繼承配置數據。子的定義可以覆蓋一些值,或者根據需要添加其他。

Spring bean定義繼承無關,與Java類的繼承,但繼承的概念是一樣的。你可以定義一個父bean定義為模板和其他孩子bean可以從父bean繼承所需的配置。

當使用基于XML的配置元數據,指明一個子bean定義使用所在的當前屬性指定的父bean作為這個屬性的值。

例如:
讓我們使用Eclipse IDE,然后按照下面的步驟來創建一個Spring應用程序:

Java的Spring框架中bean的繼承與內部bean的注入

以下是我們定義的“HelloWorld”豆里面有兩個屬性message1和message2配置文件beans.xml中。下一步“helloIndia”豆已經被定義為“HelloWorld”的子bean使用parent屬性。該子bean繼承message2屬性原狀,并覆蓋message1 屬性,并引入多一個屬性message3。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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-3.0.xsd">
 
 <bean id="helloWorld" class="com.yiibai.HelloWorld">
  <property name="message1" value="Hello World!"/>
  <property name="message2" value="Hello Second World!"/>
 </bean>
 
 <bean id="helloIndia" class="com.yiibai.HelloIndia"
  parent="helloWorld">
  <property name="message1" value="Hello India!"/>
  <property name="message3" value="Namaste India!"/>
 </bean>
 
</beans>

這里是HelloWorld.java 文件的內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.yiibai;
 
public class HelloWorld {
 private String message1;
 private String message2;
 
 public void setMessage1(String message){
  this.message1 = message;
 }
 
 public void setMessage2(String message){
  this.message2 = message;
 }
 
 public void getMessage1(){
  System.out.println("World Message1 : " + message1);
 }
 
 public void getMessage2(){
  System.out.println("World Message2 : " + message2);
 }
}

這里是HelloIndia.java文件的內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.yiibai;
 
public class HelloIndia {
 private String message1;
 private String message2;
 private String message3;
 
 public void setMessage1(String message){
  this.message1 = message;
 }
 
 public void setMessage2(String message){
  this.message2 = message;
 }
 
 public void setMessage3(String message){
  this.message3 = message;
 }
 
 public void getMessage1(){
  System.out.println("India Message1 : " + message1);
 }
 
 public void getMessage2(){
  System.out.println("India Message2 : " + message2);
 }
 
 public void getMessage3(){
  System.out.println("India Message3 : " + message3);
 }
}

以下是MainApp.java文件的內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.yiibai;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainApp {
 public static void main(String[] args) {
  ApplicationContext context =
    new ClassPathXmlApplicationContext("Beans.xml");
 
  HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
 
  objA.getMessage1();
  objA.getMessage2();
 
  HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
  objB.getMessage1();
  objB.getMessage2();
  objB.getMessage3();
 }
}

創建完成源代碼和bean配置文件,讓我們運行應用程序。如果一切順利,這將打印以下信息:

?
1
2
3
4
5
World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!

如果你在這里看到,我們沒有通過message2同時創建“helloIndia”的bean,但它通過了,因為bean定義的繼承。

bean定義模板:
您可以創建可以在不會花太多功夫被其他子bean定義的bean定義模板。在定義bean定義模板,不應指定類屬性,并應與真值指定如下所示的抽象屬性:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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-3.0.xsd">
 
 <bean id="beanTeamplate" abstract="true">
  <property name="message1" value="Hello World!"/>
  <property name="message2" value="Hello Second World!"/>
  <property name="message3" value="Namaste India!"/>
 </bean>
 
 <bean id="helloIndia" class="com.yiibai.HelloIndia"
  parent="beanTeamplate">
  <property name="message1" value="Hello India!"/>
  <property name="message3" value="Namaste India!"/>
 </bean>
 
</beans>

父bean不能被實例化它自己,因為它是不完整的,而且它也明確地標記為抽象。當一個定義是抽象的這個樣子,它只是作為一個純粹的模板bean定義,充當子定義的父定義使用。

注入內部bean
正如你所知道的Java內部類是其他類的范圍內定義的,同樣,內部bean是被其他bean的范圍內定義的bean。因此<property/>或<constructor-arg/>元素內<bean/>元件被稱為內部bean和它如下所示。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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-3.0.xsd">
 
 <bean id="outerBean" class="...">
  <property name="target">
   <bean id="innerBean" class="..."/>
  </property>
 </bean>
 
</beans>

例如:
我們使用Eclipse IDE,然后創建一個Spring應用程序,

這里是TextEditor.java文件的內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.yiibai;
 
public class TextEditor {
 private SpellChecker spellChecker;
 
 // a setter method to inject the dependency.
 public void setSpellChecker(SpellChecker spellChecker) {
  System.out.println("Inside setSpellChecker." );
  this.spellChecker = spellChecker;
 }
 // a getter method to return spellChecker
 public SpellChecker getSpellChecker() {
  return spellChecker;
 }
 
 public void spellCheck() {
  spellChecker.checkSpelling();
 }
}

下面是另外一個相關的類文件SpellChecker.java內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.yiibai;
 
public class SpellChecker {
 public SpellChecker(){
  System.out.println("Inside SpellChecker constructor." );
 }
 
 public void checkSpelling(){
  System.out.println("Inside checkSpelling." );
 }
 
}

以下是MainApp.java文件的內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.yiibai;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainApp {
 public static void main(String[] args) {
  ApplicationContext context =
    new ClassPathXmlApplicationContext("Beans.xml");
 
  TextEditor te = (TextEditor) context.getBean("textEditor");
 
  te.spellCheck();
 }
}

以下是配置文件beans.xml文件里面有配置為基于setter 注入,但使用內部bean:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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-3.0.xsd">
 
 <!-- Definition for textEditor bean using inner bean -->
 <bean id="textEditor" class="com.yiibai.TextEditor">
  <property name="spellChecker">
   <bean id="spellChecker" class="com.yiibai.SpellChecker"/>
  </property>
 </bean>
 
</beans>

創建源代碼和bean配置文件來完成,讓我們運行應用程序。如果一切順利,這將打印以下信息:

?
1
2
3
Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美在线视频一区 | 亚洲成人久久久 | 亚洲精品成人av | 国产成人高清 | www.888www看片| 四虎最新网站 | 91精品一区二区 | 狠狠爱天天操 | 懂色aⅴ精品一区二区三区蜜月 | 国产亚洲精品美女久久久久久久久久 | 麻豆网址 | 日韩成人中文字幕 | 日本不卡高字幕在线2019 | av看片网| 亚洲精品久久久久久一区二区 | 亚洲一区二区三区四区五区中文 | 免费成人av在线 | 在线a∨| 在线视频一区二区 | 一区二区三区自拍 | 国产精品成人国产乱一区 | 亚洲大奶网 | 亚洲视频在线免费观看 | 国产探花在线精品一区二区 | 成人欧美一区二区三区视频xxx | 色毛片| 亚洲精品国精品久久99热 | 色狠狠网 | 欧美一区高清 | 人人澡人人射 | 亚洲国产精品99久久久久久久久 | 久久久久久国产精品 | 龙珠z中文版普通话 | 一区二区三区免费在线观看 | 久久99久久99 | 黄色a级| 毛片在线视频 | 免费黄色在线观看 | 精品一级| 国产日韩欧美综合 | 欧美日韩精品在线观看 |