java - 多构造器的类,如何通过Spring 注解进行依赖注入,通过Spring管理Bean
PHPz
PHPz 2017-04-18 10:06:22
0
2
667
PHPz
PHPz

学习是最好的投资!

reply all(2)
阿神

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.

黄舟

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!