我有一个其他Spring Boot项目使用的常见Spring Boot项目。所以,我有一个multi-module设置。
常见项目使用Java Mail,我想在公共项目中配置它。
如果
spring.mail.host
和相关库(由spring-boot-starter-mail定义)可用,则如果不存在则创建默认JavaMailSender
。
所以,这是我的library.yml
文件,
---
spring:
mail:
host: mobile.example.com
port: 25
这是我的gradle文件:
plugins {
id 'java'
id 'eclipse'
id 'maven-publish'
id 'io.spring.dependency-management' version '1.0.3.RELEASE'
}
repositories {
mavenCentral()
mavenLocal()
}
group = 'com.example.test'
version = '1.0.0'
jar {
baseName = 'boot-library-test'
version = '1.0.0'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-mail')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports { mavenBom('org.springframework.boot:spring-boot-
dependencies:1.5.4.RELEASE') }
}
所以,我认为我已经满足了spring创建JavaMailSender
的先决条件。正确?
我有这个配置类:
package com.example.library;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
@Configuration
public class LibraryApplicationConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer dataProperties() {
PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer = new
PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("library.yml"));
propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
}
这就是我希望Spring Boot连接JavaMailSender的地方:
package com.example.library.mail;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class Mail {
private final JavaMailSender javaMailSender;
public Mail(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
}
这是我的测试配置:
package com.example.library;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages="com.example.library")
public class LibraryTestConfiguration {
}
测试本身:
package com.example.library;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.library.mail.Mail;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=LibraryTestConfiguration.class)
public class LibraryTest {
@Autowired
private Mail mail;
@Test
public void contextLoads() {
assertThat(mail.getJavaMailSender()).isNotNull();
}
}
但是测试失败了
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'org.springframework.mail.javamail.JavaMailSender'类型的限定bean可用:预计至少有1个bean可以作为autowire候选者。依赖注释:{}
如果我将library.yml
更改为application.yml
(并删除PropertySourcesPlaceholderConfigurer
bean),则可行。 (我在这里有所有代码,所以如果你尝试的话,你应该得到相同的结果。)但是由于这是一个库,我不能将文件命名为application.yml
,因为它会与使用它的其他Spring Boot项目冲突图书馆。此外,我想使用yml文件而不是属性文件(因此我不能使用@PropertySource
)。
如何使用yml道具在库项目中创建Spring Boot并连接JavaMailSender
?