Spring在bean配置文件中定义电子邮件模板
File : MailMail.java
package com.yiibai.common; import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage; public class MailMail
{
private MailSender mailSender;
private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
} public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
} public void sendMail(String dear, String content) { SimpleMailMessage message = new SimpleMailMessage(simpleMailMessage); message.setText(String.format(
simpleMailMessage.getText(), dear, content)); mailSender.send(message); }
}
2. Bean的配置文件
File : Spring-Mail.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="username" />
<property name="password" value="password" /> <property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean> <bean id="mailMail" class="com.yiibai.common.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean> <bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="from@no-spam.com" />
<property name="to" value="to@no-spam.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean> </beans>
4. 运行它
package com.yiibai.common; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); MailMail mm = (MailMail) context.getBean("mailMail");
mm.sendMail("Yiibai", "This is text content"); }
}
输出
Dear Yiibai,
Mail Content : This is text content
Spring在bean配置文件中定义电子邮件模板的更多相关文章
- 在Spring的Bean注入中,即使你私有化构造函数,默认他还是会去调用你的私有构造函数去实例化
在Spring的Bean注入中,即使你私有化构造函数,默认他还是会去调用你的私有构造函数去实例化. 如果我们想保证实例的单一性,就要在定义<bean>时加上factory-method=” ...
- 在Spring框架中bean配置文件中constructor-arg标签中没有name元素?
bean配置文件出现错误的依赖: <beans <beans xmlns="http://www.springframework.org/schema/beans" ...
- 从基础知识到重写Spring的Bean工厂中学习java的工厂模式
1.静态工厂模式其他对象不能直接通过new得到某个类,而是通过调用getInstance()方法得到该类的对象这样,就可以控制类的产生过程.顺带提一下单例模式和多例模式: 单例模式是指控制其他对象获 ...
- Spring的xml配置文件中约束的必要性 找不到元素 'beans' 的声明
今天在复习Spring MVC框架的时候,只知道xml配置文件中的约束有规范书写格式的作用,所以在配置HandlerMapping对象信息的时候没有加入约束信息之后进行测试,没有遇到问题.后来在配置S ...
- spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)
一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2, 封装到Component类中再调用 3, 用Environment类从代码中直接访问 生 ...
- 如何在WPF中定义窗体模板
参考网址:https://www.cnblogs.com/chenxizhang/archive/2010/01/10/1643676.html可以在app.xaml中定义一个ControlTempl ...
- Mybatis在非spring环境下配置文件中使用外部数据源(druidDatasource)
Spring环境下, MyBatis可以通过其本身的增强mybatis-spring提供的org.mybatis.spring.SqlSessionFactoryBean来注入第三方DataSourc ...
- 在controller中无法通过注解@Value获取到配置文件中定义的值
1. 默认的我们通常只在dao层用到jdbc的配置,然后使用到@Value注解获取到值. 这时候会在spring-dao扫描中加入下面配置 <context:property-placehold ...
- 解决spring中不同配置文件中存在name或者id相同的bean可能引起的问题
小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...
随机推荐
- ssh使两台机器建立连接
ssh利用口令建立连接过程: 客户端--> 发送连接请求 --> 远程主机 --> 返回远程主机的公钥 --> 公钥加密客户端私钥+客户端公钥返回远程主机 --> 远程主 ...
- Percona XtraDB Cluster(PXC) -集群环境安装
Percona XtraDB Cluster(PXC) ---服务安装篇 1.测试环境搭建: Ip 角色 OS PXC-version 172.16.40.201 Node1 Redhat/C ...
- 应用nslookup命令查看A记录、MX记录、CNAME记录和NS记录
https://blog.csdn.net/qq_38058202/article/details/80468688
- SQL Server Connection Pooling (ADO.NET)
SQL Server Connection Pooling (ADO.NET) Connecting to a database server typically consists of severa ...
- 第三方登陆微博、qq、微信
源文:http://blog.csdn.net/tivonalh/article/details/60954373 假设是已经申请完成各平台开发者账号. 先来简单的,微博和QQ 微博: 引入微博JS ...
- Codeforces Round #300 E - Demiurges Play Again
E - Demiurges Play Again 感觉这种类型的dp以前没遇到过... 不是很好想.. dp[u] 表示的是以u为子树进行游戏得到的值是第几大的. #include<bits/s ...
- centos7 时间同步
yum -y install ntp systemctl enable ntpd systemctl start ntpd ntpdate -u cn.pool.ntp.org
- 【笔试题】Java 继承知识点检测
笔试题 Java 继承知识点检测 Question 1 Output of following Java Program? class Base { public void show() { Syst ...
- HTML5 canvas绘制arcTo、translate和rotate的画法探索
arcTo(x1,y1,x2,y2,radius) ; 还要加上moveTo的点(x0,y0) ; 第一步:找到切点 过点 (x1,y1), (x0,y0)引射线与点(x1,y1),(x2,y2) ...
- docker-compose RabbitMQ与Nodejs接收端同时运行时的错误
首先讲一下背景: 我现在在开发的一个项目,需要运行RabbitMQ和Nodejs接收端(amqplib库),但是在Nodejs接收端运行时,无法连接至RabbitMQ端,经常提示说 connect E ...