前言:
无论是哪一种framework( 框架 ),它存在的意义都是为解决问题以及降低问题的复杂度而存在的,Spring也是如此。Spring是轻量的、开源的、非侵入式的framework,它提供方法管理你的业务对象,在大型的javaweb项目中独树一炽。接下来是对Spring的一些具体操作:
一:下载javaEE及配置https://jingyan.baidu.com/article/3a2f7c2e62d25e26afd611fa.html
二:下载Spring jar包http://repo.spring.io/release/org/springframework/spring/ 本人是3.1.1版本,Spring jar包位于dist目录下
三:下载tomcat服务器http://tomcat.apache.org/download-70.cgi 然后安装,没有导入eclipse的记得导入,方便日后进行开发
以上步骤完成之后接下来进行Spring的测试:
打开eclipse,新建名为fruit的java工程,并新建Fruit接口。代码如下:
package fruit;
public interface Fruit {
public void fruitfo();
}
然后新建名为Apple类的javabean,并实现接口中fruitf()方法,具体如下:
package fruit;
public class Apple implements Fruit {
private String taste;
public String getTaste(){
return taste;
}
public void setTaste(String taste){
this.taste = taste;
}
public void fruitfo(){
System.out.println("这种水果是:"+taste);
}
}
接下来在src右击新建application.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-3.0.xsd">
<bean id="fruit" class="fruit.Apple"><!-- 给bean一个id以及类的路径 -->
<property name="taste"><!-- 属性名即Fruit中的taste -->
<value>苹果</value><!-- 值 -->
</property>
</bean>
</beans>
最后新建主类为Production,如下
package fruit;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Production {
public static void main(String[] args) {
//寻找名为"applicationContext.xml"的资源
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
//获得javabean对象
Fruit t =(Fruit) factory.getBean("fruit");
t.fruitfo();
}
}
运行结果如下:
在这里要提醒的是要把Spring jar包导入项目中,步骤:Project > Properties > Java Build Path,选择Libraries,选择Add External JARS... 然后选择你的Spring jar包就行了
自此,一个简单的Spring项目就完成,希望这能帮助初入Spring的小伙伴们一个提示吧。