1

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

  • make Spring ignore the @Resource annotation in the problematic class OR
  • inject a @Bean with the required JNDI-name (eis/Connection)?

Thanks in advance!

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
olada
  • 45
  • 5

1 Answers1

0

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

Cergey Chaulin
  • 241
  • 1
  • 2
  • 5
  • That doesn't work - this would work if the `@Resource` annotation would use 'name' and not 'lookup' as a parameter. – olada Jul 30 '19 at 18:22
  • @Resource uses `name` also. Have you looked at https://stackoverflow.com/questions/4093504/resource-vs-autowired ? – Cergey Chaulin Jul 31 '19 at 16:31
  • Unfortunately, changing the library class, which contains the `@Resource` annotation is not an option. This means that I cannot simply change _lookup_ to _name_. My goal is either to completely ignore the `@Resource` annotation or to inject it otherwise. – olada Aug 02 '19 at 06:22