简体   繁体   English

是否可以在同一个Java Web应用程序中使用xml创建一些bean,并使用基于注释的方法创建其余bean?

[英]Is it possible to create some beans using xml and the remaining beans using annotation based approach in the same java web application

I have a web project in which I have beans injected using spring java annotations. 我有一个Web项目,其中使用Spring Java注释注入了bean。 Now in the same web project, I want to create few beans using xml based configuration. 现在在同一个Web项目中,我想使用基于xml的配置来创建几个bean。 (It will be hard for me to give here detail explanation, Why I want to do so). (我很难在这里提供详细说明,为什么要这样做)。 So to achieve this, I specified a ContextLoaderListener and contextConfigLocation in my web.xml . 为此,我在web.xml指定了ContextLoaderListenercontextConfigLocation After doing this, When I deployed my project war on server, I found that only those beans created using xml ( applicationContext.xml ) were getting created, Spring was not able to create and inject the beans created using annotations based approach. 完成此操作后,当我在服务器上部署项目战时,我发现仅创建了使用xml( applicationContext.xml )创建的那些bean,Spring无法创建和注入使用基于注释的方法创建的bean。

Is this type of use case possible to achieve ie creating some beans using annotations and some using applicationContext.xml for the same project. 是否可以实现这种用例类型,即为同一项目使用批注创建一些Bean,并使用applicationContext.xml创建某些用例? If yes, I would appreciate the help on the same. 如果是的话,我将不胜感激。

Thanks. 谢谢。

Try something like : 尝试类似的东西:

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 specified that your java class is a Configuration for Spring. @ Configuration指定您的java类是Spring的配置。 And @TmportResource allow these class to use beans defined in xml configuration files. @TmportResource允许这些类使用xml配置文件中定义的bean。

Yes, Possible. 是的,可能。

You need to add the below <context:component-scan /> tag in applicationContext.xml to recognize annotated spring beans and to get it instantiated. 您需要在applicationContext.xml添加以下<context:component-scan />标记,以识别带注释的spring Bean并将其实例化。

  • applicationContext.xml applicationContext.xml中

     <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- Annotated beans base package location to get it instantiated at the time of spring context startup --> <context:component-scan base-package="com.example.service.beans"/> <!-- normal bean configured in xml --> <bean id="userDao" class="com.example.dao.UserDao" /> </beans> 

    All the Annotated beans present under the above mentioned base-package will be instantiated along with the normal beans( <beans/> ) which you configured in xml file. 在上述基本软件包下存在的所有带注释的bean将与在xml文件中配置的普通bean( <beans/> )一起实例化。 Don't forget to add spring-context-<version>.xsd to your applicationContext.xml . 不要忘记将spring-context-<version>.xsdapplicationContext.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中的Spring配置-创建和使用2个相同类的bean,而不使用Autowired - Spring configuration in Java - Create and use 2 beans of same class NOT using Autowired 使用JAXB将XML解析为Java变量(beans) - parsing XML to java variables(beans) using JAXB 使用基于注释的配置创建延迟初始化的Spring bean - Creating lazily initialized Spring beans using annotation based configuration 使用@Qualifier 注释抛出“NoUniqueBeanDefinitionException”(发现多个相同类型的bean) - Using @Qualifier annotation throws "NoUniqueBeanDefinitionException" (found multiple beans of same type) 使用@Import 注释是否会创建导入文件中定义的所有 bean? - Does using @Import annotation create all beans defined in imported file? 如何将以下基于 xml 的 spring bean 转换为基于 Java 注释的 bean? - How do i convert the following xml based spring bean to java annotation based beans? 在JMH应用程序中使用Spring Bean - Using Spring Beans in a JMH Application 使用Spring @Configuration批注注入bean列表 - Inject a list of beans using Spring @Configuration annotation 使用注释配置bean的Spring安全配置 - Spring security configuration using annotation configured beans 使用注解配置引用多个 bean - Refering multiple beans using annotation configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM