spring的简介与初步搭建应用

一、spring的简介

  spring是什么东西就不介绍了,自己去找谷老师。

二、spring的下载

  最新的稳定spring3.1.0版本,下载地址:http://www.springsource.org/download/,最好下载“with docs”的版本,其中包括了spring API及帮助文档,这些文档对学习spring是很有用的,我的第一个简单示例就是参照文档搭建起来的。

三、简单的示例应用(使用xml的格式定义bean)

下图是我的搭建图,readMe.txt是本人的自己添加的说明文档,没有这个文档没关系。

                    

 

1.       接口及实现类的编写

接口:

package com.kane.csdn.inter;

public interface TestInterface {
	public void showMessage();
}


实现类ImpleOne:

package com.kane.csdn.impl;

import com.kane.csdn.inter.TestInterface;

public class ImpleOne implements TestInterface {

	@Override
	public void showMessage() {
		System.out.println("Implement by One!");
	}

}

 

实现类ImpleTwo:

package com.kane.csdn.impl;

import com.kane.csdn.inter.TestInterface;

public class ImpleTwo implements TestInterface {

	@Override
	public void showMessage() {
		System.out.println("Implement by Two!");
	}

}

 

2.       Spring的配置文件

<?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-3.0.xsd">
        
   <bean id="test" class="com.kane.csdn.impl.ImpleOne"/>

   <!--
      <bean id="test" class="com.kane.csdn.impl.ImpleTwo"/>
   -->   
   <!-- more bean definitions go here -->

</beans>

 

3.       接下来就可以编写一个主方法测试了

package com.kane.csdn.test;

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

import com.kane.csdn.inter.TestInterface;

public class TestManage {

	public static void main(String[] args) {
		// 简单地实现IOC
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");

		TestInterface test = (TestInterface) applicationContext.getBean("test");

		test.showMessage();
	}

}


运行后,可以看到结果为打印输出“Implement by One!”。如果我将配置文件中的第一个ImpleOne的bean注释掉,使用第二个ImpleTwo的bean,大家可以运行看到结果输出为“Implement by Two!”。正如大家看到的,这就是spring的作用,当你需要修改实现方式的时候,你不必修改代码,你只需要修改配置文件,使用你所需要的实现,即可实现目标,这难道不是很神奇嘛!

 

四、使用Annotation

  Annotation,顾名思义,就是使用注视来代替你的XMLbean配置。下面我们将上面的示例进行改造,不在XML中定义bean,而是使用annotation

1.       修改你的配置文件,引入包自动扫描

<?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:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
               
     <context:component-scan base-package="com.kane.csdn.impl"/>

</beans>

对比上面,你会发现它不再配置你的bean,而是加入了一句<context:component-scan base-package="com.kane.csdn.impl"/>,它会自动将你com.kane.csdn.impl包下面的使用了注解annotation的类初始化为bean.

2.       修改你的定义实现类,为其加上注释,以便spring识别

package com.kane.csdn.impl;

import org.springframework.stereotype.Service;

import com.kane.csdn.inter.TestInterface;

@Service("test")
public class ImpleTwo implements TestInterface {

	@Override
	public void showMessage() {
		System.out.println("Implement by Two!");
	}

}

使用了@Service注解将该类定义为id="test"的bean.

 

3.       运行测试类,你发现它同样输出成功。

这就是annotation,只要将你的实现bean使用注释定义后,就可替代原来xml,使开发更加快速。当然并不是说所有的时候使用annotationxml都更加方便有效,当你的项目越来越大的时候,你会发现你的XML文件就会很庞大,这是如果你使用annotation就会减少很多的配置。如果你是用了annotation,假如你要去替换你的实现bean,你就要去修改类的注解,就时候你就要考虑清楚了,因为你要去修改你的代码,要确保这样不会影响其它。这个就好像在项目中使不使用框架一样,具体的情况还得具体对待。

 

五、总结

  通过上面的例子讲解了spring中最核心的IOC注入,就这么简单。Spring中还有很多东东,比如:AOPspring MVC等内容,这就要靠你自己去学习了。

推荐学习的时候多看看官方的文档或是API,比如在spring-framework-3.1.0.RELEASE的下载文件中有很多文档:html版本的,pdf格式的,里面有很多讲解及简单的学习示例是很不错的,我在学的时候就是这样看文档学的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章