我有一个Web项目,其中使用Spring Java注释注入了bean。现在在同一个Web项目中,我想使用基于xml的配置来创建几个bean。 (我很难在这里提供详细的说明,为什么要这样做)。为此,我在ContextLoaderListener中指定了contextConfigLocationweb.xml。完成此操作后,当我在服务器上部署项目战时,我发现仅创建了使用xml(applicationContext.xml)创建的bean,Spring无法创建和注入使用基于注释的方法创建的bean。

是否可以使用这种类型的用例,即针对同一项目使用注释创建一些bean,使用applicationContext.xml创建一些bean。如果是,我将不胜感激。

谢谢。

最佳答案

尝试类似的东西:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Bean;


@Configuration
@ImportResource("spring-xml-configuration-file.xml")
public class ConfigClass {

    ...

     @Bean
     public Object bean1() {
        ...
    }
}


@ Configuration指定您的java类是Spring的配置。
@TmportResource允许这些类使用xml配置文件中定义的bean。

09-30 15:20
查看更多