spring项目run起来的最小依赖

spring项目跑起来,只需要spring-context这1个依赖项就行,参考下面:

 一、pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.cnblogs.yjmyzz</groupId>
 7     <artifactId>spring-boot-demo</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9 
10     <properties>
11         <java.version>1.8</java.version>
12     </properties>
13 
14     <dependencies>
15         <dependency>
16             <groupId>org.springframework</groupId>
17             <artifactId>spring-context</artifactId>
18             <version>5.2.4.RELEASE</version>
19         </dependency>
20     </dependencies>
21 
22     <build>
23         <plugins>
24             <plugin>
25                 <artifactId>maven-compiler-plugin</artifactId>
26                 <version>3.1</version>
27                 <configuration>
28                     <source>1.8</source>
29                     <target>1.8</target>
30                 </configuration>
31             </plugin>
32         </plugins>
33     </build>
34 
35 </project>
View Code

二、示例代码:

 1 package com.cnblogs.yjmyzz.springbootdemo;
 2 
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.stereotype.Service;
 7 
 8 /**
 9  * @author 菩提树下的杨过
10  */
11 @ComponentScan("com.cnblogs.yjmyzz")
12 @Configuration
13 public class SampleApplication {
14 
15     interface SampleService {
16         void helloWorld();
17     }
18 
19     @Service
20     class SampleServiceImpl implements SampleService {
21 
22         @Override
23         public void helloWorld() {
24             System.out.println("hello spring");
25         }
26     }
27 
28     public static void main(String[] args) {
29         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class);
30         SampleService service = context.getBean(SampleService.class);
31         service.helloWorld();
32     }
33 }
View Code

项目结构:

spring-context的依赖关系如下:

posted @ 2020-03-01 23:28  菩提树下的杨过  阅读(...)  评论(...编辑  收藏