# Beispiel einer rein Annotation basierten Spring Konfiguration



## Thomas Darimont (14. Januar 2007)

Hallo,

inspiriert von diesem Blogeintrag (und einer kleinen Diskussion im StudiVz...)
http://blog.interface21.com/main/2006/11/28/a-java-configuration-option-for-spring/
Hier mal noch ein Beispiel zu solch einer rein auf Annotations basierten Spring Konfiguration:

Die Implementierung AnnotationApplicationContext gibts hier:
http://sourceforge.net/project/showfiles.php?group_id=73357&package_id=213222

BeanA:

```
/**
 * 
 */
package de.tutorials;

/**
 * @author Tom
 * 
 */

public class BeanA {

    protected BeanB a;
    protected BeanB b;

    /**
     * @return the a
     */
    public BeanB getA() {
        return a;
    }

    /**
     * @param a
     *            the a to set
     */
    public void setA(BeanB a) {
        this.a = a;
    }

    /**
     * @return the b
     */
    public BeanB getB() {
        return b;
    }

    /**
     * @param b
     *            the b to set
     */
    public void setB(BeanB b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return super.toString() + " " + this.getA() + " " + this.getB();
    }
}
```

BeanB:

```
/**
 * 
 */
package de.tutorials;

/**
 * @author Tom
 * 
 */

public class BeanB {
}
```


```
/**
 * 
 */
package de.tutorials;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Bean;
import org.springframework.beans.factory.annotation.Configuration;
import org.springframework.beans.factory.annotation.Scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.java.AnnotationApplicationContext;

/**
 * @author Tom
 * 
 */
@Configuration()
public class AnnotationOnlySpringConfigurationExample {

	@Bean(autowire = Autowire.BY_TYPE)
	public BeanA beanA() {
		return new BeanA();
	}

	@Bean(scope=Scope.PROTOTYPE)
	public BeanB beanB() {
		return new BeanB();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext applicationContext = new AnnotationApplicationContext(
				AnnotationOnlySpringConfigurationExample.class.getName());
		System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames()));
		BeanA beanA = (BeanA) applicationContext.getBean("beanA");
		System.out.println(beanA);
	}
}
```

Ausgabe:

```
14.01.2007 16:32:29 org.springframework.core.CollectionFactory <clinit>
INFO: JDK 1.4+ collections available
class de.tutorials.AnnotationOnlySpringConfigurationExample
14.01.2007 16:32:29 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.java.AnnotationApplicationContext;hashCode=29193342]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [de.tutorials.AnnotationOnlySpringConfigurationExample]; root of BeanFactory hierarchy
14.01.2007 16:32:29 org.springframework.context.support.AbstractApplicationContext refresh
INFO: 3 beans defined in application context [org.springframework.context.java.AnnotationApplicationContext;hashCode=29193342]
14.01.2007 16:32:29 org.springframework.context.support.AbstractApplicationContext initMessageSource
INFO: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1975b59]
14.01.2007 16:32:29 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
INFO: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@913fe2]
14.01.2007 16:32:29 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [de.tutorials.AnnotationOnlySpringConfigurationExample,beanA,beanB]; root of BeanFactory hierarchy]
14.01.2007 16:32:29 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
INFO: CGLIB2 available: proxyTargetClass feature enabled
[de.tutorials.AnnotationOnlySpringConfigurationExample, beanA, beanB]
de.tutorials.BeanA@1dfafd1 de.tutorials.BeanB@8fce95 de.tutorials.BeanB@143c8b3
```

Diese Art der Konfiguration mit "nativen" Sprachmitteln erinnert sehr an die Konfiguration bei Django (Python) und Ruby on Rails (Ruby).

Bin zwar kein großer Fan davon aber interessant ist's trotzdem ;-)

Gruß Tom


----------

