一、依賴注入擴展
該篇講依賴注入的擴展,用其他的方式注入。
使用:p-namespace、c-namespace 簡化代碼。
項目結構:
創建實體類Student和Address。
public class Address {
private String address;
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class Student {
private String name;
/** 現住址 */
private Address address;
/** 籍貫 */
private Address nativePlace;
public Student() {
}
public Student(String name, Address address, Address nativePlace) {
this.name = name;
this.address = address;
this.nativePlace = nativePlace;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", nativePlace=" + nativePlace +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getNativePlace() {
return nativePlace;
}
public void setNativePlace(Address nativePlace) {
this.nativePlace = nativePlace;
}
}
設置配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 地址bean -->
<bean id="address" class="com.shengjava.pojo.Address">
<property name="address" value="中國浙江"></property>
</bean>
<!-- p-namespace使用(p-namespace中的p相當於property標籤,簡化代碼用的) -->
<!-- 不使用p-namespace時,使用無參構造器創建bean對象 -->
<bean id="student01Before" class="com.shengjava.pojo.Student">
<property name="name" value="張三"></property>
<property name="address">
<ref bean="address"></ref>
</property>
<property name="nativePlace">
<ref bean="address"></ref>
</property>
</bean>
<!-- 使用p-namespace時,使用無參構造器創建bean對象 -->
<bean id="student01After" class="com.shengjava.pojo.Student" p:name="張三" p:address-ref="address" p:nativePlace-ref="address"></bean>
<!-- c-namespace使用(c-namespace中的c相當於constructor-arg標籤,簡化代碼用的) -->
<!-- 不使用c-namespace時,使用有參構造器創建bean對象 -->
<bean id="student02Before" class="com.shengjava.pojo.Student">
<constructor-arg name="name" value="張三"/>
<constructor-arg name="address" ref="address"/>
<constructor-arg name="nativePlace" ref="address"/>
</bean>
<!-- 不使用c-namespace時,使用有參構造器創建bean對象 -->
<bean id="student02After" class="com.shengjava.pojo.Student" c:name="張三" c:address-ref="address" c:nativePlace-ref="address"></bean>
</beans>
注意點:配置文件中,需要引入xml命名空間。
xmlns:p=“http://www.springframework.org/schema/p”,代表引入p-namespace
xmlns:c=“http://www.springframework.org/schema/c”,代表引入c-namespace
編寫測試類
public class StudentTest {
public static void main(String[] args) {
// 獲取context
ApplicationContext context = new ClassPathXmlApplicationContext("studentBeans.xml");
// 獲取對象
Student student01Before = context.getBean("student01Before", Student.class);
Student student01After = context.getBean("student01After", Student.class);
Student student02Before = context.getBean("student02Before", Student.class);
Student student02After = context.getBean("student02After", Student.class);
System.out.println(student01Before);
System.out.println(student01After);
System.out.println(student02Before);
System.out.println(student02After);
}
}
輸出:
Student{name='張三', address=Address{address='中國浙江'}, nativePlace=Address{address='中國浙江'}}
Student{name='張三', address=Address{address='中國浙江'}, nativePlace=Address{address='中國浙江'}}
Student{name='張三', address=Address{address='中國浙江'}, nativePlace=Address{address='中國浙江'}}
Student{name='張三', address=Address{address='中國浙江'}, nativePlace=Address{address='中國浙江'}}
以上就是依賴注入的擴展,p-namespace、c-namespace 主要是減少一點代碼量。
相關
我的該分類的其他相關文章,請點擊:【Spring + Spring MVC + MyBatis】文章目錄