我正在使用intellij,Spring和Gradle。
我有一个发送电子邮件的课程。使用new运算符创建所述类。现在我read,可以使用@Configureable注释来自动装配这样的类。我似乎无法让它工作。也许有人对此有所了解并能指出我正确的方向?
@Service
@EnableAsync
@Configurable(dependencyCheck=true,autowire= Autowire.BY_TYPE)
public class EmailCommand implements ICommand {
/**
* standard logger.
*/
private final Logger log = LoggerFactory.getLogger(this.getClass());
/**
* {@link MailSender} capable of sending out mails.
*/
@Autowired
@Inject
private MailSender mailSender;
/**
* Sring with the Mailcontent.
* Taken from application.properties
*/
@Value("${spring.mail.content}")
private String mailContent;
/**
* Sring with the mailsubject.
* Taken from application.properties
*/
@Value("${spring.mail.subject}")
private String mailSubject;
/**
* The name of the sensor which triggered the alert.
*/
private String sensorName;
/**
* the email-address to send the mail to.
*/
private String emailAddress;
/**
* Standard constructor only used by Spring FW.
*/
public EmailCommand() {
}
/**
* Constructor used by the application.
*
* @param sensorName {@link EmailCommand#sensorName}
* @param emailAddress {@link EmailCommand#emailAddress}
*/
public EmailCommand(final String sensorName, final String emailAddress) {
this.sensorName = sensorName;
this.emailAddress = emailAddress;
}
@Override
public void execute() {
SimpleMailMessage message = new SimpleMailMessage();
String content = "asdf";//mailContent.replace("%SENSORNAME%", sensorName);
message.setText(content);
message.setSubject("asdf");
message.setTo(emailAddress);
try {
mailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
log.info("Sending Mail");
}
}
我正在使用new运算符创建该类:
return new EmailCommand(title, user.getEmail());
之后我得到NPE:
mailSender.send(message);
由于mailsender为空。
我的应用程序注释如下:
@SpringBootApplication(
exclude =
{org
.springframework
.boot
.autoconfigure
.security
.SecurityAutoConfiguration.class
})
@EnableLoadTimeWeaving(aspectjWeaving =
EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
@Configuration
@EnableSpringConfigured
public class WebApplication extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
WebApplication类位于Top-Level-Package中,所有其他类都位于该类的子包中。
我的Gradle文件:
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
maven { url "http://repo.spring.io/libs-snapshot" }
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-
plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'checkstyle'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url 'http://maven.springframework.org/release' }
maven { url 'http://maven.springframework.org/milestone' }
maven { url 'http://maven.springframework.org/snapshot' }
maven { url 'http://download.java.net/maven/2' }
}
war {
baseName = 'noisemap'
from '../noisemap-frontend/build'
}
test {
ignoreFailures = true;
}
configurations {
aspect
}
checkstyle {
toolVersion = '6.7'
ignoreFailures = true
}
dependencies {
//GMail
compile 'com.google.apis:google-api-services-oauth2:v1-rev132-1.22.0'
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.5'
compile 'com.google.apis:google-api-services-gmail:v1-rev65-1.22.0'
aspect 'org.springframework:spring-instrument:3.0.4.RELEASE'
compile configurations.aspect.dependencies
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
compile('org.springframework.boot:spring-boot-starter-cache')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-mail')
compile "org.springframework.social:spring-social-config:1.1.1.BUILD-SNAPSHOT"
compile "org.springframework.social:spring-social-core:1.1.1.BUILD-SNAPSHOT"
compile "org.springframework.social:spring-social-web:1.1.1.BUILD-SNAPSHOT"
compile 'org.springframework.security.oauth:spring-security-oauth2:2.1.0.RELEASE'
runtime('org.springframework.boot:spring-boot-devtools')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
testCompile('org.mockito:mockito-core:1.10.19')
testCompile('junit:junit:4.12')
testCompile('com.jayway.jsonpath:json-path:2.2.0')
}
bootRun {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "development"
jvmArgs = ["-javaagent:"+configurations.aspect.asPath]
}
答案 0 :(得分:0)
@Mauravan你是正确的创建一个bean并使用它与autowire。
但是你可以在Spring中有很多方法来创建bean。
1st:在春天,你使用Autowire在类中注入对象,而不是在CDI中使用Inject。
/**
* {@link MailSender} capable of sending out mails.
*/
@Autowired
private MailSender mailSender;
第二:您可以使用@Service或@Component注释创建bean并将其注入发送电子邮件的类中,除非
return new EmailCommand(title, user.getEmail());
第3名:毕竟你会拥有它
@Service
class EmailCommand {
@Autowired
private MailSender mailSender;
@Async
public void send(SimpleMailMessage message) {
this.mailSender.send(message);
}
}
这只是一个例子......