spring(1):spring入门,IOC介绍

http://mynote.iteye.com/admin/blogs/1174520

先看看上面这个简单工厂的例子:

 

spring的IOC类似于简单工厂,为客户端生成实例:将上面的简单工厂改为spring实现,如下:

 

Person接口

package com.test;

public interface Person {
	public String sayHello(String name);
	public String sayGoodbye(String name);
}

 

Chinese

package com.test;

public class Chinese implements Person {

	public String sayGoodbye(String name) {
		return "再见! "+name;
	}

	public String sayHello(String name) {
		return "你好! "+name;
	}

}

 

American

package com.test;

public class American implements Person {

	public String sayGoodbye(String name) {
		return "GoodBye! "+name;
	}

	public String sayHello(String name) {
		return "Hello! "+name;
	}

}

 

applicationContext.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>  
  <bean id="chinese" class="com.test.Chinese"/>
  <bean id="american" class="com.test.American"/>
</beans>

 

测试类

package com.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Client {
	public static void main(String[] args) {
		ClassPathResource cpr = new ClassPathResource("applicationContext.xml");
		XmlBeanFactory factory = new XmlBeanFactory(cpr);
		Chinese chinese = (Chinese) factory.getBean("chinese");
		System.out.println(chinese.sayHello("lisi"));
		System.out.println(chinese.sayGoodbye("lisi"));
	}
}

结果打印出:你好! lisi

                  再见! lisi

 

再看一个例子:

Person接口

package com.test;

public interface Person {
	public void doWork();
}

 

Tool接口

package com.test;

public interface Tool {
	public void doRealWork();
}

 

具体类:Chinese

package com.test;

public class Chinese implements Person {
	private Tool tool;

	public void doWork() {
		tool.doRealWork();
	}

	public Tool getTool() {
		return tool;
	}

	public void setTool(Tool tool) {
		this.tool = tool;
	}
}

 

具体工具A

package com.test;

public class ConcreteToolA implements Tool {

	public void doRealWork() {
		System.out.println("do concrete tool a");
	}

}

 

spring配置文件,可以看到在chinese的bean中引用了ConcreteToolA的实例

<bean id="toolA" class="com.test.ConcreteToolA"/>
<bean id="chinese" class="com.test.Chinese">
	<property name="tool" ref="toolA"/>
</bean>

 测试类

package com.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Client {
	public static void main(String[] args) {
		ClassPathResource cpr = new ClassPathResource("applicationContext.xml");
		XmlBeanFactory factory = new XmlBeanFactory(cpr);
		Person person = (Person) factory.getBean("chinese");
		person.doWork();
	}
}

  

打印结果:do concrete tool a

 

再看一个例子:可以spring配置文件中设置属性的值 

 

GreetingService接口

package com.springinaction.chapter01;

public interface GreetingService {
	public void sayGreeting();
}

 

具体实现类

package com.springinaction.chapter01;

public class GreetingServiceImpl implements GreetingService {
	private String greeting;

	public void sayGreeting() {
		System.out.println(this.greeting);
	}
	
	public void setGreeting(String greeting) {
		this.greeting = greeting;
	}
	
}

 

 

spring配置文件

<bean id="greetingService"  class="com.springinaction.chapter01.GreetingServiceImpl">
    <property name="greeting">
        <value>lisi</value>
    </property>
  </bean>

 

测试类

package com.springinaction.chapter01;

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

public class HelloApp {
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
		GreetingService greetingService = (GreetingService) context.getBean("greetingService");
		greetingService.sayGreeting();
	}
}

 

 

如果类中的属性是数组类型的该如何处理?看下例:

接口类

package com.test;

public interface Person {
	public void test();
}

 

具体类Person,定义了很多集合类型

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Chinese implements Person {
	private List schools = new ArrayList();
	private Map score = new HashMap();
	private Properties health = new Properties();
	private Set tools = new HashSet();

	public Map getScore() {
		return score;
	}

	public void setScore(Map score) {
		this.score = score;
	}

	public Properties getHealth() {
		return health;
	}

	public void setHealth(Properties health) {
		this.health = health;
	}
	
	public Set getTools() {
		return tools;
	}

	public void setTools(Set tools) {
		this.tools = tools;
	}
	
	public List getSchools() {
		return schools;
	}

	public void setSchools(List schools) {
		this.schools = schools;
	}

	public void test() {
		System.out.println(schools);
		System.out.println(score);
		System.out.println(health);
		System.out.println(tools);
	}

}

 

在spring配置文件中赋值

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>  
	<bean id="toolA" class="com.test.ConcreteToolA"/>
  	<bean id="chinese" class="com.test.Chinese">
  		<property name="schools">
  			<list>
  				<value>小学</value>
  				<value>中学</value>
  				<value>大学</value>
  			</list>
  		</property>
  		
  		<property name="score">
  			<map>
  				<entry key="数学">
  					<value>81</value>
  				</entry>
  				<entry key="英语">
  					<value>89</value>
  				</entry>
  				<entry key="语文">
  					<value>82</value>
  				</entry>
  			</map>
  		</property>
  		
  		<property name="health">
  			<props>
  				<prop key="血压">正常</prop>
  				<prop key="身高">175</prop>
  			</props>
  		</property>
  		
  		<property name="tools">
  			<set>
  				<value>字符串工具</value>
  				<bean class="com.test.ConcreteToolA"></bean>
  				<ref local="toolA"/>
  			</set>
  		</property>
  	</bean>
</beans>




 

测试类Client

package com.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Client {
	public static void main(String[] args) {
		ClassPathResource cpr = new ClassPathResource("applicationContext.xml");
		XmlBeanFactory factory = new XmlBeanFactory(cpr);
		Person person = (Person) factory.getBean("chinese");
		person.test();
	}
}

 

打印出的结果

[小学, 中学, 大学]
{数学=81, 英语=89, 语文=82}
{血压=正常, 身高=175}
[字符串工具, com.test.ConcreteToolA@a1807c, com.test.ConcreteToolA@fa7e74]

 

你可能感兴趣的:(Spring入门)