Spring 引用没有id的bean
我试图在Activiti中使用Spring表达式语言引用JPA存储库。但是,由于Spring正在使用Spring 引用没有id的bean,spring,el,spring-el,activiti,Spring,El,Spring El,Activiti,我试图在Activiti中使用Spring表达式语言引用JPA存储库。但是,由于Spring正在使用创建存储库bean,因此它们没有与之关联的id。有没有一种方法可以使用SpEL来引用特定类型的bean,而不是通过id?我尝试使用我认为是为locationRepository生成的名称(locationRepository),但没有成功。不确定如何在SPEL中执行此操作,但您可以使用它来决定应该注入哪个bean 如果需要,您可以创建自己的自定义@Qualifier注释并基于它访问bean。 像
创建存储库bean,因此它们没有与之关联的id。有没有一种方法可以使用SpEL来引用特定类型的bean,而不是通过id?我尝试使用我认为是为locationRepository
生成的名称(locationRepository),但没有成功。不确定如何在SPEL中执行此操作,但您可以使用它来决定应该注入哪个bean
如果需要,您可以创建自己的自定义@Qualifier注释并基于它访问bean。像 现在在repositorybean和其他您想要注入它的地方使用
@MyRepository
注释
@Repository
@MyRepository
class JPARepository implements AbstractRepository
{
//....
}
注射
@Service
class fooService
{
@Autowire
@MyRepositiry
AbstractRepository repository;
}
我假设
LocationRepository
是一个接口,并且正在为您生成支持实现。当Spring创建一个bean并且没有显式指定id时,它通常使用实现类的类名来确定bean id。因此在这种情况下,LocationRepository
的id可能是生成的类的名称
但是由于我们不知道它是什么,我们可以创建一个SpringFactoryBean
,它通过自动连接从应用程序上下文中获取LocationRepository
,并以新名称将其放回应用程序上下文中
public class LocationRepositoryFactoryBean extends AbstractFactoryBean<LocationRepository> {
@Autowired
private LocationRepository bean;
public Class<?> getObjectType() { return LocationRepository.class; }
public Object createInstance() throws Exception { return bean; }
}
公共类LocationRepositoryFactoryBean扩展了AbstractFactoryBean{
@自动连线
私有位置存储库bean;
公共类getObjectType(){return LocationRepository.Class;}
公共对象createInstance()引发异常{return bean;}
}
在应用程序上下文xml中:
<bean name="locationRepository" class="your.package.LocationRepositoryFactoryBean"/>
然后,您应该能够使用bean id LocationRepository引用您的
LocationRepository
对象。很抱歉延迟,我在一个漫长的周末之前发布了这篇文章。有道理,它们的bean名是生成的类而不是接口名,这就是为什么我不能引用它。谢谢你可能的解决方案!虽然不能完全解决我的问题,但很高兴知道,谢谢您的输入=)
<bean name="locationRepository" class="your.package.LocationRepositoryFactoryBean"/>