我们有一个打包为 WAR 的 Spring 3.1 MVC 应用程序,部署在 GlassFish 3 应用服务器上。该应用程序曾经是 Spring 2,基于 XML,在迁移到 Spring 3 之后,我们慢慢将其重构为基于注释的 IoC 配置。
该应用程序的核心部分之一是自定义 bean 工厂,它为远程 EJB 执行普通的 JNDI 查找。但是,查找的参数存储在外部属性文件中,可以在运行时更改 - 因此需要自定义工厂。我们还没有找到通过注解来配置工厂生产的bean的方法,所以配置仍然是这样的XML:
<bean id="exampleEjb" factory-method="lookup"
class="com.example.EjbServiceLocator" lazy-init="false" scope="session">
<constructor-arg value="example.ejb.connstr" /> <!-- remote hosts: x.y.z.1:3701,x.y.z.2:3701 - cluster here! -->
<constructor-arg value="example.ejb.name" /> <!-- JNDI name: ejb/example/remoteService -->
<constructor-arg value="com.example.service.RemoteInterface" /> <!-- this is interface exposed by remote bean -->
<constructor-arg ref="configReader" /> <!-- custom bean monitoring configuration changes -->
</bean>
在这里使用工厂意味着容器看到的 bean 类com.example.EjbServiceLocator
不是com.example.service.RemoteInterface
. Autowire-by-type 会混淆,并且@Autowire
注解无法将预期类型与@Qualifier
. 所以我们必须使用@Resource(name="exampleEjb")
.
嗯,它有效。用一个“但是”。每次(重新)部署应用程序时,应用程序服务器都会扫描存档并尝试@Resource
自行管理注释。它失败了,在日志中留下了丑陋的SEVERE消息,导致我们支持部门的心率加快。
我可以在这里做什么,要么:
- 防止 Java EE 容器扫描应用程序中的类以查找注释?
- 将注释更改
@Resource
为 Java EE 容器无法识别的内容? - 导致工厂创建的 bean 向 IoC 容器公开正确的类型?