Home  >  Q&A  >  body text

java - 多构造器的类,如何通过Spring 注解进行依赖注入,通过Spring管理Bean

PHPzPHPz2727 days ago669

reply all(2)I'll reply

  • 阿神

    阿神2017-04-18 10:08:22

    When using it, depending on the scenario, you need to use the SqlSessionTemplate default constructor for instantiation. Sometimes you need the SqlSessionTemplate constructor with parameters for instantiation.

    Your requirement should be that before instantiating the SqlSessionTemplate Bean, you can use the corresponding constructor according to different needs?
    Then you can use Spring Java Config to configure the Bean, for example:

    @Configuration
    public class AppConfig {
        @Bean
        public SqlSessionTemplate sqlSessionTemplate() {
            if (someCondition) {
                return new SqlSessionTemplate();
            } else {
                return new SqlSessionTemplate(args);
            }
        }
    }

    Using Spring Java Config, you can control how to generate beans based on conditions.

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:08:22

    I think you can write multiple inheritancesSqlSessionTemplate的类,然后用@Component("Your_Bean_Name")来指定不同实现的Bean名称,最后在注入的地方用@Qulifiler("The_Bean_Name") to specify the beans to be injected!

    reply
    0
  • Cancelreply