问题
Using Grails 3 it's impossible to get an instance of mailService object, DI is not working:
build.gradle
compile "org.grails.plugins:mail:1.0.7"
testCompile "org.grails.plugins:mail:1.0.7"
application.groovy
environments {
development {
//grails.logging.jul.usebridge = true
grails.plugin.springsecurity.debug.useFilter = true
grails {
mail {
host = "main.mydomain.com"
port = 25
username = "login"
password = "password"
props = ["mail.smtp.auth":"true"]
}
}
grails.mail.default.from="noreply@mydomain.com"
}
production {
grails.logging.jul.usebridge = false
}
}
testController:
import groovy.xml.MarkupBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.security.access.annotation.Secured
@Secured(["hasRole('PERM_LOGIN')"])
class TestLogController {
def Logger logger = LoggerFactory.getLogger(this.getClass())
def mailService
def index() {
logger.info("Hello");
mailService.sendMail {
to "user@daomain.com"
subject "Hello Fred"
text "How are you?"
}
}
}
The following error occurs:
Caused by NullPointerException: Cannot invoke method sendMail() on null object
->> 18 | index in TestLogController.groovy
So mailService has not been injected properly to cotroller class or to integration test:
import grails.test.mixin.integration.Integration
import grails.util.Holders
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import spock.lang.Shared
import spock.lang.Specification
@Integration
class SendMailSpec extends Specification {
@Shared Logger logger
@Shared def mailService
def setup() {
logger = LoggerFactory.getLogger(this.getClass())
ApplicationContext ctx = Holders.grailsApplication.mainContext
mailService = ctx.getBean("mailService");
}
def cleanup() {
}
void "test mailService is not null"() {
expect:
mailService !=null
}
void "test send mail"() {
expect:"mail send"
mailService.sendMail {
to "user@domain.com"
subject "Hello Fred"
text "How are you?"
}
}
}
What is a problem ??
UPDATE: It was wrong mail plugin version, this one works fine:
compile "org.grails.plugins:mail:2.0.0.RC2"
回答1:
here's the current version (at the moment of writing this) to install :
compile 'org.grails.plugins:mail:2.0.0.RC6'
Here's the link to the main plugin (I don't know why google shows only the old versions) :
https://grails.org/plugins.html#plugin/mail
I hope this helps someone
来源:https://stackoverflow.com/questions/32602032/grails-3-mail-plugin-not-working