简体   繁体   English

澄清在春季使用自动装配注释

[英]Clarification on using autowire annotation in spring

I am new to spring and i have been reading spring online reference as well. 我是Spring的新手,我也一直在阅读Spring在线参考。 I have a question regarding using autowire annotation, basically i have a web application that has for example UserController.java , UserManagerService.java and UserDao.java . 我有一个关于使用自动装配注释的问题,基本上我有一个Web应用程序,例如具有UserController.javaUserManagerService.javaUserDao.java Please find pseudocode below: 请在下面找到伪代码:

 @Controller
   public class UserController
    {
    @autowire UserManagerService userManagerService;

    public String validateUser(String userpswrd,String username){
           return userManagerService.validateUser(userpswrd,username);
    }

    }

And the UserManagerServiceImpl: 和UserManagerServiceImpl:

 @Service
public class UserManagerServiceImpl{

@autowire UserDao userDao;

public String validateUser(String userpswrd,String username){
           return userDao.validateUser(userpswrd,username);
    }

I know i need to put the the following in my applicationContext.xml in order to work: 我知道我需要将以下内容放入我的applicationContext.xml中才能工作:

<context:component-scan base-package="com.*" />
<!-- This tells Spring to activate annotation-driven transactions -->
<tx:annotation-driven />

My question is do i need to define the UserDao and UserManagerService in the applicationContext.xml file as a bean in order for the autowire annotation UserManagerService and UserDao to work ? 我的问题是我是否需要将applicationContext.xml文件中的UserDao和UserManagerService定义为bean,以便自动装配注释UserManagerServiceUserDao起作用?

Thanks in advance for the help 先谢谢您的帮助

No... the component scanning will detect all @Component related beans (including `@Service, @Repository, @Controller etc.). 否...组件扫描将检测所有与@Component相关的bean(包括`@ Service,@ Repository,@ Controller等)。 So no you don't need to explicitly define the beans, as that would defy the need for component-scanning and automatic wiring. 因此,不需要,您无需显式定义bean,因为这将消除对组件扫描和自动接线的需求。

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

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