當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)
生活随笔
收集整理的這篇文章主要介紹了
Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Bean實例化的三種方式:
(1)使用類的無參構造創(chuàng)建
(2)使用靜態(tài)工廠創(chuàng)建
(3)使用實例工廠創(chuàng)建
代碼實例:
(1)項目結構:
(2)在pom.xml中導入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.使用類的無參構造函數創(chuàng)建 --><bean id="user" class="com.zwp.domain.User"></bean><!-- 2.使用靜態(tài)工廠進行創(chuàng)建 --><!-- class的值不是寫User對象的全路徑,而是寫靜態(tài)工廠的全路徑 --><!-- factory-method的值寫要調用的方法 --><bean id="user2" class="com.zwp.domain.StaticFactory" factory-method="getUser"></bean><!-- 3.使用實例工廠進行創(chuàng)建 --><!-- 需要先創(chuàng)建beanFactory對象,再通過beanFactory對象進行調用 --><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)建了一個User對象.....");} } //靜態(tài)工廠調用: public class StaticFactory {//靜態(tài)的方法,返回User對象:public static User getUser(){return new User();} } //實例工廠 public class BeanFactory {//普通的方法,返回User對象//不能通過類名調用,需要通過對象調用。public User getUser(){return new User();} }(5)測試類:
public class Test1 {@Testpublic void test(){//1.加載spring配置文件,ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//2.得到無參構造函數創(chuàng)建的對象:User user =(User) context.getBean("user");//得到靜態(tài)工廠創(chuàng)建的對象:User user2 =(User) context.getBean("user2");//得到實例工廠創(chuàng)建的對象:User user3=(User) context.getBean("user3");System.out.println("無參構造函數創(chuàng)建的對象:"+user);System.out.println("靜態(tài)工廠創(chuàng)建的對象:"+user2);System.out.println("實例工廠創(chuàng)建的對象:"+user3);} }(6)測試結果:每種方式都創(chuàng)建了一個對象
?
二、Bean的屬性注入:
1、屬性注入的三種方式:(spring里面只支持前兩種)
(1)使用有參構造注入
(2)使用set()方法注入
(3)使用接口注入
2、代碼測試:
(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.有參構造屬性注入 --><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類的代碼:
//有參構造注入: public class Book1 {private String bookname;public Book1(String bookname) {this.bookname = bookname;}public void text(){System.out.println("有參構造注入:"+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)測試類:
public class Test2 {@Testpublic void test(){//1.加載spring配置文件,ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//2.得到加載的對象Book1 book1 = (Book1) context.getBean("construct");Book2 book2 = (Book2) context.getBean("setproperty");book1.text();book2.text();} }(4)輸出結果:
?
三、對象注入:
(1)set()方法注入;
(2)構造器注入:①通過index設置參數的位置;②通過type設置參數類型;
(3)靜態(tài)工廠注入;
(4)實例工廠;
詳細內容請參考這篇文章:Spring中bean的注入方式
?
四、復雜注入:數組、list集合、map集合、properties
(1)相關類代碼:
//復雜類型屬性注入: 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"><!-- 復雜屬性的注入 --><bean id="complextype" class="com.zwp.object.ComplexType"><!-- 1.數組 --><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)測試類
public class Test2 {@Testpublic void test3(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");ComplexType complexType = (ComplexType) context.getBean("complextype");complexType.show();} }(4)運行結果:
?
附:項目結構
?
總結
以上是生活随笔為你收集整理的Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL数据库:范式
- 下一篇: Spirng使用Aspectj实现AOP