HerrSubset
HerrSubset

Reputation: 99

Move beans defined in applicationContext.xml to jboss configuration

currently I have a spring project with an applicationContext.xml that defines some beans that are being used for code injection:

<bean class="com.example.Example1ClientImpl">
    <constructor-arg type="java.lang.String" value="${EXAMPLE1_URL}" />
</bean>

<bean class="com.example.Example2ClientImpl">
    <constructor-arg type="java.lang.String" value="${EXAMPLE2_URL}" />
</bean>

I want to refactor this code into another project that does not use spring. It runs on a JBoss server, so I assume these beans can be declared in ejb-jar.xml or jboss-ejb3.xml, but I can't figure out how.

Is it possible to do this and can somebody give me some pointers on how to do this?

EDIT: The ExampleXClientImpl classes are not defined in the project itself, they are defined in a dependency of the project. I could adapt this dependency, but I'd prefer if that's not necessary.

Upvotes: 1

Views: 241

Answers (2)

HerrSubset
HerrSubset

Reputation: 99

I decided to use @Produces, I forgot that this existed. The solution looks something like this:

public class ClientProducer {
    private static final String CLIENT1_ENDPOINT_VAR = "CLIENT1_URL";
    private static final String CLIENT2_ENDPOINT_VAR = "CLIENT2_URL";

    @Produces
    public Example1Client produceExample1Client() {
        String uri = System.getProperty(CLIENT1_ENDPOINT_VAR);
        return new Example1ClientImpl(uri);
    }

    @Produces
    public Example2Client produceExample2Client() {
        String uri = System.getProperty(CLIENT2_ENDPOINT_VAR);
        return new Example2ClientImpl(uri);
    }
}

Upvotes: 1

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

If your aim is to make EJB's out of those beans then I would try doing a mix of xml and annotations:

ejb-jar.xml

Here you define your environment properties:

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0" metadata-complete="false">
  <enterprise-beans>
    <session>
      <ejb-name>Configuration</ejb-name>
      <env-entry>
        <env-entry-name>EXAMPLE1_URL</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>url1</env-entry-value>
      </env-entry>
      <env-entry>
        <env-entry-name>EXAMPLE2_URL</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>url2</env-entry-value>
      </env-entry>
    </session>
  </enterprise-beans>
</ejb-jar>

Ejb

Here you create a Singleton (as this is the default Spring scope). IF you want to have the bean to have the default prototype scope of EJB's then you can annotate it with Stateless instead:

import javax.annotation.Resource;
import javax.ejb.Singleton;

@Singleton
public class Example1ClientImpl{

    @Resource(name = "EXAMPLE1_URL")
    private String url;

    ...

}

@Singleton
public class Example2ClientImpl{

    @Resource(name = "EXAMPLE2_URL")
    private String url;

    ...

}

Upvotes: 2

Related Questions