2017-07-29 60 views
0

我正在使用intellij,Spring和Gradle。獲得@Configurable工作

我有一個類發送電子郵件。所述類是使用新的操作符創建的。現在我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的類是頂級封裝,所有其他類都在這個類的子包。

我的搖籃 - 文件:

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

@Mauravan你是對有關創建一個bean,並與自動裝配使用。

但是你可以有很多方法在Spring中創建bean。

第一:在春天你使用Autowire注入對象而不是使用我在CDI中使用的注入。

/** 
* {@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); 
    } 
} 

這只是一個例子...

+0

嗨bpedroso,感謝您的回答。我不確定我是否明白你的意思。班本身正在工作,我能夠實例化它。問題是,春天的上下文似乎沒有選擇類並自動裝載屬性。當我通過autowirecapablebeanfactory手動autowire類時,一切正常。我只是想擺脫工廠。 – Mauravan