1

I am trying to use Spring Autowired and Configs without using springboot. I have the below dependencies and the class look like below (I got a service, component and config class). But I get SomeService as null always. What am I missing?

Config

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.somepackage")
public class AppConfig {
}

Component

 @Component
    public class TestClass extends Step {
    
        @Autowired
        private SomeService someservice;
    
        public void testConnectivity() {
            someservice.insertTransaction();
        }
    }

Service

    @Service
    public class SomeService {
    }

MainClass

public class Application {
    public static void main(String[] args) {
   SpringApplication application = new SpringApplication(Application .class);
   application.setWebApplicationType(WebApplicationType.NONE);
   application.run(args);
    }
}

Dependency

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.4.5</version>
    </dependency>
Minisha
  • 2,117
  • 2
  • 25
  • 56
  • You say `without using springboot.` but using `spring-boot-autoconfigure` - you don't need it. Attach a code of main class pleas – Alex May 17 '21 at 06:52
  • added main class. okay I will remove autoconfigure. I was thinking to read from application.properties this is required. – Minisha May 17 '21 at 07:00
  • Your main method doesn't do anything, so obviously nothing will bootstrap nor inject dependencies. – M. Deinum May 17 '21 at 07:14
  • SpringApplication application = new SpringApplication(MainApplication.class); application.setWebApplicationType(WebApplicationType.NONE); application.run(args); – Minisha May 17 '21 at 07:18
  • With the above it doesnt work either – Minisha May 17 '21 at 07:19
  • Because `SpringApplication` is from `spring-boot`. If you don't want to use `spring-boot` you have to configure all needed beans manually through configurations and create context manually. – Alex May 17 '21 at 07:25
  • Does this answer your question? [creating spring rest services without using spring boot](https://stackoverflow.com/questions/29416804/creating-spring-rest-services-without-using-spring-boot) – Alex May 17 '21 at 07:30

0 Answers0