Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Autowire a Spring-annotated service class in a @Configuration class?

I'm trying to inject a service-annotated class into a configuration class in a Spring Boot application, but it doesn't get injected (is set to null), which I assume is due to the Spring lifeycle.

Also, this service has an overloaded constructor that uses constructor injection, and I guess this is also a problem, as autowiring acts upon a default constructor. However, the service needs to be Spring-configured, so I don't think one can create a new instance in a Bean annotated method.

How can one solve this?

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private SessionService sessionService;

    @Bean
    public SessionService sessionService() {
        return sessionService;
    }
}

public interface SessionService extends BaseCacheService<Session> {
    void extendExpiration(String key);

    String getSessionId(String key);
}

@Service
public class SessionServiceImpl implements SessionService {

    private Environment environment;
    private UserService userService;

    @Autowired
    public SessionServiceImpl(Environment environment, UserService userService) {
      this.environment = environment;
      this.userService = userService;
    }
}

If I exclude the @Bean method, then I get a compilation error: Autowire compilation error if no bean found

like image 991
Razeen Avatar asked Jul 12 '16 19:07

Razeen


People also ask

Can you Autowire in a configuration class?

The configuration classes themselves are registered as beans to the Spring container. That means, we can do whatever we do with a normal spring bean. For example we can use @Autowire to have Spring to perform DI in them.

Can we use @autowired in service class?

Spring @Autowired Annotation - Service ClassWe will use the same service class for perform spring autowiring byName, byType and by constructor. The setter method will be used for spring autowiring byName and byType whereas constructor based injection will be used by constructor autowire attribute.

What is the use of @configuration annotation in Spring?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.


1 Answers

Your error is the following (you are returning a null value):

@Bean
public SessionService sessionService() {
    return sessionService;
}

Solution

  1. Since your SessionServiceImpl is annotated with @Service, you can just remove the @Bean method and let spring create it. Spring already makes it available for you.

  2. Or, If your SessionServiceImpl wasn't annotated with @Service, you would need the following :

@Bean
public SessionService sessionService() {
   return new SessionService();
}

If this doesn't work, it may just be that your SessionServiceImpl is in a package not being scanned by spring (as suggested by @Miloš Milivojević)

You may add @ComponentScan to your Configuration class

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@ComponentScan("com.package.to.sessionServiceImpl-or-higher")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
like image 97
alexbt Avatar answered Oct 21 '22 05:10

alexbt