英文:
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("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.
答案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
// 接着有一些东西要自动装配到 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 -> 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;
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论