Spring IOC 4

import org.springframework.core.annotation.AliasFor;
@Award
public @interface Trophy {
        @AliasFor(annotation = Award.class, attribute = "value")
        String name() default {};
}
import org.springframework.stereotype.Component;
@Component("johnMayer")
@Award(value={"grammy", "platinum disk"})
public class Singer {
        private String lyric = "We found a message in a bottle we were drinking";
        public void sing() {
                System.out.println(lyric);
        }
}
@Component("johnMayer")
@Trophy(name={"grammy", "platinum disk"})
public class Singer {
        private String lyric = "We found a message in a bottle we were drinking";
        public void sing() {
                System.out.println(lyric);
        }
}

别名

<beans>
    <bean id="nonSingleton" class="com.apress.prospring5.ch3.annotated.Singer"
             scope="prototype" c:_0="John Mayer"/>
    </beans>
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("nonSingleton")
@Scope("prototype")
public class Singer {
      private String name = "unknown";
      public Singer(@Value("John Mayer") String name) {
            this.name = name;
      }
}

scope: Prototype

<beans>
    <bean id="fooOne"
       class="com.apress.prospring5.ch3.xml.complicated.FooImpl1"
       primary="true"/>
    <bean id="fooTwo"
       class="com.apress.prospring5.ch3.xml.complicated.FooImpl2"/>
    <bean id="bar" class="com.apress.prospring5.ch3.xml.Bar"/>
    <bean id="targetByType" autowire="byType"
        class="com.apress.prospring5.ch3.xml.complicated.CTarget"
        lazy-init="true"/>
</beans>

Primary

@Component("gigi")
@Lazy
public class TrickyTarget {
       
        @Autowired
        @Qualifier("fooImplOne")
        public void setFoo(Foo foo) {
                this.foo = foo;
                System.out.println("Property fooOne set");
        }
        @Autowired
        @Qualifier("fooImplTwo")
        public void setFooTwo(Foo fooTwo) {
                this.fooTwo = fooTwo;
                System.out.println("Property fooTeo set");
        }
       
}

Qualifier 或者Primary

<beans>
    <bean id="parent" class="com.apress.prospring5.ch3.xml.Singer"
        p:name="John Mayer" p:age="39"/>
    <bean id="child" class="com.apress.prospring5.ch3.xml.Singer"
        parent="parent" p:age="0"/>
</beans>
public class Singer {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String toString() {
        return "\tName: " + name + "\n\t" + "Age: " + age;
    }
}
import org.springframework.context.support.GenericXmlApplicationContext;
public class InheritanceDemo {
        public static void main(String... args) {
                GenericXmlApplicationContext ctx =
                    new GenericXmlApplicationContext();
                ctx.load("classpath:spring/app-context-xml.xml");
                ctx.refresh();
                Singer parent = (Singer) ctx.getBean("parent");
                Singer child = (Singer) ctx.getBean("child");
                System.out.println("Parent:\n" + parent);
                System.out.println("Child:\n" + child);
        }
}

配置继承

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值