1. Create a custom annotation
1 ImportOrg.springframework.beans.factory.annotation.Qualifier;2 ImportJava.lang.annotation.ElementType;3 Importjava.lang.annotation.Retention;4 ImportJava.lang.annotation.RetentionPolicy;5 ImportJava.lang.annotation.Target;6 7 8 @Target ({elementtype.field,elementtype.parameter,elementtype.type})9 @Retention (retentionpolicy.runtime)Ten @Qualifier One Public@Interfacestringedinstrument { A -}
View Code
The retention annotation has an attribute value, which is of type Retentionpolicy, and enum Retentionpolicy is an enumeration type,
Annotated with @retention (Retentionpolicy.class), which indicates that the information of the annotations is kept in the CLASS file (bytecode file) when the program is compiled, but is not read by the virtual machine at run time;
@retention (Retentionpolicy.source) modified annotations, indicating that the information of the annotations will be discarded by the compiler, will not be left in the class file, the information will only remain in the source file;
Annotated with @retention (Retentionpolicy.runtime), indicating that the information of the annotations is retained in the class file (bytecode file) when the program compiles, the virtual machine is kept at runtime,
2. Create instrument and use annotations
1 @StringedInstrument 2 Public class Instrument {34 Public void Play () {5 System.out.println ("Playing ..."); 6 }7 }
3, create the performer Kenny, for automatic assembly
1 Importorg.springframework.beans.factory.annotation.Autowired;2 3 Public classKenny {4 5 @Autowired6 @StringedInstrument7 Privateinstrument instrument;8 9 PublicInstrument getinstrument () {Ten returninstrument; One } A - Public voidSetinstrument (instrument instrument) { - This. Instrument =instrument; the } - - - PublicKenny (instrument instrument) { + This. Instrument =instrument; - } + A Public voidperform () { at Instrument.play (); - } -}
4. Configure Spring
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >6 7 <Context:annotation-config></Context:annotation-config>8 9 <BeanID= "Kenny"class= "Kenny">Ten </Bean> One A <BeanID= "instrument"class= "instrument"> - - </Bean> the - </Beans>
5. Testing
1 ImportOrg.springframework.context.ApplicationContext;2 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;3 4 5 Public classTest {6 Public Static voidMain (string[] args) {7 8ApplicationContext context =NewClasspathxmlapplicationcontext ("Context.xml");9Kenny Kenny = (Kenny) context.getbean ("Kenny");Ten kenny.perform (); One } A}
Create spring custom annotations for automatic assembly