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);
}
}
配置继承