您的位置:首页 > 编程语言 > Java开发

Spring IoC使用的基本配置

2014-12-17 21:51 471 查看
       本文主要交代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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: