quote

Visualizzazione post con etichetta spring. Mostra tutti i post
Visualizzazione post con etichetta spring. Mostra tutti i post

martedì, febbraio 08, 2011

Spring - Referring to a String as a bean


<bean id="myString" class="java.lang.String">
<constructor-arg type="java.lang.String" value="Lucio"/>
</bean>

<bean id="myUser" class="com.benfante.User">
<property name="firstName" ref="myString"/>

</bean>

Thanks to Lucio for the example

giovedì, dicembre 17, 2009

Deploying Spring @Component annotated application in JBoss 5.1

I've spent the last few days fighting with jboss 5.1 trying to deploy a spring based web application using the Component annotation.
The application as opposed works using Tomcat 6.
Basically the spring context was not able to find the definition of SchedulerBo which is my Component annotated class.

package webscheduler.bo;

@Component
public class SchedulerBo {
...............................
}

This is the section of my spring configuration file which allows the spring context to auto-scan the schedulerBo.

<context:component-scan base-package="webscheduler.bo"/>

This is the exception I had.

19:17:09,706 ERROR [ContextListener] Error in base ContextListener.contextInitialized
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobNormal' defined in ServletContext resource [/WEB-INF/schedulerContext.xml]: Cannot resolve reference to bean 'schedulerBo' while setting bean property 'jobDataAsMap' with key [TypedStringValue: value [schedulerBo], target type [null]]; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'schedulerBo' is defined

............................................................

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'schedulerBo' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBeanDefinition(DefaultListableBeanFactory.java:387)

This is the main section of my listener (ServletContextListener) class which I used to load the spring context(marked in red the row to change)

private WebApplicationContext applicationContext;
.....................................
config.add("WEB-INF/schedulerContext.xml");
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setServletContext(servletContext);
ctx.setConfigLocations(config.toArray(new String[config.size()]));
ctx.refresh();
applicationContext = ctx;

To sort out this problem you have to use org.jboss.spring.factory.VFSXmlWebApplicationContext instead of rg.springframework.web.context.support.XmlWebApplicationContext.
Therefore I changed this row in my listener.

XmlWebApplicationContext ctx = new org.jboss.spring.factory.VFSXmlWebApplicationContext();

Here the steeps to follow to solve the problem:

  1. Add this dependency to your pom, be sure to have the jboss repository in your pom.xml


    <dependency>
    <groupId>org.jboss.snowdrop</groupId>
    <artifactId>snowdrop-vfs</artifactId>
    <version>1.0.0.GA</version>
    <exclusions>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    </exclusion>
    </exclusions>
    </dependency>


  2. Using VFSXmlWebApplicationContext in your listener class in this way

    XmlWebApplicationContext ctx = new org.jboss.spring.factory.VFSXmlWebApplicationContext();

  3. run $ mvn clean install and deploy the new generated war into <JBOSS_HOME>/server/default/deploy


This is the link in JBoss JIRA which reports the bug and shows the solution.

venerdì, gennaio 09, 2009

spring message tag libray

If you are using spring message tag library you know how much is annoying get a runtime exception in the case you have inserted a wrong code as attribute.
For every message code tag library you define in your jsp you have to add the corresponding property entry in the message.properties file but can happen you forget to insert it or maybe you insert a wrong value.
For example having such tl message where the code is Email

<spring:message code="Email" />

I have to define the corresponding entry in the file message.properties, or in more files depending on the type of internationalization I use.

Email=E-Mail

A way to avoid to get the runtime exception, in case of wrong code, is to add the property text to the message tl. In case of not matching the value of text will appear in the page and you could fix the problem after. Better to use text property with ? before and after the string so that you can easily understand there is a wrong value of code.
Therefore use

<spring:message code="Email" text="?Email?"/>

As usual, thanks to Lucio.

venerdì, dicembre 05, 2008

Auto injected spring beans

A colleague of mine asked me to solve a problem about using beans, configured using the spring container, in legacy code.
Basically he couldn't change the way those beans are created but he wanted to be able to inject their properties using a spring configuration file.
This is the way he wants to create an instance of the class BeanAutoInjecting.

BeanAutoInjecting bai = new BeanAutoInjecting();

The class BeanAutoInjecting has its attributes injected by spring container but we don't want to have any trace of Spring in the code that use this class.
A friend of mine, Lucio, proposed me to use the class AutowiredAnnotationBeanPostProcessor to sort out this problem.
Therefore we have to modify the constructor of the class BeanAutoInjecting in this way:

public BeanAutoInjecting()
{
ApplicationContext ctx = SpringLoader.getApplicationContext();
AutowiredAnnotationBeanPostProcessor aabpp = (AutowiredAnnotationBeanPostProcessor)ctx.getBean(
"org.springframework.context.annotation.
internalAutowiredAnnotationProcessor");
aabpp.processInjection(this);
}

The trick is to define a dummy constructor and to instruct the spring container to use it. Here the dummy constructor

public BeanAutoInjecting(int a)
{
System.out.println("I am the dummy constructor!");
}

Here the definition of the bean and the auto-wiring directive in the spring configuration file.

<!-- auto wiring directive -->
<context:component-scan base-package="spikes.springexamples"/>
---------------------------
<!-- bean definition with dummy constructor ->
<bean name="beanAutoInjecting" class="spikes.springexamples.BeanAutoInjecting">
<constructor-arg>15</constructor-arg>
</bean>

That's cool I would say, I am able to use a spring managed bean in my legacy code creating it out of the spring container!!!

martedì, febbraio 19, 2008

spring mail configuration using gmail as smtp server

If you are going to use gmail as smtp server in your spring configuration file remember to add the property mail.smtp.starttls.enable and set it to true.
Here the configuration of mailSender used in jugevents .

<!-- start mail section -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- here your smtp server -->
<property name="host"><value>smtp.gmail.com</value></property>
<!-- Parameters for SMTP AUTH -->
<property name="username"><value>yourUsername</value></property>
<property name="password"><value>yourPassword</value></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<!-- used by gmail smtp server -->
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>

Fail doing it you will face into the following exception

org.springframework.mail.MailSendException; nested exception details (1) are: Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first z37sm13522675ikz.1 at

Thanks to Lucio for the help.

martedì, luglio 17, 2007