Spring Boot无Web应用

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>com.example</groupId>
 <artifactId>NotWebProject</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <properties>
  <maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.source>1.8</maven.compiler.source>
 </properties>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.0.RELEASE</version>
 </parent>

 <dependencies>
  <!-- 仅仅需要基本的starter,不需要web -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
    <exclusion>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-log4j2</artifactId>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <executable>true</executable>
     <includeSystemScope>true</includeSystemScope>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <!-- 对要打的jar包进行配置 -->
    <configuration>
     <!-- Configuration of the archiver -->
     <archive>
      <!--生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
      <addMavenDescriptor>false</addMavenDescriptor>

      <!-- Manifest specific configuration -->
      <manifest>
       <!--是否要把第三方jar放到manifest的classpath中 -->
       <addClasspath>true</addClasspath>

       <!-- 生成的manifest中classpath的前缀, 因为要把第三方jar放到lib目录下, 所以classpath的前缀是lib/ -->
       <classpathPrefix>lib/</classpathPrefix>
      </manifest>
     </archive>
     <!--过滤掉不希望包含在jar中的文件 -->
     <excludes>
      <!-- 排除不需要的文件夹(路径是jar包内部的路径) -->
      <exclude>**/assembly/</exclude>
     </excludes>
    </configuration>
   </plugin>
  </plugins>
 </build>

</project>

application.properties
logging.level.root=error

ApplicationContextUtils.java
package com.example;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtils implements ApplicationContextAware {
    
    private static ApplicationContext context;

    public static <T> T getBean(Class<T> t) {
        return context.getBean(t);
    }

     @Override
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            context = applicationContext;
     }

}

Main.java
package com.example;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Main implements CommandLineRunner {
    

     public static void main(String[] args) throws IOException {
          SpringApplication springApplication = new SpringApplication(Main.class);

          ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);

          // 因为没有Servlet容器运行,所以程序不会阻塞
          System.in.read();
          
          ApplicationContextUtils.getBean(TestService.class).say();
     }
    
    @Resource private TestService testService;

     @Override
     public void run(String... args) throws Exception {
            testService.say();
     }

}

  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值