Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have this xml based configuration. But in my project I want to use java annotation based configuration. 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>
like image 870
sndu Avatar asked Aug 25 '17 11:08

sndu


People also ask

Does Spring core supports XML Java and annotation based configuration?

2. What is Spring Annotation Based Configuration? In Spring Framework annotation-based configuration instead of using XML for describing the bean wiring, you have the choice to move the bean configuration into component class. It is done by using annotations on the relevant class, method or the field declaration.

Can we use both annotation and XML based configuration?

Yes, you can use XML, annotations, or a mix.


2 Answers

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.

@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. If this is not the case i.e. 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.

You can instruct Spring to read this configuration class by

  • Component scanning e.g. @ComponentScan("your.package.name")
  • Registering the class with an AnnotationConfigApplicationContext e.g.

       AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
       context.register(MyConfiguration.class);
       context.refresh();
    
like image 89
glytching Avatar answered Oct 30 '22 18:10

glytching


Answer of @glitch was helpful but I got an error at below line.

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;
    }
}
like image 37
sndu Avatar answered Oct 30 '22 17:10

sndu