1

I am trying to implement Spring in a spigot plugin. I noticed I got the BeanNotFoundException, when searching around on the internet I tried some things but it never worked. So I tried running the same code in the main function and it worked perfectly.

Java plugin code:

private static AnnotationConfigApplicationContext context;

@Override
public void onEnable() {
    context = new AnnotationConfigApplicationContext("net.kikker234");

    Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
}

Main code:

private static AnnotationConfigApplicationContext context;

public static void main(String[] args) {
    context = new AnnotationConfigApplicationContext("net.kikker234");

    Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
}

Pom.xml dependencies:

   <dependencies>
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.18-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.4.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.15</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.6.3</version>
    </dependency>
</dependencies>

Output main function:

    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalPersistenceAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
core
registration

Output java plugin:

  [10:56:00] [Server thread/INFO]: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[10:56:00] [Server thread/INFO]: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[10:56:00] [Server thread/INFO]: org.springframework.context.annotation.internalPersistenceAnnotationProcessor
[10:56:00] [Server thread/INFO]: org.springframework.context.event.internalEventListenerProcessor
[10:56:00] [Server thread/INFO]: org.springframework.context.event.internalEventListenerFactory

I understand that I can't use the main function in a plugin, but what can I try as a workaround?

BTW: when I use the @ComponentScan annotation it didn't work in both cases.

Justin
  • 39
  • 1
  • 5

1 Answers1

1

Spring needs at least one source wich is annotated with @ComponentScan to scan for components.

You could use the SpringApplicationBuilder. It provides an easy setup with the builder pattern.

ApplicationContext context = new SpringApplicationBuilder(Main.class)
  .run(new String[]{});
DevOskar
  • 40
  • 7