java開發(fā)4年經驗,現(xiàn)在才在項目中真正用上Spring翅楼,慚愧。
1 生成目錄結構
IDEA生成工程目錄機構和pom.xml:
File-> New -> Project…->Maven -> 勾選Create from archetype -> org.apache.camel.archetypes:maven-archetype-quickstart -> Next ->
生成的目錄結構(除了resources目錄和java文件)如下:
springlearn
|─pom.xml
|─src
|─main
| |─java
| |-indi.zhb
| |-Quest.java
| |-SlayDragonQuest.java
| |-Knight.java
| |-BraveKnight.java
| |-App.java
|
| |─resources
| |─spring.xml
|
|─test
|─java
2. pom.xml
生成的pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>indi.zhb</groupId>
<artifactId>springlearn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springlearn</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
參考官方文檔https://projects.spring.io/spring-framework/ 纫雁,加入spring依賴:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
代碼內容(改自Spring in action第一章)
Quest.java
package indi.zhb;
/**
* Created by huabinzheng on 2017/6/26.
*/
public interface Quest {
void embark();
}
SlayDragonQuest.java
package indi.zhb;
import java.io.PrintStream;
/**
* Created by huabinzheng on 2017/6/26.
*/
public class SlayDragonQuest implements Quest {
private PrintStream stream;
public SlayDragonQuest(PrintStream stream) {
this.stream = stream;
}
public void embark() {
stream.println("Embarking on quest to slay the dragon!");
}
}
Knight.java
package indi.zhb;
/**
* Created by huabinzheng on 2017/6/26.
*/
public interface Knight {
public void embarkOnQuest();
}
BraveKnight.java
package indi.zhb;
/**
* Created by huabinzheng on 2017/6/26.
*/
public class BraveKnight implements Knight {
private Quest quest;
public BraveKnight(Quest quest) {
this.quest = quest;
}
public void embarkOnQuest() {
quest.embark();
}
}
App.java
package indi.zhb;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BraveKnight knight = context.getBean(BraveKnight.class);
knight.embarkOnQuest();
context.close();
}
}
Spring配置文件
用IDE來生成配置文件框架:右擊resources-> New -> XML Configuration File -> Spring Config锅论,取名為spring.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.xsd">
</beans>
往xml文件添加bean:
<bean id = "knight" class = "indi.zhb.BraveKnight">
<constructor-arg ref = "quest" />
</bean>
<bean id="quest" class="indi.zhb.SlayDragonQuest">
<constructor-arg value="#{T(System).out}"/>
</bean>