简体   繁体   English

Spring MVC,我的bean如何在控制器中自动接线?

[英]spring mvc, how do my beans get autowired in controller?

I'm new to spring and spring mvc. 我是Spring和Spring MVC的新手。 I have configured a dataSource and mvc by follwing a video tutorial. 我已经按照视频教程配置了dataSource和mvc。 I have added a context file offers-servlet.xml in the WEB-INF where i'm scanning my controllers package where my controller.java is located. 我添加了一个上下文文件offers-servlet.xmlWEB-INF在那里我扫描我的controllers封装在我的controller.java所在。 i created another context service-context.xml where i'm component-scanning service.java classes. 我创建了另一个上下文service-context.xml ,其中我是组件扫描service.java类。 i'm creating the instance of service.java in my controller.java with setService method which i annotated with @Autowired . 我正在用setService方法在我的controller.java创建service.java的实例, setService方法用@Autowired注释。 And my doubt here is how the service.java is getting autowired in my controller.java where each of these are in different packages and different context.xml files with different component-scan . 我的疑问是, service.java如何自动连接到我的controller.java ,其中的每一个都位于不同的包中,并且具有不同的component-scan的不同context.xml文件。

This link here explains Component scanning in the case of Annotations. 这里的链接说明了注释情况下的组件扫描。 In your case you have configured component scanning through XML. 在您的情况下,您已经配置了通过XML进行组件扫描。 Every XML definition in spring has its corresponding annotation definition. Spring中的每个XML定义都有其对应的注释定义。 Although this link exmplains the case when component scan is configured through annotations, it is completly valid in your case as well: 尽管此链接示例了通过注释配置组件扫描的情况,但在您的情况下它也完全有效:

How to understand Spring @ComponentScan 如何了解Spring @ComponentScan

The fact that the beans are located in different packages is not important, if they are known to the spring container. 如果弹簧容器知道这些豆位于不同包装中的事实并不重要。 By enabling the component scan in both packages, you make available all beans in both packages globally in the current execution context of the application. 通过在两个软件包中启用组件扫描,可以在应用程序的当前执行上下文中全局使两个软件包中的所有bean可用。

You can think of spring IoC as a global map, that given a (previously registered) key it can give you the instance related to that key. 您可以将spring IoC视为一个全局映射,给定一个(先前注册的)密钥,它可以为您提供与该密钥相关的实例。

When you mentioned component-scan packages, it indicates the spring-container to find the annotated classes in those mentioned package and then beans (objects of classes which would have @Component or related annotation like @Service, @Controller, @Repository) would be managed (whole life-cycle from creation to destroy) by Spring. 当您提到组件扫描程序包时,它指示弹簧容器在那些提到的程序包中找到带注释的类,然后将Bean(具有@Component或相关注释(如@ Service,@ Controller,@ Repository)的类的对象)由Spring管理(从创建到破坏的整个生命周期)。 So, in your application, whenever you would need one such bean, rather than creating, it would be provided by container. 因此,在您的应用程序中,只要您需要一个这样的bean(而不是创建一个),它将由容器提供。 On the other hand, if you wouldn't have specified 'component-scan', You will have to identify your classes (using either xml or javaConfig) whose beans you want Spring-container to manage. 另一方面,如果您未指定'component-scan',则必须标识要使用Spring容器管理其bean的类(使用xml或javaConfig)。 Now this part was identifying which beans would be managed by Spring-container. 现在,这一部分正在确定哪些容器将由Spring容器管理。

Next comes wiring these beans where it is needed. 接下来是在需要的地方布线这些bean。 To do so, you mentioned @Autowired on setter method. 为此,您在设置方法上提到了@Autowired。 As Spring-container encounters this annotation, it will look for that bean (service bean in your case) in container. 当Spring容器遇到此注释时,它将在容器中查找该bean(在您的情况下为service bean)。 Since, that bean is managed by container (from previous step) and is available when needed (in controller class in your case), container will provide (auto-wire) service bean into the setter method. 由于该bean由容器管理(来自上一步),并且在需要时可用(在您的情况下为控制器类),因此容器会将服务bean提供(自动装配)到setter方法中。

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

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