gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:58:01 24 4
gpt4 key购买 nike

我有这个基于 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>

最佳答案

创建一个用 @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;
}
}

此示例假定您的应用程序配置中的其他地方需要 mailSendervelocityEngine bean,因为您提供的 XML 配置暗示了这一点。如果不是这种情况,即如果 mailSendervelocityEngine bean 需要构建 registrationService bean 那么您不需要将 mailSender()velocityEngine() 方法声明为公共(public)方法,也不需要使用 @Bean 进行注释。

你可以通过以下方式指示Spring读取这个配置类

  • 组件扫描,例如@ComponentScan("your.package.name")
  • 使用 AnnotationConfigApplicationContext 注册类,例如

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

关于java - 如何将 spring bean 集成从 XML 转换为基于 Java 注解的 Config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45880027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com