2017-08-25 103 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; 
    } 
}