【问题标题】:How to use Spring for dependancy injection in a java program如何在java程序中使用Spring进行依赖注入
【发布时间】:2019-10-07 08:45:22
【问题描述】:

我想在 java 程序中使用 Spring 进行 DI。我有一些我想注入的配置类和客户端类等类,但我不想创建 Tomcat 服务器,我希望能够像通常那样在主函数中编写我的程序。

我尝试使用应用程序上下文但没有成功(您可以猜到)。

这里有两个类的代码来重现:

package com.alltests;

import com.socionodes.alltests.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

@SpringBootApplication
@Component
public class App 
{
  @Autowired 
  public static Config config;

  public static void main( String[] args )
  {
    ApplicationContext context = new AnnotationConfigApplicationContext();
    System.out.println("conf obj is : ");
    System.out.println(config);
  }
package com.alltests.configs;
import org.springframework.stereotype.Configuration;

@Configuration("config")
public class Config{
  private String user;

  public String getUser() {
    return user;
  }

  public void setUser(String user) {
    this.user = user;
  }

和 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.alltests</groupId>
  <artifactId>test-spring-di</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test-spring-di</name>
  <url>http://maven.apache.org</url>

  <properties>
    <java.version>1.8</java.version>
  </properties>

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


  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.alltests.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

properties.yml:

config:
  user: me

目标是自动在 config 中拥有 Config 对象。这可能吗?

【问题讨论】:

  • "我不想创建 Tomcat 服务器" 那你为什么要包含 spring-boot-starter-web
  • 您不需要 Spring Boot Server,对于 DI,您可以使用 Spring Core 功能,包括 Spring Core、Context、Spring Beans。
  • @Michael 你是对的,我只是坚持我所知道的,但这很愚蠢,ty。
  • @Sambit ty,我研究了一下,但我仍然很乐意举一个例子来说明如何使用这些依赖项创建应用程序上下文。
  • 全部解决,谢谢!

标签: java spring


【解决方案1】:

您的问题中有许多人为的陈述。我会尽力解释。

首先,我建议弄清楚您是否要使用弹簧靴。看起来你这样做了(通过放置 @SpringBootApplication 注释)但是在 main 方法中,你试图创建一个来自常规 spring 世界的上下文。

在 Spring Boot 中没有这样做的意义,它会为你准备一个应用程序上下文。

现在,如果您不想创建 tomcat - 它可以在 Spring Boot 中创建,在这种情况下,您不需要在 maven 中包含 spring-boot-starter-web 依赖项。

Spring boot 通常可以运行非基于 Web 的(使用 tomcat、jetty 等读取)应用程序:

这是example

如果您选择“普通”弹簧 - 在其核心含义中,它提供了一个 DI 容器。所以你那里不会有任何tomcat。

总而言之,您似乎并不真正了解普通弹簧和弹簧靴之间的区别(对不起,如果我弄错了,从您的问题和提供的代码 sn-ps 看起来就像这样)

【讨论】:

  • 你完全没有看错。我认为普通的春天就足够了。我看到的对spring boot的唯一兴趣是,我认为,自动组件扫描,但它带有很多我不明白的东西,我不想花太多时间在上面。我只希望 DI 用于常规 Java 程序中的一些对象。我会研究常规的 Spring,ty。
  • 常规弹簧也有组件扫描,虽然它没有建立关于应该扫描哪些包的规则 - 你应该自己指定要扫描的包。从你的评论看来你需要一个常规弹簧暂时。
猜你喜欢
  • 2011-02-06
  • 2015-03-01
  • 2014-10-27
  • 2011-08-04
  • 2012-01-24
  • 1970-01-01
  • 2010-11-29
  • 2010-10-26
  • 1970-01-01
相关资源
最近更新 更多