不需要。

huangapple go评论66阅读模式
英文:

Should I annotate pojo class with @Component?

问题

我搜索了一下是否应该用 @Component 注释 POJO。看起来建议不要注释 pojo。

这是我的 OrderStatusMnemonic 配置类,用于读取一个 txt 文件:

@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;
}

由于我自动装配了 OrderStatus POJO 类,所以我得到了错误:

考虑在您的配置中定义一个类型为 'com.spectrum.sci.osm.orderstatus.OrderStatus' 的 bean。

英文:

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(&quot;OrderStatusMnemonic.class&quot;);
	
	private ResourceLoader resourceLoader;
	
	@Autowired
	public OrderStatus orderStatus;

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

OrderStatus POJO:

@Getter
@Setter
@ToString
public class OrderStatus {
	private List&lt;String&gt; 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.

答案1

得分: 2

@Component或@Autowired应该仅用于由Spring管理的类。POJOs不由Spring管理。因此,既不应添加@Component,也不应自动装配它。由于您正在尝试自动装配POJO类,所以出现了定义类型为OrderStatus的bean的错误。

英文:

@Component or @Autowired should be used only for classes managed by Spring. POJOs are not managed by Spring. So, should neither add @component nor you should autowire it. Since you are trying to autowire POJO class, you are getting error to define bean of type OrderStatus

答案2

得分: 2

Your OrderStatus 目前不需要添加 @Component 注解,所以不应该添加它。而且你不应该在没有 @Component 的情况下尝试在任何地方使用 @Autowire

当然,你可以添加 @Component,然后在需要的地方使用 @Autowire,但这没有必要,因为你可以更容易地通过 new OrderStatus() 来实例化你的 POJO。而且这也可能会浪费资源。

那么,什么时候需要这两个注解呢?当你的 POJO 需要成为一个受管理的 Bean 时。换句话说,当 Spring 需要执行一些自动化操作时。考虑一下,如果你的 POJO 更复杂,比如(查看注释):

// 启用自动装配 OrderStatus -> 自动装配的 OrderStatus 由 Spring 管理
@Component
public class OrderStatus {
private List values;
// 接着有一些东西要自动装配到 OrderStatus
// 如果 OrderStatus 不由 Spring 管理,这将被忽略!
// 但因为它受管理,Spring 也会自动装配这个
// 当然,SomeOtherManagedBean 必须是一个 @Component,例如
@Autowired
private SomeOtherManagedBean somb;
}

英文:

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 -&gt; autowired OrderStatus is managed 
// by Spring
@Component 
public class OrderStatus {
    private List&lt;String&gt; 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;
}

答案3

得分: 0

Any plain-vanilla Java Class which is being used as light weight bean and treated/called as POJO should not be bound with any of the Spring components.

The java class can be decorated with either of the spring annotations (i.e. @Component, @Service, @Bean, @Configuration) only if that class has been enhanced by adding extra flavor and only if that class is being used to support other classes with enhanced features other than getter setter properties.

@Autowired can be used only if the class is marked with any of the spring components as the @Autowired is spring annotation which would scan only those beans which are being managed within Spring.

英文:

Any plain-vanilla Java Class which is being used as light weight bean and treated/called as POJO should not be bound with any of the Spring components.

The java class can be decorated with either of the spring annotations (i.e. @Component, @Service, @Bean, @Configuration) only if that class has been enhanced by adding extra flavour and only if that class is being used to support other classes with enhanced features other then getter setter properties.

@Autowired can be used only if the class is marked with any of the spring components as the @Autowired is spring annotation which would scan only those beans which are being managed withing Spring.

huangapple
  • 本文由 发表于 2020年8月7日 01:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63289013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定