如何将一个 Bean 自动注入到两个不同的控制器中(Spring)

huangapple 未分类评论69阅读模式
标题翻译

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

问题

我正在学习 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;

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

这段代码将我的 DAOs 注入到了这个服务类中然后我需要将上面的类注入到两个控制器中
我尝试过的一种方式是使用这段代码但是没有成功

@Autowired
SchoolService sm;


我应该如何将它注入到我的控制器类中呢?我尝试过将控制器类标记为 @Component,但似乎没有任何作用。

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

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


这种方式可以工作,但我不想每次都为了获取这个 bean 而创建一个新的 ApplicationContext。

是的,我目前正在使用 XML 配置,拜托不要开枪打我 :D
谢谢。
英文翻译

I am learning spring and i have a problem that i do not know how to solve.

@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.
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.

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.

Yes i am using xml at the moment, please don't shoot me 如何将一个 Bean 自动注入到两个不同的控制器中(Spring)
Thanks.

答案1

得分: 0

尝试在 application.xml 文件中创建控制器 Bean,而不是在控制器上添加注解。

英文翻译

Try creating the controller bean in the application.xml file instead of annotating the controller.

答案2

得分: 0

由于这显然是一个教育性问题,我将尽量提供详细的答案:

关于 Spring 的一个基本概念是,所有自动装配的魔法只发生在由 Spring 管理的 bean 上。

所以:

  • 你的控制器必须由 Spring 管理
  • 你的服务(service)必须由 Spring 管理
  • 你的数据访问对象(DAOs)必须由 Spring 管理

否则,自动装配将不起作用,我无法再强调这一点了。

现在,想象一下应用程序上下文(Application Context)就像是所有 bean 的全局注册表。默认情况下,这些 bean 是单例的(在 Spring 的术语中是 singleton 作用域),这意味着在应用程序上下文中只有一个对象(实例)保留着那个 bean。

自动装配的行为基本上意味着由 Spring 管理的 bean,例如你的控制器,具有依赖关系,Spring 可以通过查找全局注册表,获取匹配的 bean,并将其设置到被标有 @Autowired 注解的数据字段上。

因此,如果你有两个控制器(同样都由 Spring 管理),你可以这样:

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


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

在这种情况下,相同的实例 的学校服务(SchoolService)将被注入到两个不同的控制器中,这样你就可以继续进行。

英文翻译

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.

So:

  • Your controllers must be managed by spring
  • Your service must be managed by spring
  • Your DAOs must be managed by 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. 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.

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.

So, if you have two controllers (again, both managed by spring), you can:

@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.

huangapple
  • 本文由 发表于 2020年1月30日 19:38:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/59985199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定