应用程序配置,Bean到xml

时间:2015-02-04 10:00:05

标签: java xml spring javabeans

我在xml上使用配置库。如何将此java代码翻译为xml?

@Bean
public CacheManager cacheManager() {
 SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
 GuavaCache cache1 = new GuavaCache("book", CacheBuilder.newBuilder().build());
 GuavaCache cache2 = new GuavaCache("books", CacheBuilder.newBuilder()
             .expireAfterAccess(30, TimeUnit.MINUTES)
             .build());
 simpleCacheManager.setCaches(Arrays.asList(cache1, cache2));
 return simpleCacheManager;
}

我有这个,但我卡住了:

    <bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager">
        <property name="caches">
            <list>
                <ref bean="cache1"/>
                <ref bean="cache2"/>
            </list>
        </property>
    </bean>
    <bean id="cache1" class="org.springframework.cache.guava.GuavaCache">
        <constructor-arg value="book" />
        <constructor-arg refer="foo" />
    </bean>

    <bean id="cahe1Settings" class="com.google.common.cache.CacheBuilder" factory-method="newBuilder">

    </bean>
    <bean id="foo" factory-bean="cahe1Settings" factory-method="build"/>

1 个答案:

答案 0 :(得分:1)

试试这个对我有用:

<bean id="cacheBuilder" class="com.google.common.cache.CacheBuilder" factory-method="from">
    <constructor-arg value="expireAfterAccess=30m" />
  </bean>

  <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
      <set>
        <bean class="org.springframework.cache.guava.GuavaCache">
          <constructor-arg value="mycache" />
          <constructor-arg>
            <bean factory-bean="userCacheBuilder" factory-method="build" />
          </constructor-arg>
        </bean>
      </set>
    </property>
  </bean>