简体   繁体   中英

Spring @Component with external beans

I have to use third party cache in my project and this is my class

import com.cgm.life.common.cdi.cache.CacheName;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
import java.util.concurrent.ConcurrentMap;

@Component
public class MyClass {

    @Inject
    @CacheName("journeyID")
    private ConcurrentMap<String, String> journeyIdMap;

    //other codes
}

The third party class looks like ( not a Spring project)

import javax.inject.Qualifier;
@Qualifier
public @interface CacheName {
    ....
}

But MyClass is failing to compile with

cannot find bean with qualifier journeyID

Of course, if I remove the @Component annonation from my class, there is no issue. Is there any way to resolve it? I can't change the third party stuff and I don't want to use any xml in my project. Maybe something can be done with @PostConstruct ?

Any help is appreciated.

You can create a configuration bean, where you create a bean from your 3rd party class.

This way, you'll have it as a bean in the spring bean container, and you'll be able to wire it into your MyClass .

@Configuration
public class CacheConfig {

  @Bean
  public CacheName journeyID() {
    return new CacheName() etc..    
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM