码迷,mamicode.com
首页 > 编程语言 > 详细

Spring IoC的使用配置(一)

时间:2014-12-17 22:42:15      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:spring   xml配置   

       本文主要交代Spring相关的配置的快速了解与使用,所以对于不长常使用的配置项简单叙述或不讲解,需要深入了解Spring的原理,还需读者自行学习或补充。
本例使用的Spring版本为spring-4.0.0.M2,准备环境操作如下:
一、创建Java Project项目,导入需要的Spring的jar。本例使用的包括:

/SpringLearn/lib/spring-aop-4.0.0.M2.jar
/SpringLearn/lib/spring-beans-4.0.0.M2.jar
/SpringLearn/lib/spring-context-4.0.0.M2.jar
/SpringLearn/lib/spring-core-4.0.0.M2.jar
/SpringLearn/lib/spring-expression-4.0.0.M2.jar
/SpringLearn/lib/spring-instrument-4.0.0.M2.jar
        另外包括spring引用的外部jar文件。
/SpringLearn/lib/commons-logging-1.1.3.jar

 二、在src下创建主配置文件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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Learn 01 Bean的命名  导入相关的spring配置文件-->
	<import resource="com/mahaochen/spring/learn01/beanLearn01.xml"/>

</beans>

【转载使用,请注明出处:http://blog.csdn.net/mahoking

Spring bean的基本配置

 

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="spearker01" class="com.mahaochen.spring.learn01.Speaker"
		scope="singleton" />
</beans>

id:称为“标识符”,用于指明对应的bean,id值必须是唯一的;
class:用于指明需要实例化对应的类对象;
scope:用于指明实例对应的作用域。


 Spring bean的命名

 

操作步骤:
1、 创建Speaker对象。

public class Speaker {

	/**
	 * 演讲
	 */
	public void speech() {
		System.out.println("The Speaker starts to speech!");
	}
}


2、 创建Spring配置文件beanLearn01.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


	<!-- Learn 01 Bean的命名 -->
	<!--scope:
		singleton 在每个Spring IoC容器中一个bean定义对应一个对象实例。 prototype 一个bean定义对应多个对象实例。 
		request 在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring 
		ApplicationContext情形下有效。 session 在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring 
		ApplicationContext情形下有效。 -->
	<bean id="spearker01" class="com.mahaochen.spring.learn01.Speaker"
		scope="singleton" />
</beans>

3、 将Spring配置文件beanLearn01.xml引入到主配置文件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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Learn 01 Bean的命名  -->
	<import resource="com/mahaochen/spring/learn01/beanLearn01.xml"/>
</beans>

4、 编写测试类TestSpring01.java。

public class TestSpring01 {

	public static void main(String[] args) {
		
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
		//根据id获取bean【方式一】
		Speaker speaker01 = (Speaker) beanFactory.getBean("spearker01");
		speaker01.speech();
		//根据id获取bean【方式二】
		Speaker speaker02 = beanFactory.getBean("spearker01", Speaker.class);
		speaker02.speech();
		System.out.println("Bean 的 作用域 配置成 scope=\"singleton\" 时:两个Speaker是同一对象引用");
		System.out.println("\"Speaker speaker01 = Speaker speaker02?\"  " +(speaker01==speaker02));
	}
}


使用构造器实例化Bean


操作步骤:
1、创建Speaker对象。

public class Speaker {
	private String name ;
	private String topic;
	private int timeHour;
	/**
	 * 空的构造函数
	 */
	public Speaker() {
	}

	/**
	 * 带参构造函数
	 * @param name
	 * @param topic
	 * @param timeHour
	 */
	public Speaker(String name, String topic, int timeHour) {
		this.name = name;
		this.topic = topic;
		this.timeHour = timeHour;
	}

	/**
	 * 演讲
	 */
	public void speech() {
		System.out.println(toString());
	}

	@Override
	public String toString() {
		return "Speaker [name=" + name + ", topic=" + topic + ", timeHour="
				+ timeHour + "]";
	}
}


2、创建Spring配置文件beanLearn02.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- Learn 02 使用构造器实例化Bean -->
	<!--使用有参数构造参数--> 
	<bean id="speaker02" class="com.mahaochen.spring.learn02.Speaker">
		<!-- 指定构造器参数 -->  
		<constructor-arg index="0" value="Make"/>
		<constructor-arg index="1" value="enjoy your lift!"/>
		<constructor-arg index="2" value="1"></constructor-arg>
	</bean>
</beans>

3、将Spring配置文件beanLearn02.xml引入到主配置文件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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Learn 02  使用构造器实例化Bean -->
	<import resource="com/mahaochen/spring/learn02/beanLearn02.xml"/>
</beans>

4、编写测试类TestSpring02.java。

public class TestSpring02 {

	public static void main(String[] args) {
		
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
		//使用构造器实例化Bean
		Speaker speaker = beanFactory.getBean("speaker02", Speaker.class);
		speaker.speech();
		System.out.println("Success!");
	}
}




【转载使用,请注明出处:http://blog.csdn.net/mahoking

Spring IoC的使用配置(一)

标签:spring   xml配置   

原文地址:http://blog.csdn.net/mahoking/article/details/41988563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!