简体   繁体   English

如何将 bean 自动装配到 2 个不同的控制器中(Spring)

[英]How to autowire a bean into a 2 different controllers (Spring)

I am learning spring and i have a problem that i do not know how to solve.我正在学习 spring 并且我有一个我不知道如何解决的问题。

@Service
@Transactional
public class SchoolService {

   @Autowired
   private CourseDao courseDao;
   @Autowired
   private EducationDao educationDao;
   @Autowired
   private StudentDao studentDao;
   @Autowired
   private TeacherDao teacherDao;
   @Autowired
   private StatisticsDao statisticsDao;


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

This code is injecting my DAOS into this service class but then i need to inject the class above into two controllers.这段代码将我的 DAOS 注入到这个服务类中,但是我需要将上面的类注入到两个控制器中。 One way i have tried was with this code but that did not work.我尝试过的一种方法是使用此代码,但这不起作用。

    @Autowired
    SchoolService sm;

How would i inject it into my controller class.我如何将它注入我的控制器类。 I have tried making the controller class a @Component but nothing seems to work.我曾尝试将控制器类设为 @Component,但似乎没有任何效果。

ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext("application.xml");

SchoolService sm = container.getBean(SchoolService.class);

This way works but i do not want to create a new applicationcontext for each time i want to get that bean.这种方式有效,但我不想在每次我想获得那个 bean 时都创建一个新的应用程序上下文。

Yes i am using xml at the moment, please don't shoot me :D Thanks.是的,我目前正在使用 xml,请不要射击我 :D 谢谢。

尝试在application.xml文件中创建控制器 bean 而不是注释控制器。

Since its obviously an educational question, I'll try to provide a very detailed answer as much as I can:由于这显然是一个教育问题,我会尽量提供一个非常详细的答案:

Once basic thing about spring that all the auto-wiring magic happens only with beans that are managed by spring.曾经关于 spring 的所有自动装配魔法都发生在 spring 管理的 bean 上。

So:所以:

  • Your controllers must be managed by spring您的控制器必须由 spring 管理
  • Your service must be managed by spring您的服务必须由 spring 管理
  • Your DAOs must be managed by spring您的 DAO 必须在 spring 之前进行管理

Otherwise, autowiring won't work, I can't stress it more.否则,自动装配将不起作用,我不能再强调它了。

Now, Think about the Application Context as about the one global registry of all the beans.现在,将应用程序上下文视为所有 bean 的一个全局注册表。 By default the beans are singletons (singleton scope in terms of spring) which means that there is only one object (instance) of that bean "retained" in the Application Context.默认情况下,bean 是单例(就 spring 而言是单例范围),这意味着在应用程序上下文中“保留”了该 bean 的只有一个对象(实例)。

The act of autowiring means basically that the bean (managed by spring) - controller in your case has dependencies that spring can inject by looking in that global registry, getting the matching bean and setting to the data field on which the @Autowired annotation is called.自动装配的行为基本上意味着 bean(由 spring 管理)-在您的情况下,控制器具有 spring 可以通过查看全局注册表,获取匹配的 bean 并设置为调用@Autowired注释的数据字段来注入的依赖项.

So, if you have two controllers (again, both managed by spring), you can:所以,如果你有两个控制器(同样,都由 spring 管理),你可以:

@Controller
public class ControllerA {
   @Autowired
   private SchoolService sm;
}


@Controller
public class ControllerB {
   @Autowired
   private SchoolService sm;
}

In this case, the same instance of school service will be injected into two different controllers, so you should good to go.在这种情况下,学校服务的同一个实例将被注入到两个不同的控制器中,所以你应该很高兴。

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

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