简体   繁体   English

如何将spring bean集成从XML转换为基于Java注释的Config

[英]How to convert spring bean integration From XML to Java annotation based Config

I have this xml based configuration. 我有这个基于xml的配置。 But in my project I want to use java annotation based configuration. 但在我的项目中,我想使用基于java注释的配置。 How to do the conversion? 怎么做转换?

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="mail.csonth.gov.uk"/>
    </bean>

    <bean id="registrationService" class="com.foo.SimpleRegistrationService">
        <property name="mailSender" ref="mailSender"/>
        <property name="velocityEngine" ref="velocityEngine"/>
    </bean>

    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <value>
                resource.loader=class
                class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            </value>
        </property>
    </bean>

</beans>

Create a class annotated with @Configuration ( org.springframework.context.annotation.Configuration ) and for each bean declaration in your XML file create a @Bean ( org.springframework.context.annotation.Bean ) method within this class. 创建一个使用@Configurationorg.springframework.context.annotation.Configuration )注释的类,并为XML文件中的每个bean声明创建@Beanorg.springframework.context.annotation.Bean )方法。

@Configuration
public class MyConfiguration {

    @Bean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("mail.csonth.gov.uk");
        return mailSender;
    }

    @Bean
    public SimpleRegistrationService registrationService(JavaMailSenderImpl mailSender, VelocityEngineFactoryBean velocityEngine) {
        SimpleRegistrationService registrationService = new SimpleRegistrationService();
        registrationService.setMailSender(mailSender);
        registrationService.setVelocityEngine(velocityEngine); 
        return registrationService; 
    }

    @Bean
    public VelocityEngineFactoryBean velocityEngine() {
        VelocityEngineFactoryBean velocityEngine = new VelocityEngineFactoryBean();
        Properties velocityProperties = new Properties();
        velocityProperties.put("resource.loader", "class");
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.setVelocityProperties(velocityProperties);
        return velocityEngine;
    }
}

This example assumes that the mailSender and velocityEngine beans are required elsewhere in your application config since this is implied by the XML config you supplied. 此示例假定在应用程序配置中的其他位置需要mailSendervelocityEngine bean,因为您提供的XML配置暗示了这一点。 If this is not the case ie if the mailSender and velocityEngine beans are only required to construct the registrationService bean then you do not need to declare the mailSender() and velocityEngine() methods as public nor do you need to annotate then with @Bean . 如果不是这种情况,即如果mailSendervelocityEngine bean 需要构造registrationService bean,那么您不需要将mailSender()velocityEngine()方法声明为public也不需要使用@Bean进行注释。

You can instruct Spring to read this configuration class by 您可以指示Spring读取此配置类

  • Component scanning eg @ComponentScan("your.package.name") 组件扫描,例如@ComponentScan("your.package.name")
  • Registering the class with an AnnotationConfigApplicationContext eg 使用AnnotationConfigApplicationContext注册类,例如

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MyConfiguration.class); context.refresh(); 

Answer of @glitch was helpful but I got an error at below line. @glitch的答案很有帮助,但我在下面的行中收到错误。

velocityEngine.setVelocityProperties("resource.loader=class", "class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

How ever I fixed that. 我怎么解决这个问题。 Find below is the full implementation 以下是完整实施

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import com.vlclabs.adsops.service.SendEmailServiceImpl;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;

import java.util.Properties;

@Configuration
public class EmailConfiguration {

    @Bean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("mail.csonth.gov.uk");
        return mailSender;
    }

    @Bean
    public SendEmailServiceImpl registrationService(JavaMailSenderImpl mailSender, VelocityEngineFactoryBean velocityEngine) {
        SendEmailServiceImpl registrationService = new SendEmailServiceImpl();
        registrationService.setMailSender(mailSender);
        registrationService.setVelocityEngine(velocityEngine.getObject()); 
        return registrationService;
    }

    @Bean
    public VelocityEngineFactoryBean velocityEngine() {

        VelocityEngineFactoryBean velocityEngine = new VelocityEngineFactoryBean();
        Properties velocityProperties = new Properties();
        velocityProperties.put("resource.loader", "class");
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.setVelocityProperties(velocityProperties);

        return velocityEngine;
    }
}

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

相关问题 如何将Spring bean Oauth集成从XML转换为基于Java注释的Config - How to convert spring bean Oauth integration From XML to Java annotation based Config 如何将xml配置bean转换为java注释bean(spring boot) - How to convert an xml config bean to java annotation bean ( spring boot ) 如何将以下基于 xml 的 spring bean 转换为基于 Java 注释的 bean? - How do i convert the following xml based spring bean to java annotation based beans? Spring xml to Java config:如何转换嵌套的XML bean定义 - Spring xml to java config: how to convert nested xml bean definition 如何将基于Java的Bean配置转换为基于xml的配置 - How to convert java based bean configuration to xml based config 将 Spring Integration Cafe 演示从 xml 配置转换为 Java 8 DSL - Convert Spring Integration Cafe demo from xml config to Java 8 DSL 如何将xml bean转换为spring java bean - How to convert xml bean to spring java bean 如何将xml表示法转换为基于注释的表示法:Spring-Java - how to convert xml notation to annotation based notation : Spring - Java 如何将Java转换为Spring XML bean - How to convert java to Spring XML bean 如何将基于xml的spring-integration-email配置转换为基于Java的配置? - How to covert xml based spring-integration-email configuration to java based config?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM