Skip to content
Advertisement

Should I annotate pojo class with @Component?

I searched whether I should annotate POJO with @Component or not. It seems like it is recommended not to annotate pojo.

Here is my OrderStatusMnemonic Configuration class that reads a txt file:

@Configuration
public class OrderStatusMnemonic {
    private static final Logger log = LoggerFactory.getLogger("OrderStatusMnemonic.class");
    
    private ResourceLoader resourceLoader;
    
    @Autowired
    public OrderStatus orderStatus;

    public OrderStatusMnemonic(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    
    @PostConstruct
    public void init() {
        try {
            log.info("Loading order-status-mnemonic file ");
            Resource resource = resourceLoader.getResource("classpath:order-status-mnemonic.txt");
            InputStream inputStream = resource.getInputStream();
            
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            
            String str;
            List<String> orderStatusMnemonicList = new ArrayList<>();
            while ( (str = bufferedReader.readLine()) != null) {
                log.info("str = " + str);
                            
                orderStatusMnemonicList.add(str);
            }
            
            orderStatus.setValues(orderStatusMnemonicList);
            
            log.info("orderStatusMnemonicList = " + orderStatusMnemonicList.toString());
            
            
        } catch (IOException | NullPointerException e) {
            log.error("Failing to Load order status mnemonic file" + e.getMessage(), e);
        }
    }
        
}

OrderStatus POJO:

@Getter
@Setter
@ToString
public class OrderStatus {
    private List<String> values;
}

Since I am autowiring OrderStatus POJO class I am getting error:

Consider defining a bean of type ‘com.spectrum.sci.osm.orderstatus.OrderStatus’ in your configuration.

Advertisement

Answer

Your OrderStatus as it is now does not need annotation @Component so you should not add it. Also you should not try to @Autowire it anywhere without @Component.

You surely can add @Component and then @Autowire it anywhere you need it but there is no point in it since you can more easy instantiate your POJO by just issuing new OrderStatus(). And it might also be a waste of resources.

Then, when do you need those two annotations? Whenever your POJO needs to become a managed bean. In other words when there is a need for Spring to do some automagical things. Consider your POJO would have something more complex, like (check the comments):

// Enables autowiring OrderStatus -> autowired OrderStatus is managed 
// by Spring
@Component 
public class OrderStatus {
    private List<String> values;
    // Then there is something to autowire to OrderStatus also
    // Without OrderStatus being managed by Spring this would be ignored!
    // But because managed, Spring autowires also this one
    // Of course SomeOtherManagedBean must be a @Component, for example
    @Autowired
    private SomeOtherManagedBean somb;
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement