1. 環境搭建
- 第一步搭建專案,根據下面專案詳情的內容先把project建好,IDEA如何搭建Spring專案,這個靠自己了
- 第二步原始碼下載,在 Spring原始碼下載 下載對應版本的Spring原始碼,本文基於4.3.11.RELEASE版本
- 第三部匯入原始碼,如何匯入看這篇Intellij idea關聯jar包和原始碼
2. 專案詳情
- Applicaton.java
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
MessageService messageService = (MessageService) context.getBean("messageService");
System.out.println(messageService.getMessage());
}
}
複製程式碼
- MessageService.java
public interface MessageService {
String getMessage();
}
複製程式碼
- MessageServiceImpl.java
public class MessageServiceImpl implements MessageService {
public String getMessage() {
return "hello world";
}
}
複製程式碼
- application.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
<bean id="messageService" class="com.ktcatv.springmvc.service.impl.MessageServiceImpl"/>
</beans>
複製程式碼
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.test</groupId>
<artifactId>springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
</dependencies>
</project>
複製程式碼
以上就是整個專案的所有檔案,如果有問題請自行谷歌