๐ขํ๋ก์ ํธ๋ฅผ ๋ง๋ค๊ณ ํ ์คํธ๋ฅผ ๋๋ฆฌ๋๋ฐ ํ ์คํธ ๊ณผ์ ์์ ๊ณ์ NoSuchBeanDefinition error๊ฐ ๋ฐ์ํด ๋นํฉํ์๋ค.
error์ ๋ด์ฉ์ MailService๊ฐ์ฒด๋ฅผ ์ฐพ์ ์ ์๋ค๋ ์๋ฌ์๋ค.
@Profile("dev")
@Service
@RequiredArgsConstructor
public class MailService {
private final JavaMailSender mailSender;
private String FROM_ADDRESS = "MY_ADDRESS";
public void sendMail(String email, String uuid) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(email);
message.setFrom(FROM_ADDRESS);
message.setSubject("localhost:8080 email ํ์ธ");
message.setText(uuid);
mailSender.send(message);
}
}
๐ฐ์์ ๊ฐ์ ์ฝ๋๊ฐ ๋ณธ๋ฌธ์ ์์๋๋ฐ ํ
์คํธ ๊ณผ์ ์์ MailService๋ฅผ ๊ณ์ ์ฐพ์ง ๋ชปํด Controller์์ Autowired๊ฐ ๋์ง ์์ ํ
์คํธ๊ฐ ๊ณ์ ํฐ์ก๋ค. ํด๊ฒฐ๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค. ๋ด๊ฐ ๋ง๋ Mailservice๋ฅผ ์ง์ ๋ฑ๋กํ๋ฉด ๋๋ค.
ํ
์คํธ ํด๋์ TestConfigํ์ผ์ ๋ง๋ค์ด ๋ฑ๋กํ๋ฉด ๋๋ค.
@Configuration
public class MailConfig {
@Bean
public MailService mailService() {
return new MailService(javaMailService());
}
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.naver.com");
javaMailSender.setUsername("์ด๋ฉ์ผ");
javaMailSender.setPassword("๋น๋ฐ๋ฒํธ");
javaMailSender.setPort(465);
javaMailSender.setJavaMailProperties(properties());
return javaMailSender;
}
private Properties properties() {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.debug", "true");
properties.setProperty("mail.smtp.ssl.trust","smtp.naver.com");
properties.setProperty("mail.smtp.ssl.enable","true");
return properties;
}
}
๐ซค์ฒซ ๋ฒ์งธ MailService๋ ๋ด๊ฐ ๋ง๋ ์๋น์ค๋ก ์ง์ ๋ฑ๋กํด์ฃผ๊ณ ํ์ํ ์ค์ ๋ค์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค. 2,3๋ฒ์งธ ๋น์ ํ์ํ ์ค์ ๋ค์ ์ค์ ํด์ฃผ๋ ๊ณผ์ ์ด๋ค.
์๋ ์ฐธ๊ณ ๋ธ๋ก๊ทธ๋ถ์ด ์๋์์ผ๋ฉด ๋ช ์๊ฐ์ ํค๋งธ์ ๊ฒ ๊ฐ์๋ค.๐
์ฐธ๊ณ ๋ธ๋ก๊ทธ: TIL -leesomyoung
์๋ ํ์ธ์. ์ ๋ ๋์ผํ ๋ฌธ์ ๋ฅผ ๊ฒช๊ณ ์๋๋ฐ ํน์ ๊ด๋ จ๋ ๋ ํฌ์งํ ๋ฆฌ ๋งํฌ๊ฐ ์์๊น์?
์๋๋ฉด ์คํ๋ง๋ถํธ ๋ฒ์ ๊ณผ ์์กด์ฑ(build.gradle)์ ์ด๋ค๊ฑธ ์ถ๊ฐํ์ จ๋์ง ์๊ณ ์ถ์ต๋๋ค.