Spring项目运行时,spring-contex解析是否为必需依赖?
- 内容介绍
- 文章标签
- 相关推荐
本文共计408个文字,预计阅读时间需要2分钟。
Spring项目启动,仅需依赖spring-context这一个项即可运行。参考以下配置:
xml 4.0.0 com.example spring-project 1.0 UTF-8
spring项目跑起来,只需要spring-context这1个依赖项就行,参考下面:
一、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cnblogs.yjmyzz</groupId> <artifactId>spring-boot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
二、示例代码:
package com.cnblogs.yjmyzz.springbootdemo; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Service; /** * @author 菩提树下的杨过 */ @ComponentScan("com.cnblogs.yjmyzz") @Configuration public class SampleApplication { interface SampleService { void helloWorld(); } @Service class SampleServiceImpl implements SampleService { @Override public void helloWorld() { System.out.println("hello spring"); } } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class); SampleService service = context.getBean(SampleService.class); service.helloWorld(); } }
项目结构:
spring-context的依赖关系如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计408个文字,预计阅读时间需要2分钟。
Spring项目启动,仅需依赖spring-context这一个项即可运行。参考以下配置:
xml 4.0.0 com.example spring-project 1.0 UTF-8
spring项目跑起来,只需要spring-context这1个依赖项就行,参考下面:
一、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cnblogs.yjmyzz</groupId> <artifactId>spring-boot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
二、示例代码:
package com.cnblogs.yjmyzz.springbootdemo; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Service; /** * @author 菩提树下的杨过 */ @ComponentScan("com.cnblogs.yjmyzz") @Configuration public class SampleApplication { interface SampleService { void helloWorld(); } @Service class SampleServiceImpl implements SampleService { @Override public void helloWorld() { System.out.println("hello spring"); } } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class); SampleService service = context.getBean(SampleService.class); service.helloWorld(); } }
项目结构:
spring-context的依赖关系如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

