SeedStack Spring bridge add-on is a bi-directional injection bridge between SeedStack (Guice) and Spring. It allows to inject Spring beans with Guice and vice-versa.


Show version
dependencies {


  # If true auto-detection of XML files in META-INF/spring ending with *-context.xml is enabled (defaults to true)
  autodetect: (boolean) 
  # Classpath locations of Spring XML files to load explicitly (in addition to autodetected ones if any) 
  contexts: (List<String>)
  # If true, Spring-managed JPA EntityManager will be injected instead of the SeedStack one (defaults to true)
  manageJpa: (boolean)

To dump the spring configuration options:

mvn -q -Dargs="spring" seedstack:config


By default, the add-on detects all XML files named *-context.xml located in the META-INF/spring classpath location and aggregates them into a global Spring context. This behavior can be changed with the following configuration:

Inject Spring beans in Guice instances

The add-on scans the Spring context built from detected and/or explicitly listed XML files and makes every bean injectable through Guice with:

  • The bean class and the bean name,
  • The bean parent class (except Object) and the bean name,
  • Any bean directly implemented interface and the bean name.

Consider the following example:

public class SomeClass {
    private SomeBeanClass bean1;
    private SomeBeanParentClass bean2;
    private SomeBeanImplementedInterface bean3;

Note that you always need to qualify your injection with the bean identifier (@Named("theBeanId")).

Create Guice instances in Spring context

To use Guice instances in the Spring context, you need to add the Seed namespace to your Spring files and use the <seed:instance> element:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" 
    <seed:instance id="bean1" class="org.myorganization.myproject.SomeClass"/>
    <seed:instance id="bean2" class="org.myorganization.myproject.SomeClass" qualifier="someQualifier"/>

The bean1 declaration is equivalent to the following Guice injection:

public class SomeClass {
    private SomeClass bean1;

The bean2 declaration is equivalent to the following Guice injection:

public class SomeClass {
    private SomeClass bean1;

Since Guice can inject Spring beans and Spring can inject Guice instances, circular dependencies can occur. To break it, Guice instances are by default proxied for lazy initialization. If you need to disable this behavior, specify proxy="false":

<seed:instance id="bean2" 

Create configuration values in Spring context

SeedStack configuration values can be created in Spring context:

    <bean id="..." class="...">
        <property name="someValue">
            <seed:configuration key="myProject.someValue"/>

This will inject the SeedStack myProject.someValue configuration value into the someValue property of the bean. A default value can be specified:

<seed:configuration key="myProject.someValue" default="someDefaultValue"/>

You can make a configuration value optional by specifying mandatory="false":

<seed:configuration key="myProject.someValue" mandatory="false"/>

An identifier can be given to the configuration value to be further referenced:

    <seed:configuration id="someConfigProperty" key="myProject.someValue"/>
    <bean id="..." class="...">
        <property name="someValue" ref="someConfigProperty"/>

JDBC data sources in Spring context

You can create a JDBC data source bean from a SeedStack configured datasource. The following datasource1 JDBC datasource:

      url: jdbc:hsqldb:mem:testdb1

Can be retrieved as a bean as below:

<seed:datasource id="someDatasource" name="datasource1"/>

On this page

Edit this page