国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

淺談Spring Bean的基本配置

瀏覽:37日期:2022-08-13 09:37:57
一、Spring中set方法的注入

User實體

@Data//lombok提供的有參構造@AllArgsConstructorlombok提供的無參構造@NoArgsConstructorpublic class User { private int id; private String name; private int age; private String sex; private String birthday;}

beanFactory.xml

<bean class='edu.xalead.User'><property name='id' value='1806'/><property name='name'> <value>張三</value></property><property name='sex' value='男'/><property name='age' value='18'/><property name='birthday' value='2000-1-1'/> </bean>1.1 set的兩種注入方法

我們在xml文件中注入的時候我們可以寫成這樣:

<property name='id' value='1806'/>

也可以寫成這樣:

<property name='id'> <value>1806</value></property>

這沒什么區別的,不過我們一般使用前者,畢竟看起來代碼少,也方便

代碼測試:

@Test public void test3(){//創建工廠BeanFactory beanFactory = new ClassPathXmlApplicationContext('beanFactory.xml');//從工廠中拿配置好的UserServlet實例User user = beanFactory.getBean(User.class);System.out.println(user); }

淺談Spring Bean的基本配置

1.2 type屬性

有事我們在注入的時候有時候會看到type:

<property name='id'> <value type='int'>1806</value></property>

這相當于一個類型聲明,聲明value是什么類型的數據,然后調用類型轉換器將我們寫入的字符串轉換為我們我們所定義的類型。但其實這是一個多余的,當我們注入的時候,會通過User對象進行反射從而知道是什么類型。

1.3 Date()類型的注入

但有一些特殊的類型是Spring所沒有的,需要我們自己去定義,就比如Date類型,如果我們這樣寫就會報錯

private Date birthday;

<property name='birthday' value='2000-1-1'/>

淺談Spring Bean的基本配置

Spring是沒有這個的轉換器,將字符串轉換為Date類型,所以其實我們可以直接用String來寫,或者new Date(),但是后者約束性太大,不能得到我們想要的日期,還是前者方便。

二、Spring_scop

當我們在測試案例中創建兩個User實例時,進行檢查發現,這兩個實例其實是一個

BeanFactory beanFactory = new ClassPathXmlApplicationContext('beanFactory.xml');//從工廠中拿配置好的UserServlet實例User user = beanFactory.getBean(User.class);User user1 = beanFactory.getBean(User.class);System.out.println(user == user1);

淺談Spring Bean的基本配置

如果我們要創建多例模式,就要使用到屬性scope

scope屬性有兩個值:

1.singleton(默認情況下,即單例模式)

2.prototype(多例模式)

<bean scope='prototype'>

我們在進行測試答案為false

三、自動注入(autowire)

我們在userServlet中注入userDao

<bean class='edu.xalead.UserDao'></bean><bean class='edu.xalead.UserServlet'> <property name='userDao' ref='userDao'/></bean>

但其實我們沒必要這樣寫,Bean中you自動注入的屬性autowire,他有兩個值:

1.byName,根據名字注入,即id=“userDao”

<bean class='edu.xalead.UserDao'></bean><bean autowire='byName'/>

2.byType,根據類型注入,類型注入比較有局限性,同種類型只能注入一個,多了會報不是唯一錯誤

<bean class='edu.xalead.UserDao'></bean><bean autowire='byType'>四、構造注入

<!-- User有個四參構造,我們通過constructor-arg一個一個對應構造參數進行值的注入 --><bean class='edu.xalead.User'> <constructor-arg value='1111'/> <constructor-arg value='zhangsan'/> <constructor-arg value='20'/> <constructor-arg value='M'/></bean>

構造注入和set注入的不同點就是,加入元素的順序必須和你所創建的實體(User)類相同,若不同,則會報錯,由于反射過來的類型和轉換器轉換的類型不同,這時候我們需要加入另一個屬性index

<!-- User有個四參構造,我們通過constructor-arg一個一個對應構造參數進行值的注入 --><bean class='edu.xalead.User'> <constructor-arg value='M' index='3'/> <constructor-arg value='zhangsan' index='1'/> <constructor-arg value='1111'/ index='0'> <constructor-arg value='20' index='2'/></bean>

那什么時候使用構造注入呢?當我們自己定義一個構造函數的時候使用構造注入

public class User { private int id; private String name; private int age; private String sex; private String birthday;//自定義構造函數 public User(int id , String name){this.id = id;this.name = name; }}

<bean class='edu.xalead.User'><constructor-arg value='18'/><constructor-arg value='張三'/> </bean>

這個時候就不能使用set注入,他會報錯,即使你寫出全參構造函數也不行

淺談Spring Bean的基本配置

五、Array注入(數組注入)

private String[] photos;

<property name='photos'><array> <value>1.jpg</value> <value>2.jpg</value> <value>3.jpg</value></array></property>六、List注入

private List<String> list;

<property name='list'><list> <value>a</value> <value>b</value> <value>c</value></list></property>七、Set注入

private Set<String> set;

<property name='set'><set> <value>a</value> <value>b</value> <value>c</value></set></property>八、Map注入

private Map<Integer,String> map

<property name='map'><map>//第一種寫法 <entry key='1' value='a'/> //第二種寫法 <entry key='2'> <value>b</value> </entry> <entry key='3' value='c'/> </map></property>九、Property注入

private Properties prop;

<property name='prop'> <props><prop key='4432'>42341231</prop><prop key='54353'>5464564</prop><prop key='9865'>2659846</prop> </props></property>

到此這篇關于淺談Spring Bean的基本配置的文章就介紹到這了,更多相關Spring Bean的配置內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 崇信县| 收藏| 盘山县| 红河县| 共和县| 将乐县| 鱼台县| 北辰区| 哈尔滨市| 怀安县| 高阳县| 大新县| 营山县| 巢湖市| 肃宁县| 海宁市| 丁青县| 信阳市| 龙里县| 和平县| 鲁甸县| 呼玛县| 金门县| 佛冈县| 星子县| 垦利县| 同心县| 泸州市| 洪雅县| 桐乡市| 临洮县| 孝感市| 固始县| 神木县| 黄平县| 荃湾区| 神农架林区| 石嘴山市| 霍邱县| 郓城县| 京山县|