google Analytics

Sunday, July 25, 2010

Spring Autowiring Demo byName:byType:constructor:autodetect

=============================================================

=============================================================

public class AutowiringDemo {

public static void main(String[] args) {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("/META-INF/spring/autowiringdemo1-context.xml"));

autowire(bf, "byName");
autowire(bf, "byType");
autowire(bf, "constructor");
autowire(bf, "autodetect");
}

private static void autowire(BeanFactory factory, String beanName) {
System.out.println(beanName + ":");
System.out.println(factory.getBean(beanName));
System.out.println();
}
}

=============================================================

=============================================================

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="foo" class="com.apress.prospring2.ch03.autowiring.Foo"/>
    <bean id="bar" class="com.apress.prospring2.ch03.autowiring.Bar"/>

    <bean id="byName" autowire="byName" class="com.apress.prospring2.ch03.autowiring.Target"/>
    <bean id="byType" autowire="byType" class="com.apress.prospring2.ch03.autowiring.Target"/>
    <bean id="constructor" autowire="constructor" class="com.apress.prospring2.ch03.autowiring.Target"/>
    <bean id="autodetect" autowire="autodetect" class="com.apress.prospring2.ch03.autowiring.Target"/>
</beans>


=============================================================

=============================================================
public class Bar {

@Override
public String toString() {
return getClass().getName();
}
}

public class Target {
private Foo foo;
private Foo foo2;
private Bar bar;

public Target() {
System.out.println("Target()");
}

public Target(Foo foo) {
System.out.println("Target(Foo)");
this.foo = foo;
}

public Target(Foo foo, Bar bar) {
System.out.println("Target(Foo, Bar)");
this.foo = foo;
this.bar = bar;
}

public void setDependency(Bar bar) {
System.out.println("Target.setDependency(Bar)");
this.bar = bar;
}

public void setFoo(Foo foo) {
System.out.println("Target.setFoo(Foo)");
this.foo = foo;
}

public void setFoo2(Foo foo2) {
System.out.println("Target.setFoo2(Foo)");
this.foo2 = foo2;
}

public void setBar(Bar bar) {
System.out.println("Target.setBar(Bar)");
this.bar = bar;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Target");
sb.append("{foo=").append(foo);
sb.append(", foo2=").append(foo2);
sb.append(", bar=").append(bar);
sb.append('}');
return sb.toString();
}
}

=============================================================

=============================================================

public class Foo {

@Override
public String toString() {
return getClass().getName();
}
}

=============================================================

=============================================================

No comments:

Post a Comment