2017-08-25 105 views
1

我有這個基於xml的配置。但在我的項目中,我想使用基於java註釋的配置。如何做轉換?如何轉換spring bean集成從XML到基於Java的註釋Config

<?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> 
+0

您是否正在使用基於Annotation的配置? – sForSujit

+0

是的。像這樣,@Bean public EntityManagerFactory entityManagerFactory(){HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); etc – sndu

回答

2

創建@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; 
    } 
} 

此示例假設mailSendervelocityEngine豆在應用程序中配置的其他地方需要的,因爲這是由您提供的XML配置暗示。如果不是這種情況下,即如果mailSendervelocityEngine構建registrationService豆那麼你並不需要聲明mailSender()velocityEngine()方法爲公衆也不需要與@Bean然後標註要求。

可以指示春天通過

  • 組件掃描例如閱讀本配置類@ComponentScan("your.package.name")
  • 使用AnnotationConfigApplicationContext(例如,

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

是的,對於所有其他用戶定義的類,您可以使用@Autowired將bean注入到彈簧容器中 – sForSujit

+0

我需要導入什麼?像這樣org.springframework.context.annotation – sndu

+0

添加Spring 3.0 jar或更高版本 – sForSujit

1

@glitch的答案是有益的,但我在下面一行得到了一個錯誤。

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

我該如何解決這個問題。下面是完整的實施

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; 
    } 
}