Heim  >  Fragen und Antworten  >  Hauptteil

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

PHPzPHPz2727 Tage vor671

Antworte allen(2)Ich werde antworten

  • 阿神

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

    在使用的时候,根据场景,需要使用 SqlSessionTemplate 默认构造器进行实例化,有时候需要SqlSessionTemplate 带参数的构筑器进行实例化。

    你的需求应该是在实例化 SqlSessionTemplate Bean 之前, 可以根据不同的需求来使用相应的构造器吧?
    那么可以使用Spring Java Config 的方式来配置 Bean, 例如:

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

    使用 Spring Java Config , 就可以根据条件控制如何生成 Bean 的.

    Antwort
    0
  • 黄舟

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

    我觉得可以写多个继承SqlSessionTemplate的类,然后用@Component("Your_Bean_Name")来指定不同实现的Bean名称,最后在注入的地方用@Qulifiler("The_Bean_Name")来指定要注入的Bean!

    Antwort
    0
  • StornierenAntwort