Quick Jasypt Spring 3 Tutorial

This is a quick tutorial on setting up Jasypt with Spring 3. You will need to be familiar with Maven and m2e to follow the steps below.

1. Create a new Maven Project using the Eclipse New Wizard. When prompted for the archetype, pick the spring-mvc-jpa-archetype. This will give us a running Spring MVC web application.

2. Enter com.teamextension for the groupId. Enter quickjasypt for the artifactId. Enter com.teamextension.quickjasypt for the package. Hit Finish.

3. Right click on the quickjasypt project and go to Maven -> Add Dependency. Enter the groupId org.jasypt and the artifactId jasypt. This adds the Jasypt library to our Spring MVC application.

4. Edit src/main/webapp/WEB-INF/spring/db.xml. Remove the existing placeHolderConfig bean and add the beans:

  1. EnvironmentStringPBEConfig
  2. StandardPBEStringEncryptor
  3. EncryptablePropertyPlaceholderConfigurer
<bean id="encryptorConfig" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
  	<property name="algorithm" value="PBEWithMD5AndDES" />
  	<property name="passwordEnvName" value="PASSWORD_ENV_VARIABLE" />
</bean>

<bean id="encryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
	<property name="config" ref="encryptorConfig" />
</bean>

<bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
	<constructor-arg ref="encryptor" />
	<property name="locations">
   		<list>
   			<value>/WEB-INF/classes/db.properties</value>
   		</list>
	</property>
</bean>

5. Open src/main/resources/db.properties and you will see the password is in plain text. Now let’s use Jasypt to encrypt our passwords.

6. Download and unzip the Jasypt distribution, then open a command line at at the bin directory. Encrypt the password by running

encrypt input="database password" password="master password" algorithm="PBEWITHMD5ANDDES"

7. Copy the output of the encrypt command and update the password in db.properties. Enclose the encrypted password with ENC().

db.username=sa
db.password=ENC(8XPVPCbKt51RZYG1gPydm1x2JE15wX3s)
db.driver=org.hsqldb:mem:app-db
db.dialect=org.hibernate.dialect.HSQLDialect

8. In the runtime/deployment environment, make sure an environment variable PASSWORD_ENV_VARIABLE=master password is set. This is the master password entered in the encrypt command. Note that the PASSWORD_ENV_VARIABLE is specified in the encryptorConfig bean in db.xml.

For more information, please visit the Jasypt with Spring Framework page.


Quick Spring AMQP Tutorial

Here’s a quick tutorial on how to get up and running with Spring AMQP 1.0.0.RELEASE. You will need to be familiar with Maven and m2e to follow the steps below.

1. Since we are going to create and run a Spring AMQP application, we need an AMQP implementation. For this tutorial, we will use RabbitMQ. Install RabbitMQ. Since RabbitMQ is written in Erlang, you will need to install Erlang as well. Run RabbitMQ and you should see the following.

Step 1, Figure 1

2. Create a new Maven in Eclipse using m2e with the maven-archetype-quickstart archetype.
Step 2, Figure 1

Step 2, Figure 2

3. Add the spring-context, spring-amqp, and spring-rabbit dependencies.

Step 3, Figure 1
Step 3, Figure 2

4. Create the Producer class.

package com.teamextension.quickamqp;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Producer {
	public static void main(String[] args) {
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
		AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
		amqpTemplate.convertAndSend("helloworld.queue", "Hello World");
	}
}

5. Create the Consumer class.

package com.teamextension.quickamqp;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Consumer {
	public static void main(String[] args) {
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
		AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
		System.out.println(amqpTemplate.receiveAndConvert("helloworld.queue"));
	}
}

6. Create the Spring applicationContext.xml.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/rabbit
		http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- A reference to the org.springframework.amqp.rabbit.connection.ConnectionFactory -->
	<rabbit:connection-factory id="connectionFactory"/>

	<!-- Creates a org.springframework.amqp.rabbit.core.RabbitTemplate for access to the broker -->
	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

	<!-- Creates a org.springframework.amqp.rabbit.core.RabbitAdmin	to manage exchanges, queues and bindings -->
	<rabbit:admin connection-factory="connectionFactory"/>

	<!-- Creates a queue for consumers to retrieve messages -->
	<rabbit:queue name="helloworld.queue"/>
</beans>

5. Run the Producer, then the Consumer. You should see Hello World printed out

Step 5, Figure 1

For more information, visit the Spring AMQP site. Spring AMQP examples can be found at GitHub.


Quick Quartz + Spring 3 Tutorial

This is a quick Quartz + Spring tutorial. You will need Eclipse and the Eclipse m2e Plugin to follow the steps below.

This post was originally set out to use Quartz 2.0.2. It seems Quartz 2 has changed things around and its no longer compatible with the Spring Quartz classes. The error I get is java.lang.InstantiationError: org.quartz.JobDetail. This is because JobDetail used to be a class and is now an interface. So we will fall back to Quartz 1.8.5 for this tutorial.

1. Go to Eclipse and create a new Maven project. Click “Create a simple project” and hit Next. On the next page, enter Group Id as “com.teamextension”, Artifact Id as “quickquartzspring” and Name as “quickquartzspring”.

2. Right click on the project and go to Maven -> Add Dependency. Enter “quartz-scheduler” in the search box and pick “org.quartz-scheduler” version 1.8.5.

Add Dependency

Also add the Spring 3 dependencies. Enter “org.springframework” in the search box and add the dependencies “spring-context”, “spring-context-support” and “spring-tx”, version 3.0.6.RELEASE.

We will also add SLF4J as a dependency so you can see the Quartz logs. Enter “slf4j-simple” in the search box and pick “org.slf4j slf4j-simple” version 1.6.1.

3. Create the Job class. The job simply prints the current time to System.out.

package com.teamextension.quickquartzspring;

import java.util.Date;

public class QuickQuartzSpringJob {
	public void execute() {
		System.out.println(new Date());
	}
}

Notice that this class has no Quartz or Spring dependencies.

4. Create the following applicationContext.xml file under src/resources.

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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-3.0.xsd"&gt;

	&lt;bean id="quickQuartzSpringJob" class="com.teamextension.quickquartzspring.QuickQuartzSpringJob" /&gt;

	&lt;!-- create the Job --&gt;
	&lt;bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;
		&lt;property name="targetObject" ref="quickQuartzSpringJob" /&gt;
		&lt;property name="targetMethod" value="execute" /&gt;
	&lt;/bean&gt;

	&lt;!-- create the Trigger with a Schedule that runs every 5 seconds --&gt;
	&lt;bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;
		&lt;property name="jobDetail" ref="jobDetail" /&gt;
		&lt;property name="cronExpression" value="0/5 * * * * ?" /&gt;
 	&lt;/bean&gt;

 	&lt;!-- schedule the Job --&gt;
	&lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;
		&lt;property name="triggers" ref="trigger" /&gt;
	&lt;/bean&gt;
&lt;/beans&gt;

The Spring ApplicationContext creates the job and trigger, and registers them with the Scheduler. The schedule in this example is set to every 5 seconds. This uses the simpler MethodInvokingJobDetailFactoryBean instead of org.springframework.scheduling.quartz.JobDetailBean.

5. Create our main application. It will simply load the Spring configuration file. If you are running a Spring MVC application, the same configuration can be used.

package com.teamextension.quickquartzspring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class QuickQuartzSpring {
	public static void main(String[] args) throws InterruptedException {
		new ClassPathXmlApplicationContext("applicationContext.xml");
	}
}

Run the application and you should see the current date printed out every 5 seconds.

For more information about Spring and Quartz integration, read the Spring Task Execution and Scheduling page.