Spring 3에서 @Named, @Inject 해결

손찬호·2024년 6월 15일
0

Spring Boot 3

목록 보기
2/10

발생한 문제

스프링 2.7.0에서 사용하던 @Named, @Inject 어노테이션이
스프링 3.3.0에서 사용하니 제대로 호환되지 않았다.
그래서 하는 수 없이 라이브러리를 맞게 수정해줘야했다.
그래서 import 경로를 javax->jakarta로 수정했는데도 여전히 문제가 발생했다.

문제 코드

package ...

import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Named
class BusinessService{
    private DataService dataService;
    @Inject
    public void setDataService(DataService dataService) {
        System.out.println("Setter Injection");
        this.dataService = dataService;
    }

    public DataService getDataService() {
        return dataService;
    }
}
@Named
class DataService{}

@Configuration
@ComponentScan
public class CDIApplication {
    public static void main(String[] args) {
        try(var context = new AnnotationConfigApplicationContext(com.penloo.learn4_4.CDIApplication.class)){
            Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
            System.out.println(context.getBean(BusinessService.class).getDataService());
        }
        catch (Exception exception){
        }
    }
}

해결

문제가 발생해서 대체할만한 어노테이션을 찾아보며 변경했다.

@Named -> @Component

https://stackoverflow.com/questions/18540696/named-annotation-in-spring-mvc
@Named works the same as @Component. However, the annotations @Controller, @Service, and @Repository are more specific.
이 글에서 @Named는 @Component와 동일한 기능을 수행한다고 한다.
물론 @Component보단 @Controller, @Service, @Repository로
역할을 명확하게 구별해주는 게 좋다고 한다.

@Inject -> @Autowired

https://codevang.tistory.com/256

이 글에서도 @Inject와 @Autowired는 사실상 거의 동일한 기능을 수행하고 있었다.
그래서 바꿔주었다.

수정코드

package ...

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
class BusinessService{
    private DataService dataService;
    @Autowired
    public void setDataService(DataService dataService) {
        System.out.println("Setter Injection");
        this.dataService = dataService;
    }

    public DataService getDataService() {
        return dataService;
    }
}
@Component
class DataService{}

@Configuration
@ComponentScan
public class CDIApplication {
    public static void main(String[] args) {
        try(var context = new AnnotationConfigApplicationContext(com.penloo.learn4_4.CDIApplication.class)){
            Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
            System.out.println(context.getBean(BusinessService.class).getDataService());
        }
        catch (Exception exception){
        }
    }
}
profile
매일 1%씩 성장하려는 주니어 개발자입니다.

0개의 댓글