基于XML管理bean(3.0)

七,为集合类型属性赋值 

①为 List 集合类型属性赋值
Clazz 类中添加以下代码
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
配置 bean
<bean id="clazzTwo" class="com.atguigu.spring.bean.Clazz">
<property name="clazzId" value="4444"></property>
<property name="clazzName" value="Javaee0222"></property>
<property name="students">
<list>
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
</list>
</property>
</bean>
若为 Set 集合类型属性赋值,只需要将其中的 list 标签改为 set 标签即可
②为 Map 集合类型属性赋值
创建教师类 Teacher
public class Teacher { 
private Integer teacherId; 
private String teacherName; 
public Integer getTeacherId() { 
return teacherId; 
} 
public void setTeacherId(Integer teacherId) { 
this.teacherId = teacherId; 
} 
public String getTeacherName() { 
return teacherName; 
} 
public void setTeacherName(String teacherName) { 
this.teacherName = teacherName; 
} 
public Teacher(Integer teacherId, String teacherName) { 
this.teacherId = teacherId; 
this.teacherName = teacherName; 
}
public Teacher() { 
} 
@Override 
public String toString() { 
return "Teacher{" + 
"teacherId=" + teacherId + 
", teacherName='" + teacherName + '\'' + 
'}'; 
} 
}
Student 类中添加以下代码:
private Map<String, Teacher> teacherMap;
public Map<String, Teacher> getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
this.teacherMap = teacherMap;
}

配置 bean
<bean id="teacherOne" class="com.atguigu.spring.bean.Teacher"> 
<property name="teacherId" value="10010"></property> 
<property name="teacherName" value="大宝"></property> 
</bean> 
<bean id="teacherTwo" class="com.atguigu.spring.bean.Teacher"> 
<property name="teacherId" value="10086"></property> 
<property name="teacherName" value="二宝"></property> 
</bean> 
<bean id="studentFour" class="com.atguigu.spring.bean.Student"> 
<property name="id" value="1004"></property> 
<property name="name" value="赵六"></property> 
<property name="age" value="26"></property> 
<property name="sex" value="女"></property> 
<!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --> 
<property name="clazz" ref="clazzOne"></property> 
<property name="hobbies"> 
<array> 
<value>抽烟</value> 
<value>喝酒</value> 
<value>烫头</value> 
</array> 
</property> 
<property name="teacherMap"> 
<map> 
<entry> 
<key> 
<value>10010</value> 
</key> 
<ref bean="teacherOne"></ref> 
</entry> 
<entry> 
<key> 
<value>10086</value> 
</key> 
<ref bean="teacherTwo"></ref> 
</entry> 
</map> 
</property> 
</bean>
③引用集合类型的 bean
<!--list集合类型的bean--> 
<util:list id="students"> 
<ref bean="studentOne"></ref> 
<ref bean="studentTwo"></ref> 
<ref bean="studentThree"></ref> 
</util:list> 
<!--map集合类型的bean--> 
<util:map id="teacherMap"> 
<entry> 
<key> 
<value>10010</value> 
</key> 
<ref bean="teacherOne"></ref> 
</entry> 
<entry> 
<key> 
<value>10086</value> 
</key> 
<ref bean="teacherTwo"></ref> 
</entry> 
</util:map> 
<bean id="clazzTwo" class="com.atguigu.spring.bean.Clazz"> 
<property name="clazzId" value="4444"></property> 
<property name="clazzName" value="Javaee0222"></property> 
<property name="students" ref="students"></property> 
</bean> 
<bean id="studentFour" class="com.atguigu.spring.bean.Student"> 
<property name="id" value="1004"></property> 
<property name="name" value="赵六"></property> 
<property name="age" value="26"></property> 
<property name="sex" value="女"></property> 
<!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --> 
<property name="clazz" ref="clazzOne"></property> 
<property name="hobbies"> 
<array> 
<value>抽烟</value> 
<value>喝酒</value> 
<value>烫头</value> 
</array> 
</property> 
<property name="teacherMap" ref="teacherMap"></property>
</bean> 

八,引入外部属性文件

①加入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
②创建外部属性文件

jdbc.user=root
jdbc.password=atguigu
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver

 ③引入属性文件

<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
④配置 bean
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

⑤测试

@Test
public void testDataSource() throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext("springdatasource.xml");
DataSource dataSource = ac.getBean(DataSource.class);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值