Spring介紹、安裝、helloworld
阿新 • • 發佈:2018-12-09
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