Spring Ioc 使用XML配置
1、首先到引入依賴包
<!--引入配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
2、建立Spring配置檔案,一般取名這三個(任意 無約定) spring-context.xml、applicationContext.xml、beans.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
</beans>
3、建立一個使用ioc建立一個物件 並且幫他設定好初始值
3.1首先建立好Class 這邊有兩個類一個是學生類 和 地址類
3.1.1學生類 Student.java 並設定他的get和set 方法要不要xml配置不了屬性
//Student.java
package com.ared.entity;
import java.util.List;
import java.util.Properties;
import java.util.Set;
public class Student {
private int id;
private String name;
private List<String> phone;
private Set<String> oldName;
private Properties info;
private Address address;
public Student(){
System.out.println("----Constructor----");
}
public void init(){
System.out.println("-----init------");
}
public void destroy(){
System.out.println("-----destroy-----");
}
public Student(int id, String name, List<String> phone, Set<String> oldName, Properties info, Address address) {
this.id = id;
this.name = name;
this.phone = phone;
this.oldName = oldName;
this.info = info;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPhone() {
return phone;
}
public void setPhone(List<String> phone) {
this.phone = phone;
}
public Set<String> getOldName() {
return oldName;
}
public void setOldName(Set<String> oldName) {
this.oldName = oldName;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", phone=" + phone +
", oldName=" + oldName +
", info=" + info +
", address=" + address +
'}';
}
}
3.1.2地址類 Address.java
//Address.java
package com.ared.entity;
public class Address {
String homeAddress;
String officeAddress;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getOfficeAddress() {
return officeAddress;
}
public void setOfficeAddress(String officeAddress) {
this.officeAddress = officeAddress;
}
@Override
public String toString() {
return "Address{" +
"homeAddress='" + homeAddress + '\'' +
", officeAddress='" + officeAddress + '\'' +
'}';
}
}
3.1.3配置Spring配置檔案 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="address" class="com.ared.entity.Address">
<property name="homeAddress" value="青山湖"/>
<property name="officeAddress" value="高新"/>
</bean>
<bean id="student" class="com.ared.entity.Student">
<property name="id" value="10001"/>
<property name="name" value="碼小農"/>
<!-- 新增一個list引數 -->
<property name="phone">
<list>
<value>13711112222</value>
<value>13711112223</value>
</list>
</property>
<!-- 新增一個set引數 -->
<property name="oldName">
<set>
<value>小明</value>
<value>小紅</value>
</set>
</property>
<!--新增一個Properties引數-->
<property name="info">
<props>
<prop key="username">admin</prop>
<prop key="password">123456</prop>
</props>
</property>
<!--如果屬性有物件的話可以先建立bean 然後使用ref 應用id 得到值-->
<property name="address" ref="address"/>
</bean>
</beans>
3.1.4寫一個測試類 IocTest.java
package com.ared.test;
import com.ared.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
}
3.1.5使用 在Bean建立時會可以指定一個 初始方法 和 銷燬方法 只需要修改Bean 標籤上加一個 init-method 和 destroy-method
<bean id="student" class="com.ared.entity.Student" init-method="init" destroy-method="destroy">
3.1.6 自動裝配 如果你的Bean中需要注入其他物件的話 你可以使用 autowire 這個標籤注入 他有兩種注入方式
1、一種是根據id名字注入
<bean id="student" class="com.ared.entity.Student" autowire="byName">
2、二種是更具型別注入
<bean id="student" class="com.ared.entity.Student" autowire="byType">
3.1.7 單例和多例 scope
1、單例模式:當xml載入到spring中 只要是 scope="singleton" 然後 預設都是單例模式 spring載入的時候就會把這些物件建立好
<bean id="student" class="com.ared.entity.Student" scope="singleton">
2、多例模式: 當每次getBean的時候才建立一個新的物件 初始話的時候不會建立
<bean id="student" class="com.ared.entity.Student" scope="prototype">