Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

30/06/2010

Upgrading to Spring 3: "Unable to locate Spring NamespaceHandler" in JAR file built by Maven

After upgrading to Spring 3 your may discover that your application packaged in JAR archive does not start any more throwing an exception like this:

"Unable to locate Spring NamespaceHandler for XML schema namespace[http://www.springframework.org/schema/tx]"

This happens when you include multiple Spring module dependencies in your pom.xml file and use Maven Shade or Assembly plugin to build a single JAR.
Since Spring can does not contain a single distribution jar (org.springframework:spring:jar), Spring namespace handlers, schema mappings and tooling information files are now present in multiple files with names:

  • META-INF/spring.handlers
  • META-INF/spring.schemas
  • META-INF/spring.tooling

In order to make it work in a single shaded JAR you need to merge contents of these files from different jars and place merged files into new JAR. More...

30/01/2009

How to Lookup JBoss MBean Server from Spring (JMX)

If you having problems accessing "mbeanServer" Spring-managed bean under JBoss Application server Read more...

29/01/2009

How to Use Spring from EJB3

This is a short instruction how to inject a spring-managed bean into EJB3 component.

  1. Read SpringFratamework's reference here: http://static.springframework.org/spring/docs/2.5.x/reference/ejb.html#ejb-implementation-ejb3

  2. Place to ejb module's classpath a file beanRefContext.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

    <bean id="myBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg value="myApplicationContext.xml"/>
    </bean>

    </beans>

  3. Create application context file named myApplicationContext.xml and define You beans there. Place this file to the ejb module's classpath.

  4. Annotate your Stateless Session Bean:
    @Stateless
    @Interceptors(org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.class)
    public class MyFacadeBean implements MyFacade {

    @Autowired
    private MySpringComponent component;
    ...
    public void foo() {
    component.foo();//invocation
    }
    }

  5. Deploy and test Your application.

27/01/2009

How To Export Spring Managed Bean To JNDI

Sometimes, it is necessary to export a spring managed bean to JNDI context. Here I want to show how do it.

In spring, there is a bean that provides a similar functionality for exporting to MBean server: MBeanExporter. Unfortunately, there is no standard JNDI bean exporter implementation in spring (current version is 2.5.6) - (Why?).
But it's easy to write it youself:
package com.example.spring.jndi.export;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jndi.JndiTemplate;

public class JndiExporter implements InitializingBean, DisposableBean {

private String jndiName;

private Object bean;

private final JndiTemplate jndiTemplate = new JndiTemplate();

public String getJndiName() {
return jndiName;
}

public void setJndiName(String jndiName) {

this.jndiName = jndiName;
}

public Object getBean() {
return bean;
}

public void setBean(Object bean) {
this.bean = bean;
}


public void afterPropertiesSet() throws Exception {
jndiTemplate.bind(jndiName, bean);
}

public void destroy() throws Exception {
if (bean != null && jndiName != null && bean == jndiTemplate.lookup(jndiName)) {
jndiTemplate.unbind(jndiName);
}
}
}

Add following fragment to spring configuration file:
<bean id="myBean" class="com.example.MyBean"/>

<bean class="com.example.spring.jndi.export.JndiExporter">
<property name="bean" ref="myBean" />
<property name="jndiName" value="MyJNDIName"/>
</bean>
Don't forget to make your bean serializable by implementing java.io.Serializable interface.
Now we can lookup exported bean by adding to the spring config fil:
<jee:jndi-lookup id="myJndiBean" jndi-name="MyJNDIName" proxy-interface="com.example.IMyBean" lookup-on-startup="false"/>

That's all, folks!

redirect