從頭認識Spring 1 9 內部類注入Bean

2022-07-30 04:30:23 字數 1747 閱讀 7086

這一章節我們來討論一下內部類注入bean。

1.domain

蛋糕類:(跟前一章節的一樣)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9;

public class cake

public void setname(string name)

public double getsize()

public void setsize(double size)

public int getid()

@override

public string tostring()

}

廚師類:(基本沒有變化)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9;

public class chief

private string name = "";

public chief(string name)

public chief(string name, cake cake)

public string getname()

public void setname(string name)

public void setcake(cake cake)

private final int id = index++;

public int getid()

private static int index = 0;

public cake makeonecake()

}

測試類:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.test.context.contextconfiguration;

import org.springframework.test.context.junit4.springjunit4classrunner;

@runwith(springjunit4classrunner.class)

@contextconfiguration(locations =

}

2.配置檔案

以下是通過屬性注入內部類的配置檔案

<?xml version="1.0" encoding="utf-8"?>

注意:我們內部類注入,配置檔案中面的cake是不帶id的,並且。他不能為其它的bean所公用。

以下是通過構造器注入內部類的配置檔案:

<?

xml version="1.0" encoding="utf-8"?>

大家須要注意的是。我們上面的配置檔案配置bean的時候配置了兩個屬性,因此我們的構造器裡面,必須存在這兩個屬性值,不然就會拋異常。

總結:這一章節主要介紹內部類注入bean的形式和注意點。

資料夾:

我的github:

從頭認識java 8 6 匿名內部類

這一章節我們來討論一下匿名內部類。我們之前已經提到了內部類,如下 package com.ray.ch03 public class test private mytest mytest public static void main string args inte ce mytest 上面的內部...

從頭認識java 8 4 內部類與向上轉型

這一章節我們來討論一下內部類與向上轉型。跟普通的類一樣,內部類也可以實現某個介面然後向上轉型。為什麼?因為這樣能夠更好的隱藏實現的細節,基本其他程式設計師使用繼承來擴充套件介面和方法都不能訪問相關實現。package com.ray.ch03 public class test public per...

從頭認識java 8 2 鏈結到外部類

這一章節我們來討論一下內部類鏈結到外部類的幾個方面。package com.ray.ch08 public class test public void run class destination private void say private class content 上面的例子充分展現了內部...