简体   繁体   English

如何将这些类注入到我的Spring控制器类中?

[英]How are injected these classes into my Spring controller class?

I am pretty new in Spring and I have some doubts about how is injected some classes into a controller class. 我在Spring刚起步,对如何将某些类注入控制器类存有疑问。

Into my project I have this HomeController class: 进入我的项目,我有这个HomeController类:

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    @Autowired
    private MessageSource  messageSource;
    @Autowired
    private Environment env;

    .....................................................
    .....................................................
    .....................................................
}

My doubt is related to the 2 objects MessageSource messageSource and Environment env classes. 我的怀疑与2个对象MessageSource messageSourceEnvironment env类有关。

As you can see these classes are injected by the @Autowired annotation. 如您所见,这些类是通过@Autowired注释注入的。

The problem is that I have not bean definition into my XML configuration for these classes. 问题是我没有为这些类在XML配置中定义bean。 So why are correctly injected? 那么为什么要正确注射呢? Where are the definition of these bean? 这些bean的定义在哪里?

Tnx TNX

Spring mappings can be done with XML or with annotations . Spring映射可以使用XML注释来完成。

In your case, if no XML defined, your MessageSource and Environment classes should be mapped by Spring annotations like @Component @Service or @Resource : 在您的情况下,如果未定义XML,则应使用Spring批注(@Component @Service@Resource映射您的MessageSourceEnvironment类:

@Component

Indicates that an a nnotated class is a "component". 指示带注释的类是 “组件”。 Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. 当使用基于注释的配置和类路径扫描时,此类会被视为自动检测的候选

@Autowired

@Autowired annotation will try to find a bean of type Foo in the spring context and will then inject the same. @Autowired注解将尝试在春季上下文中找到Foo类型的bean,然后将其注入。

@Resource

Similar to this is @Resource annotation that will try to find the bean with the name "foo". 与此类似的是@Resource批注,它将尝试查找名称为“ foo”的bean。 To summarize, @Autowired wires by type and @Resource wires by name. 总而言之,@ Autowired导线按类型,@ Resource导线按名称。

Automatic discovery of beans is based on the following rules: Bean的自动发现基于以下规则:

1) Use context:annotation-config tag in spring-config.xml to let Spring use Annotations 1)在spring-config.xml中使用context:annotation-config标签让Spring使用Annotations
2) Use context:component-scan tag in spring-config.xml and tell Spring the package in which to look for auto-discovering beans 2)在spring-config.xml中使用context:component-scan标记,并告诉Spring在其中寻找自动发现bean的包
3) Use @Component annotation to mark a class as a Spring auto-discoverable bean 3)使用@Component批注将类标记为Spring自动发现的bean

If @Component annotation is used, then the bean declarations need not be declared in spring-config.xml 如果使用@Component annotation ,则不需要在spring-config.xml中声明Bean声明。

Both the Environment and the MessageSource are closely tied to the inner workings of Spring Framework. EnvironmentMessageSource都与Spring Framework的内部工作紧密相关。

The environment is part of the application context and will be available for autowiring. 该环境是应用程序上下文的一部分,将可用于自动装配。

The ApplicationContext interface extends the MessageSource interface and will be available for autowiring as a message source, even if you have not defined your own message source bean. ApplicationContext接口扩展了MessageSource接口,即使您尚未定义自己的消息源Bean,也可以将其作为消息源进行自动装配。 (If you define your own message source, the application context will delegate to that ) (如果您定义自己的消息源,则应用程序上下文将委托给该消息源)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM