javascript
Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)
一、Bean實(shí)例化的三種方式:
(1)使用類的無參構(gòu)造創(chuàng)建
(2)使用靜態(tài)工廠創(chuàng)建
(3)使用實(shí)例工廠創(chuàng)建
代碼實(shí)例:
(1)項(xiàng)目結(jié)構(gòu):
(2)在pom.xml中導(dǎo)入spring的核心jar包依賴:
(3)applicationContext.xml的配置:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 1.使用類的無參構(gòu)造函數(shù)創(chuàng)建 --><bean id="user" class="com.zwp.domain.User"></bean><!-- 2.使用靜態(tài)工廠進(jìn)行創(chuàng)建 --><!-- class的值不是寫User對(duì)象的全路徑,而是寫靜態(tài)工廠的全路徑 --><!-- factory-method的值寫要調(diào)用的方法 --><bean id="user2" class="com.zwp.domain.StaticFactory" factory-method="getUser"></bean><!-- 3.使用實(shí)例工廠進(jìn)行創(chuàng)建 --><!-- 需要先創(chuàng)建beanFactory對(duì)象,再通過beanFactory對(duì)象進(jìn)行調(diào)用 --><bean id="beanFactory" class="com.zwp.domain.BeanFactory"></bean><bean id="user3" factory-bean="beanFactory" factory-method="getUser"></bean> </beans>(4)domain類的代碼:
public class User {public void add(){System.out.println("創(chuàng)建了一個(gè)User對(duì)象.....");} } //靜態(tài)工廠調(diào)用: public class StaticFactory {//靜態(tài)的方法,返回User對(duì)象:public static User getUser(){return new User();} } //實(shí)例工廠 public class BeanFactory {//普通的方法,返回User對(duì)象//不能通過類名調(diào)用,需要通過對(duì)象調(diào)用。public User getUser(){return new User();} }(5)測(cè)試類:
public class Test1 {@Testpublic void test(){//1.加載spring配置文件,ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//2.得到無參構(gòu)造函數(shù)創(chuàng)建的對(duì)象:User user =(User) context.getBean("user");//得到靜態(tài)工廠創(chuàng)建的對(duì)象:User user2 =(User) context.getBean("user2");//得到實(shí)例工廠創(chuàng)建的對(duì)象:User user3=(User) context.getBean("user3");System.out.println("無參構(gòu)造函數(shù)創(chuàng)建的對(duì)象:"+user);System.out.println("靜態(tài)工廠創(chuàng)建的對(duì)象:"+user2);System.out.println("實(shí)例工廠創(chuàng)建的對(duì)象:"+user3);} }(6)測(cè)試結(jié)果:每種方式都創(chuàng)建了一個(gè)對(duì)象
?
二、Bean的屬性注入:
1、屬性注入的三種方式:(spring里面只支持前兩種)
(1)使用有參構(gòu)造注入
(2)使用set()方法注入
(3)使用接口注入
2、代碼測(cè)試:
(1)applicationContext.xml的配置
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 屬性注入的方式:start --><!-- 1.有參構(gòu)造屬性注入 --><bean id="construct" class="com.zwp.domain.Book1"><constructor-arg name="bookname" value="這是Book1的name"></constructor-arg></bean><!-- 2.set方法屬性注入 --><bean id="setproperty" class="com.zwp.domain.Book2"><property name="bookname" value="這是Book2的name"></property></bean><!-- 屬性注入的方式:end --> </beans>(2)domain類的代碼:
//有參構(gòu)造注入: public class Book1 {private String bookname;public Book1(String bookname) {this.bookname = bookname;}public void text(){System.out.println("有參構(gòu)造注入:"+bookname);} } //set方法注入屬性: public class Book2 {private String bookname;public void setBookname(String bookname) {this.bookname = bookname;}public void text(){System.out.println("set方法注入屬性:"+bookname);} }(3)測(cè)試類:
public class Test2 {@Testpublic void test(){//1.加載spring配置文件,ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//2.得到加載的對(duì)象Book1 book1 = (Book1) context.getBean("construct");Book2 book2 = (Book2) context.getBean("setproperty");book1.text();book2.text();} }(4)輸出結(jié)果:
?
三、對(duì)象注入:
(1)set()方法注入;
(2)構(gòu)造器注入:①通過index設(shè)置參數(shù)的位置;②通過type設(shè)置參數(shù)類型;
(3)靜態(tài)工廠注入;
(4)實(shí)例工廠;
詳細(xì)內(nèi)容請(qǐng)參考這篇文章:Spring中bean的注入方式
?
四、復(fù)雜注入:數(shù)組、list集合、map集合、properties
(1)相關(guān)類代碼:
//復(fù)雜類型屬性注入: public class ComplexType {//第一步:private String[] arrs;private List<String> list;private Map<String,String> map;private Properties properties;//第二步:set方法public void setArrs(String[] arrs) {this.arrs = arrs;}public void setList(List<String> list) {this.list = list;}public void setMap(Map<String, String> map) {this.map = map;}public void setProperties(Properties properties) {this.properties = properties;}//展示注入的屬性public void show(){System.out.println("arrs:"+arrs);System.out.println("list:"+list);System.out.println("map:"+map);System.out.println("properties:"+properties);} }(2)applicationContext.xml文件的配置
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 復(fù)雜屬性的注入 --><bean id="complextype" class="com.zwp.object.ComplexType"><!-- 1.數(shù)組 --><property name="arrs"><list><value>arr1</value><value>arr2</value><value>arr3</value></list></property><!-- 2.list集合 --><property name="list"><list><value>list1</value><value>list2</value><value>list3</value></list></property><!-- 3.map集合 --><property name="map"><map><entry key="1" value="map1"></entry><entry key="2" value="map2"></entry><entry key="3" value="map3"></entry></map> </property><!-- 4.properties --><property name="properties"><props><prop key="a">properties-a</prop><prop key="b">properties-b</prop><prop key="c">properties-c</prop></props></property></bean> </beans>(3)測(cè)試類
public class Test2 {@Testpublic void test3(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");ComplexType complexType = (ComplexType) context.getBean("complextype");complexType.show();} }(4)運(yùn)行結(jié)果:
?
附:項(xiàng)目結(jié)構(gòu)
?
總結(jié)
以上是生活随笔為你收集整理的Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL数据库:范式
- 下一篇: SpringMVC框架--学习笔记(上)