Friday, January 18, 2008

spring standalone jndi context

while looking for something to allow the use of jndi out of a jee container, in an environment such as tomcat, i came across james strachan's blog detailing his work on this area for apache servicemix.

he's implemented a standalone jndi context using some apache servicemix jndi classes, and an intro to this can be found here.

spring scheduling

javaranch has a great tutorial on spring scheduling, which uses opensymphony's quartz api.

spring jms quickstart infrastructure configuration

using spring JMS and ApacheMQ you can set up messaging capabilities within your application, even outside of a jee container.

here is an xml sample representing the beans that set up the jms infrastructure:

<?xml version="1.0" encoding="UTF-8"?>
<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="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://embedded?broker.persistent=false"/>
</bean>

<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue.request"></constructor-arg>
</bean>

<bean id="confirmationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue.confirmation"></constructor-arg>
</bean>

</beans>

the latter beans are queues, which can be named and coded appropriately and you can have as many as you need. queues are required for messages to be sent on.

the broker url also needs adjusting to represent your infrastructure, however this embedded broker can be used for development and testing.

blogging html and xml code

ive found this useful tool which will encode html and xml code into escaped characters, and if wrapped between an html blockquote, pre and code tags, it will retain whitespace as necessary.

http://centricle.com/tools/html-entities/

useful eclipse java shortcuts

ctrl + [space] - auto-complete.

use this when you have no text and what to know suggestions, when you have started the text you want but want it completed, or after you have entered the capital letters of the class you require.

ctrl + O - quick outline

ctrl + shift + O - organise imports

ctrl + T - class (or type) hierarchy

ctrl + shift + T - find class (or type) by name

Thursday, January 17, 2008

spring mvc MessageSource

A MessageSource is sourced by a number of "message bundles" which provide the mapping from a key to a readable String. The code below points out a sample message source:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/validation"/>
</bean>

This code creates a bean for a reloadable message source, so when changes are made, they will be picked up without redeployment or reloading. The basename represents where the messages can be found, and in this case the default file will be in the folder '/WEB-INF/messages' in a file called 'validation.properties'. To support internationalisation, multiple message bundles (i.e. validation_XX_YY.properties) can be created in this folder using the correct naming convention for the languages required.

Now if a message of code, for example, 'empty.value' requires a message it will look for the message within these files/this file, using the standard property convention 'key=value'.

Labels:

spring ViewResolver

spring's dispatcher servlet delegates to a ViewResolver to map returned view names to view implementations. by default, view names are treated as web application relative file paths, but this can be overridden by registering a ViewResolver bean within the application context.

by using the following code, view names will be prefixed with '/WEB-INF/views' and suffixed with '.jsp':


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
this means if a ModelAndView is returned from a Controller with a view name 'summary', the resource '/WEB-INF/views/summary.jsp' will be returned.

spring webflow configuration template


<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

<!-- Write your flow here -->

</flow>

spring full bean definition configuration template


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd">

<!-- add bean definitions here -->

</beans>

spring basic bean definition configuration template


<?xml version="1.0" encoding="UTF-8"?>
<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">

<!-- add bean definitions here -->

</beans>

Monday, January 14, 2008

spring log4j xml configuration template


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>

<!-- Application logger -->
<logger name="yourapp">
<level value="info" />
</logger>

<!-- 3rdparty Loggers -->
<logger name="org.springframework.beans">
<level value="warn" />
</logger>

<logger name="org.springframework.jdbc">
<level value="warn" />
</logger>

<logger name="org.springframework.transaction">
<level value="warn" />
</logger>

<logger name="org.springframework.orm">
<level value="warn" />
</logger>

<logger name="org.springframework.web">
<level value="warn" />
</logger>

<logger name="org.springframework.webflow">
<level value="warn" />
</logger>

<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>

</log4j:configuration>