Spring框架学习之第7节
2023-09-14 09:07:42 时间
配置Bean的细节
☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大
②如何给集合类型注入值
Java中主要的map,set,list / 数组
Collection col = new ArrayList();
col能够使用的方法(点出来的方法)取决于左边的数据类型
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="department" class="com.litao.collection.Department"> <property name="name" value="财务部" /> <!-- 给数组注入值 --> <property name="empName"> <list> <value>小明</value> <value>小明小明</value> <value>小明小明小明小明</value> </list> </property> <!-- 给list注入值 list中可以有相同的对象 --> <property name="empList"> <list> <ref bean="emp1"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> </list> </property> <!-- 给set注入值 set中不能有相同的对象 --> <property name="empsets"> <set> <ref bean="emp1"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> </set> </property> <!-- 给map注入值 map中也不能有相同的对象,后面的会把前面的覆盖,map只要key一样就可以装配value对应的bean --> <property name="empMaps"> <map> <entry key="1" value-ref="emp1"></entry> <entry key="2" value-ref="emp2"></entry> <entry key="2" value-ref="emp2"></entry> </map> </property> </bean> <bean id="emp1" class="com.litao.collection.Employee"> <property name="name" value="北京" /> <property name="id" value="1" /> </bean> <bean id="emp2" class="com.litao.collection.Employee"> <property name="name" value="天津" /> <property name="id" value="2" /> </bean> </beans>
Employee.java
package com.litao.collection; public class Employee { private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
Department.java
package com.litao.collection; import java.util.List; import java.util.Map; import java.util.Set; public class Department { private String name; private String[] empName; private List<Employee> empList; private Set<Employee> empsets; private Map<String,Employee> empMaps; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getEmpName() { return empName; } public void setEmpName(String[] empName) { this.empName = empName; } public List<Employee> getEmpList() { return empList; } public void setEmpList(List<Employee> empList) { this.empList = empList; } public Set<Employee> getEmpsets() { return empsets; } public void setEmpsets(Set<Employee> empsets) { this.empsets = empsets; } public Map<String, Employee> getEmpMaps() { return empMaps; } public void setEmpMaps(Map<String, Employee> empMaps) { this.empMaps = empMaps; } }
App1.java
package com.litao.collection; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac = new ClassPathXmlApplicationContext("com/litao/collection/beans.xml"); Department department = (Department)ac.getBean("department"); System.out.println(department.getName()); for (String emName : department.getEmpName()) { System.out.println(emName); } System.out.println("************通过list集合取出数据**************"); for(Employee e: department.getEmpList()){ System.out.println("name=" + e.getName() + " " + e.getId()); } //set取得时候不能保证顺序,list取时可以保证顺序 System.out.println("************通过set集合取出数据**************"); for(Employee e: department.getEmpsets()){ System.out.println("name=" + e.getName()); } System.out.println("************通过map集合取出数据**************"); //1.用迭代器 Map<String,Employee> empmaps = department.getEmpMaps(); Iterator it = empmaps.keySet().iterator(); while (it.hasNext()) { String key = (String)it.next(); //System.out.println(key); Employee emp = empmaps.get(key); System.out.println("key="+key+" "+emp.getName()); } //2.最简洁的方法 for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){ System.out.println(entry1.getKey() + " " + entry1.getValue().getName()); } } }
相关文章
- 消息中间件activemq-5.13.0整合spring
- spring学习笔记(25)spring整合quartz多版本实现企业级任务调度
- Spring MVC 处理一个请求的流程分析
- ssm(Spring+Spring mvc+mybatis)——showDept.jsp
- spring: spittr实例 构建简单的web应用
- Spring MVC学习问题记录
- 18-spring学习-利用Annotation配置AOP
- 13-spring学习-class类型表达式
- 08-spring学习-annotation配置
- Spring 知识点提炼-转
- 深入学习微框架:Spring Boot
- 监听器如何获取Spring配置文件
- Spring 学习 3- AOP
- Spring学习-1 框架总览
- 不一样的视角来学习Spring源码之容器与Bean---下
- Spring Beans 自动装配
- Java Spring MVC model学习
- 高效实现Web开发:掌握Spring Boot 2高级模块Thymeleaf的神奇妙用
- 学习Spring Boot:(二十四)多数据源配置与使用
- 学习Spring Boot:(十四)spring-shiro的密码加密
- 学习Spring Boot:(四)应用日志
- 毕业设计 Spring Boot的在线课堂学习系统(含源码+论文)
- Spring框架学习之第8节
- SpringBoot学习笔记(四)——Spring Boot集成MyBatis起步
- Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)
- Spring aop 小例子demo
- 【Spring注解驱动开发】二狗子让我给他讲讲@EnableAspectJAutoProxy注解
- JavaEE——Spring学习笔记02【Spring和Mybatis的整合】