Change Spring email options based on dev / test / prod mode

I often test my application with a copy of a live database, but I have to be careful not to take any action that causes the email to be sent to the user. I would like to have a way to configure spring, so when I am in dev or test mode, no email will be sent to real users. Ideally, I would like all emails to be sent to the user instead sent to a mailbox that I can check. And I don't want to change any code for this to happen, just the xml configuration files.

I already use PropertyPlaceholderConfigurer and read in different properties based files if I work in production, testing or dev mode. I am setting up JavaMailSenderImpl based on the values ​​in the properties file. I also use SimpleMailMessage to create a template with a From address.

Ideally, there would be a way to rewrite the TO address of all outgoing emails to a test account if I am running in dev or test mode.

My first thought was to use a different SMTP server for dev and test. But then I will also have to manage another mail server, and he will need to configure it so that it does not send mail anywhere, but instead sends it to one mailbox. I do not want to add additional management requirements, if possible.

This may be the best solution, but there seems to be a way to intercept emails and change the recipient.

Has anyone dealt with this issue before? What solutions did you come up with?

+5
source share
6 answers

Note: my answer is based on using Maven, so this is more about building than about anything, but using spring allows me to enter values ​​conveniently.

I use the -placeholder property and create beans as:

jdbc.properties:

db.url=hostname:1521/test
db.username=awesome
db.password=password

applicationContext.xml (using context namespace)

<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="yo" class="some.DataSource">
    <property name="url" value="${db.url}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
</bean>

Then I store the environments in separate folders with values ​​such as:

src/main/environments
 ++ prod
 +++++ jdbc.properties
 ++ dev
 +++++ jdbc.properties
 ++ cert
 +++++ jdbc.properties

Using maven profiles (from pom.xml):

<profile>
    <id>environment</id>
    <activation>
        <property>
            <name>environment</name>
        </property>
    </activation>
    <build>
        <resources>
            <resource>
                <directory>src/main/environments/${environment}</directory>
            </resource>
        </resources>

maven -Denviroment :

mvn clean -Denvironment=dev test
mvn clean -Denvironment=cert package
mvn clean -Denvironment=prod deploy

... src/main/resources.

+4

, , EnvironmentSelectingFactoryBean (Spring, , ...). FactoryBean, "" beans .

XML, , :

<bean id="mailSender" class="com.kizoom.spring.config.EnvironmentSelectingFactoryBean">
   <property name="beanMap">
      <map>
         <entry key="test">
            <bean class="org.springframework.mail.javamail.JavaMailSenderImpl">
               <property name="host" value="${mailhost.test}"/>
            </bean>
         </entry>
         <entry key="production">
            <bean class="org.springframework.mail.javamail.JavaMailSenderImpl">
               <property name="host" value="${mailhost.production}"/>
            </bean>
         </entry>
      </map>
   </property>
</bean>

, EnvironmentSelectingFactoryBean , "" "", bean beanMap FactoryBean.getObject(). beans beanMap , , , .

JavaMailSender bean.

+2

, , - . JavaMailSender. , - dev .

+1

mock-javamail ( ) , , - SMTP-, Fakemail.

+1

Property Place - System .

                 /WEB -INF/config/myapp.${myapp.env}.properties          

WEB-INF/conf myapp.prod.properties myapp.stag.properties myapp.test.properties

-Dmyapp.env = prod tomcat script.

+1

javax.mail.Session JNDI. - prod/ test dev. , .

dev * ** test javax.mail.Session.

github javaMail , :

  • text
  • mbox
  • .
0
source

All Articles