2

I have a custom annotation that is declared as a Spring managed Service:

@Service
public @interface MyServiceAnnotation {
    // my service url
    String url();
}

The above declaration enables my services to be autowired as Spring Managed beans.
@MyServiceAnnotation(url="/path/serviceLocation")
public class SomeService {
   doWork();
}

However, there are certain services that have their bean definitions in an applicationContext.xml. Adding the @MyServiceAnnotation to such beans makes them both Autowiring enabled, as well as inject dependecy through the xml file.

Due to issues related to legacy code, I don't want to remove the xml bean definitions and make them all autowired.

So, is there a way in which I could turn off autowiring in this case, and still use @MyServiceAnnotation? Ideally I would like to have the @Service annotation on MyServiceAnnotation, the existing services would still use the @MyServiceAnnotation but would get their dependencies injected based on the xml. All the new services would be autowired without the xml bean definitions.

One possible approach is to create NonSpringManagedMyServiceAnnotation that is same as MyServiceAnnotation, but without the @Service annotation on it. The downside of this is that I'd have to duplicate rest of the code from MyServiceAnnotation, which I don't want to.

4

2 回答 2

4

这是一种方法,但可能不是那么理想。我假设你会component-scan在你的 xml 中指定一个标签来扫描具有 Spring 原型注释的类,这些标签支持一个exclude-filter子标签来忽略特定的模式。如果您指定的文件遵循特定模式(特定包、特定名称等),那么您可以简单地指定此子标签来忽略包含注释的类。

<context:component-scan base-package="mypackage">
    <context:exclude-filter type="regex" expression=".*ToBeIgnoredNaming"/>
</context:component-scan>
于 2013-06-03T21:24:55.617 回答
0

我认为您应该尝试保持关注点分离。

添加@Service注释使其成为事实上的 Spring 服务:由于这不是您想要的行为,您可能只需@MyAnnotation在每个服务(旧版和新版)上都有一个简单的(带有 url 属性)。然后在每个新服务上添加 Spring 的@Service注解,以通过注解启用 bean 注册。

于 2013-06-03T21:26:24.450 回答