Spring 4.0
描述
Spring(官网:projects.spring.io)是一个框架,为了简化企业级应用开发而生,使用Spring可以使简单的javabean实现以前只有gjb才能实现的功能。
安装
下载spring的jar包,在eclipse中选择help–>install new software–>add–>archive,然后找到下载的安装包,选中,在选择功能中选择带有spring IDE的项目,安装重启即可.
若在window–> preperences可以找到Spring选项,表示安装成功
hello world
1、新建Java project
2、导入
commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
五个包
3、新建一个bean类
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void run(){
System.out.println(name+ " running");
}
}
4、创建一个Spring配置文件,即new–>Spring Bean Configuration File
<?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 = "person" class="com.spring.test.Person">
<property name="name" value = "spring"></property>
</bean>
</beans>
5、编写main
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) application.getBean("person");
person.run();
}
}
6、运行
一月 09, 2018 10:16:10 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@28fa1b85: startup date [Tue Jan 09 22:16:10 CST 2018]; root of context hierarchy
一月 09, 2018 10:16:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
spring running