Skip to content

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:

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

    public OrderStatusMnemonic(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    public void init() {
        try {
  "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) {
      "str = " + str);
  "orderStatusMnemonicList = " + orderStatusMnemonicList.toString());
        } catch (IOException | NullPointerException e) {
            log.error("Failing to Load order status mnemonic file" + e.getMessage(), e);

OrderStatus POJO:

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.



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
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
    private SomeOtherManagedBean somb;