简体   繁体   English

如何确保Spring Bean的加载顺序?

[英]How can I ensure the loading order of Spring Beans?

I have a third party jar which provides a servlet filter used in my project. 我有一个第三方jar,它提供了在我的项目中使用的servlet过滤器。 In the servlet filter there is a static block initializing a static object. 在Servlet过滤器中,有一个静态块初始化一个静态对象。

What I am doing is using reflection to retrieve the static object and revise it. 我正在做的是使用反射来检索静态对象并对其进行修改。

The configuration is as following: 配置如下:

<filter>
  <filter-name>thirdPartyFilter</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>thirdPartyFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Where thirdPartyFilter is defined in xml 在XML中定义了thirdPartyFilter的地方

<bean id="thirdPartyFilter" class="com.abc.ThirdPartyFilter" />

The ThirdPartyFilter in simple ThirdPartyFilter简单

class ThirdPartyFilter implements Filter{
    private static BB bb = new BB();
    static{
         bb.setText("bb");
    }
    doFilter(){...}
}

I already have a class called ThirdPartyFilterModifier,in which there is a factory bean using reflection to manipulate the object BB which is initialized in thirdPartyFilter. 我已经有一个名为ThirdPartyFilterModifier的类,其中有一个工厂bean使用反射来操作在thirdPartyFilter中初始化的对象BB。 But the definition of the modifier is using annotation. 但是修饰符的定义是使用注释。

@configuration
class ThirdPartyFilterModifier{
   @Bean
   public BB reviseBB(){
      // using reflection to retrieve bb and revise it.
      return bb;
   }
}

Checking the console log while the app is starting, I can clearly see that the static block in ThirdPartyFilter printed out first, and then the factory bean of ThirdPartyFilterModifier comes later. 在应用程序启动时检查控制台日志,我可以清楚地看到先打印出ThirdPartyFilter中的静态块,然后再显示ThirdPartyFilterModifier的工厂bean。

I admit this is the very result what I expect. 我承认这就是我所期望的结果。 But the thing confuses me a bit because I didn't do anything to ensure the thirdPartyFilter loaded before the thirdPartyFilterModifier. 但是,这让我有些困惑,因为我没有做任何事情来确保在thirdPartyFilterModifier之前加载thirdPartyFilter。

Can anyone point out if I am doing the right thing in both the xml and annotation configuration? 谁能指出我在xml和注释配置中做的正确吗?

//UPDATED I have just tried converting the annotation into xml //更新我刚刚尝试将注释转换为xml

<bean id="thirdPartyFilterModifier" class="com.mycom.ThirdPartyFilterModifier" factory-method="reviseBB" />

and placed this definition before & after the thirdPartyFilter's. 并将此定义放在thirdPartyFilter的前后。 Got the same result of the ordering : firstly thirdPartyFilter and then thirdPartyFilterModifier. 得到了相同的排序结果:首先是thirdPartyFilter,然后是thirdPartyFilterModifier。

If you just want to ensure the order of creation between any two beans managed by Spring, you can use depends-on . 如果只想确保由Spring管理的任何两个bean之间的创建顺序,则可以使用depends-on

In XML: 在XML中:

<bean id="beanOne" class="ExampleBean" depends-on="manager">
  <property name="manager" ref="manager" />
</bean>

In annotations: 在注释中:

@DependsOn

In general: modifying the a third-party filter via reflection does not sound like a good idea. 通常,通过反射修改第三方过滤器听起来不是一个好主意。 If possible, you should try to find an alternative solution. 如果可能,您应该尝试寻找替代解决方案。 Even after you make this work, you will be locked with that version of your dependency and cannot safely update to a newer version. 即使完成这项工作,您也将被该版本的依赖项锁定,并且无法安全地更新到较新版本。 Maybe it would be worth posting another SO question to find a cleaner solution. 也许值得提出另一个SO问题以找到更清洁的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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