spring中的控制反转以及集合的遍历

首先配置applicationContext.xml,因为要展示遍历所以又写了一个applicationContextBean.xml文件

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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="byType">

<!--控制反转 -->
<bean id="fs" class="com.hp.dao.HelloDao" scope="prototype">
<!-- 给构造方法添加值 -->
<constructor-arg>
<value>张三</value>
</constructor-arg>
<constructor-arg value="23"></constructor-arg>
<constructor-arg ref="user"></constructor-arg>
<!-- 给list集合添加值 -->
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!-- 给set集合添加值 -->
<property name="set">
<set>
<ref bean="user" /><!-- 给set集合添加一个对象 -->

</set>
</property>
<!-- 给map集合添加值 -->
<property name="map">
<map>
<entry key="1">
<value>张三</value>
</entry>
<entry key="2" value="李四"></entry>
<entry key="3">
<value>王五</value>
</entry>
</map>
</property>


</bean>

<!--控制反转 -->
<bean id="fd" class="com.hp.service.HelleService"
init-method="init" destroy-method="close">
<!--依赖注入 -->
<!-- -->
<property name="hd" ref="fs"></property>
</bean>

 

<!-- 注入一个javabeanuser -->
<bean id="user" class="com.hp.bean.User">
<property name="id" value="1"></property>
<property name="name" value="李四"></property>
<property name="age" value="23"></property>
<property name="grade" value="java4班"></property>
</bean>

 

 

</beans>

applicationContextBean.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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- 注入一个javabean-user -->
<bean id="user" class="com.hp.bean.User"
p:id="100"
p:age="123"
p:grade="皇帝"
p:name="李师师"
/>

 

 

</beans>

 

 

然后写bean中User.java类

package com.hp.bean;

public class User {
private int id;
private String name;
private int age;
private String grade;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}




}

然后在写dao层中HelloDao.java

package com.hp.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.hp.bean.User;

public class HelloDao {

private String name;
private int age;
private User user;
private List<String> list = new ArrayList<String>();
private Set<User> set = new HashSet<User>();

private Map<Integer,String> map = new HashMap<Integer,String>();


public void setMap(Map<Integer, String> map) {
this.map = map;
}

 

public void setSet(Set<User> set) {
this.set = set;
}

 

public void setList(List<String> list) {
this.list = list;
}

 

public void setName(String name) {
this.name = name;
}

 

public void setAge(int age) {
this.age = age;
}

 

public void setUser(User user) {
this.user = user;
}

 

public HelloDao(String name, int age, User user) {
super();
this.name = name;
this.age = age;
this.user = user;
}

 

public void print(){
System.out.println("已经为你找到以下数据"+name+""+age);
System.out.println("userbean里面的数据为:"+user.getAge()
+user.getGrade()+user.getId()+user.getName());


//遍历list集合
System.out.println("------遍历list集合-------");
for(String l:list){
System.out.println(l);
}

//遍历set集合
System.out.println("------遍历set集合-------");
for(User s:set){
System.out.println(s.getId());
System.out.println(s);
}
//第一种遍历map集合
Iterator<Entry< Integer, String>> s =map.entrySet().iterator();
System.out.println("------遍历map集合-------");
while(s.hasNext()){
Entry<Integer,String> e = s.next();

System.out.println(e.getKey());
System.out.println(e.getValue());
}
//第二种map集合遍历
Iterator<Integer> it = map.keySet().iterator();
System.out.println("------遍历map集合-------");
while(it.hasNext()){
int key = it.next();
System.out.println(key);
System.out.println(map.get(key));
}
}

}

HelleService.java类

package com.hp.service;


import com.hp.dao.HelloDao;

public class HelleService {

public HelleService(){
System.out.println("HelleService的构造方法开始执行");
}
//初始方法
public void init(){
System.out.println("HelleService的构造方法开始执行");
}



//接收以下配置文件注入的实体类
private HelloDao hd;


public void setHd(HelloDao hd) {
this.hd = hd;
}

 


public void print(){
hd.print();
}

public void close(){
System.out.println("调用close方法释放资源");
}




}

最后写一个测试方法测试

package com.hp.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hp.service.HelleService;

public class Test1 {


public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelleService s= (HelleService) context.getBean("fd");
s.print();
s.close();
}

}

 

运行部分代码展示

 

 

posted @ 2016-11-21 16:02  倾世【天意】  阅读(399)  评论(0编辑  收藏  举报