sping——基於xml配置Bean
阿新 • • 發佈:2019-01-05
spring支援三種依賴注入方式:
1.屬性注入
2.構造器注入
3.工廠方式注入(很少用)
一. 首先介紹一下spring所需的幾個jar檔案:
或者使用maven進行管理:
pom檔案如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wj.spring01</groupId> <artifactId>spring01</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies> </project>
目錄結構:
二. 基於set方法的注入方式:
建立User實體:
注意要有 屬性對應的set方法,以及一個無參的構造方法
編寫xml檔案:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置一個 bean --> <bean id="user" class="com.wj.spring.entity.User"> <property name="userNam" value="bxklk"></property> <property name="age" value="26"></property> </bean> </beans>
<bean> 標籤對應的是需要進行注入的類,id是bean的名字,class 是該類的全類名,通過反射機制 注入
property對應的是類的屬性,以及需要賦的值。
編寫main方法:
package com.wj.spring.entity; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by 小美女 on 2018/11/16. */ public class Main { public static void main(String[] args){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); User user = (User) ctx.getBean("user"); System.out.println(user); } }
ClassPathXmlApplicationContext:從 類路徑下載入配置檔案。
getBean();通過對應的bean ID 查詢要注入的類
輸出結果:
注:
1.也可以不通過Id而使用類名進行注入,如上圖
2.如果 User 裡面包含另一個物件屬性 Car
修改User類,加一個物件
在bean.xml中新增<bean id="car">,並且在<bean id="user">中加入相應的屬性 如下:
<!-- 配置一個 bean -->
<bean id="user" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 通過 ref 屬性值指定當前屬性指向哪一個 bean! -->
<property name="car" ref="car"></property>
</bean>
<bean id="car" class="com.wj.spring.entity.Car">
<property name="carNam" value="baoma"></property>
<property name="carSize" value="25"></property>
</bean>
這裡也可以不單獨配置car的bean ,而使用宣告內部bean的方式進行注入
<!-- 配置一個 bean -->
<bean id="user" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 內部 bean, 類似於匿名內部類物件. 不能被外部的 bean 來引用, 也沒有必要設定 id 屬性 -->
<property name="car">
<bean class="com.wj.spring.entity.Car">
<property name="carNam" value="benchi"></property>
<property name="carSize" value="20"></property>
</bean>
</property>
<!--<!– 通過 ref 屬性值指定當前屬性指向哪一個 bean! –>-->
<!--<property name="car" ref="car"></property>-->
</bean>
4.集合屬性裝配
<bean id="user2" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 通過 ref 屬性值指定當前屬性指向哪一個 bean! -->
<property name="car" ref="car"></property>
<property name="cars">
<list>
<ref bean="car"></ref>
<ref bean="car2"></ref>
</list>
</property>
</bean>
此處還可以單獨宣告list集合bean
<bean id="user2" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 通過 ref 屬性值指定當前屬性指向哪一個 bean! -->
<property name="car" ref="car"></property>
<!-- 宣告集合型別的 bean -->
<property name="cars" ref="cars">
</property>
</bean>
<!-- 宣告集合型別的 bean -->
<util:list id="cars">
<ref bean="car"></ref>
<ref bean="car2"></ref>
</util:list>
5.使用p進行快速配置
<bean id="user3" class="com.wj.spring.entity.User"
p:cars-ref="cars" p:userNam="Titannic"></bean>
以上簡單介紹了屬性配置的幾個方面
三 . 構造器注入
進行構造器注入一定要有帶引數的建構函式
若一個 bean 有多個構造器, 如何通過構造器來為 bean 的屬性賦值
可以根據 index 和 type進行更加精確的定位.
若字面值中包含特殊字元, 則可以使用 DCDATA 來進行賦值.
注:構造器注入只是簡單介紹了一下,關於spring的bean配置方式還涉及級聯操作以及有關map型別資料的注入,感興趣的朋友可以查閱一下