一、依赖注入扩展
该篇讲依赖注入的扩展,用其他的方式注入。
使用: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】文章目录