如何将 spring bean 集成从 XML 转换为基于 Java 注释的配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45880027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert spring bean integration From XML to Java annotation based Config
提问by sndu
I have this xml based configuration. But in my project I want to use java annotation based configuration. How to do the conversion?
我有这个基于 xml 的配置。但是在我的项目中,我想使用基于 Java 注释的配置。如何进行转换?
<?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>
回答by glytching
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
( org.springframework.context.annotation.Configuration
)注释的类,并为 XML 文件中的每个 bean 声明在这个类中创建一个@Bean
( org.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. If this is not the case i.e. if the mailSender
and velocityEngine
beans are onlyrequired 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
.
此示例假设您的应用程序配置中的其他地方需要mailSender
和velocityEngine
bean,因为您提供的 XML 配置暗示了这一点。如果不是这种情况,即如果只需要mailSender
and velocityEngine
beans来构造bean,那么您不需要将and方法声明为 public,也不需要用 注释 then 。registrationService
mailSender()
velocityEngine()
@Bean
You can instruct Spring to read this configuration class by
您可以通过以下方式指示 Spring 读取此配置类
- 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();
- 元件扫描例如
@ComponentScan("your.package.name")
使用
AnnotationConfigApplicationContext
eg注册课程AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MyConfiguration.class); context.refresh();
回答by sndu
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;
}
}