spring --(1)入门

spring4.x中文文档
http://spring.cndocs.tk/index.html
开涛的spring4.1介绍
http://jinnianshilongnian.iteye.com/blog/2102278
spring4框架参考手册
http://www.open-open.com/doc/view/b15f500781184c4c9f731577a9b83746
spring4参考中文手册
http://www.docin.com/p-1426181729.html
spring官网
http://projects.spring.io/spring-framework/
maven下载路径
http://mvnrepository.com/
sts插件下载
http://spring.io/tools/sts/allhtml

<dependencies> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId> 
        <version>4.3.0.RELEASE</version> 
    </dependency> 
</dependencies>

我下载的jar包以下
输入图片说明
开始spring的第一个示例
1.新建一个java project
2.导入spring的5个jar包,同时须要导入commons-logging.jar包,由于这个是spring的依赖包
3.建一个HelloWorld类java

public class HelloWorld {
	
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void hello() {
		System.out.println("hello:"+name);
	}
	
}

4.新建一个Main类spring

public class Main {
	
	public static void main(String[] args) {
		
		//常规作法
		//建立实例对象
		HelloWorld helloWorld = new HelloWorld();
		//给对象赋值
		helloWorld.setName("test");
		//调用对象方法
		helloWorld.hello();

		
		//使用spring
		//1.建立spring的ioc容器对象,即调用配置文件中全部类的构造器方法,同时给其属性设值
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("appli.xml");
		//2.从ioc容器中获取bean实例,即上述配置文件中bean 的 id
		HelloWorld helloWorld2 =(HelloWorld) ctx.getBean("helloWorld");
		//3.调用示例方法
		helloWorld2.hello();
	}

}

5.appli.xml配置文件以下app

<?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 -->
	<bean id="helloWorld" class="com.test.bean.HelloWorld">
		<property name="name" value="spring"></property>	
	</bean>

</beans>
相关文章
相关标签/搜索