olada
olada

Reputation: 45

Ignore JNDI-Resource injection (@Resource) in Spring

I am currently experimenting with Spring Boot and I'm trying to include a library class which is currently used in a Java EE context. Said class has a parent class which has a @Resource injection with a JNDI lookup name.

In a reduced example, the involved classes look like this:

Configuration.java

@Configuration
public class Facades {
  @Bean
  public Connection connection() {
    return new Connection();
  }

  @Bean
  public Facade facade(Connection connection) {
    return new Facade(connection);
  }
}

Facade + ParentFacade

public class ParentFacade {
  @Resource(lookup = "eis/Connection")  // <-- the problem
  protected Connection connection;
}

public class Facade extends ParentFacade {
  public Facade(Connection connection) {
    this.connection = connection;
  }
}

When I run the application, following error appears:

Invalid bean definition with name 'eis/Connection' defined in JNDI environment: JNDI lookup failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Obviously, I don't need the @Resource injection since I can inject the connection by constructor. However, Spring always attempts to inject a bean with the JNDI name dynamically.

I have tried to exclude the class from IoC inclusion with following annotations, but it made no difference:

@Configuration
@ComponentScan(
        basePackages = "the.package",
        excludeFilters = @ComponentScan.Filter(
                type = FilterType.ASSIGNABLE_TYPE,
                classes = { Facade.class, ParentFacade.class }
        )
)

My question now is: Can I either

Thanks in advance!

P.S.: changing the library class which contains the @Resource annotation is not an option

Upvotes: 1

Views: 682

Answers (1)

Cergey Chaulin
Cergey Chaulin

Reputation: 241

I think, you need @Resource(name = "connection")

Upvotes: 0

Related Questions