Spring annotations can greatly reduce the configuration of XML, and the declaration of the Spring.xml file is:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-3.0.xsd " > <Context:annotation-config/></Beans>
Spring 3 supports several different annotations for automated assembly:
1, spring comes with the @autowired annotation;
2, the @inject annotation of JSR-330;
3. @resource Annotation of JSR-250
@Autowired Note: Bytype automatic assembly is used
The sample 1:spring attempts to assemble the Instrument property, but if no bean matching the type instrument is found, the app will have no problem and the Instrument property will be set to NULL
@Autowired (required=false)Private instrument instrument;
Example 2: Bean matching bean with ID person1
@Autowired @qualifier ("Person1") Private Person Monitor, ... @Qualifier ("Person1") Public class extends person{. @Qualifier ("Person2")publicclassextends person{
While <context:annotation-config> helps to completely eliminate the <property> and <constructor-arg> elements in the spring configuration, we still need to use < bean> element Display Definition Bean
However, springy has another way of <context:component-scan> element, which, in addition to completing the same work as the <context:annotation-config> element, It also allows spring to automatically detect beans and define beans. This means that the <bean> element is not used, and that most or all of the beans in the spring application can be defined and assembled
Statement:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-3.0.xsd " > <Context:component-scanBase-package= "Action"></Context:component-scan></Beans>
The <context:component-scan> element finds the classes that are annotated with stereotype annotations, which are the following special annotations:
@Component-generic structural annotations that identify the class as a spring component
@Controller-identifies defining the class as a spring MVC Controller
@Repository-Identifies that the class is defined as a data warehouse
Any custom annotations that use @component annotations
Examples:
PackageAction;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.stereotype.Component; @Component ("P")//If there is no setpoint p, get the name Persona Public classPersonA {@Value ("Lily") PrivateString name; @Value ("15") Private intAge ; PublicPersonA () {System.out.println ("Create Person"); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } }
Packagespring.studye.spring.bean2;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importaction. PersonA;Importaction. Springidolconfig;/*** Hello world! **/ Public classApp { Public Static voidMain (string[] args) {System.out.println ("Begin" ); ApplicationContext CTX=NewClasspathxmlapplicationcontext ("Spring.xml"); PersonA P= (PersonA) ctx.getbean ("P");//If there is no setpoint p, then Ctx.getbean ("PersonA")System.out.println ("name =" +p.getname () + "age =" +p.getage ()); System.out.println ("End"); }}
The components under the base-package can be filtered, using <context:include-filter> and <context:exclude-filter>
Example: Require that all classes derived from Person1 be automatically registered as spring beans, requiring that the Person2-derived is not automatically registered as a spring bean
<Context:component-scanBase-package= "Action"> <Context:include-filtertype= "assignable"expression= "Person1"/> <Context:exclude-filtertype= "assignable"expression= "Person2"/> </Context:component-scan>
Filter Type:
Annotation: Filter scan uses the classes marked with the specified annotations to specify which annotations to scan with the expression property
Assignable: Filter scans those classes that derive from the type specified by the Expression property
ASPECTJ: Filter scans those classes that match the ASPECTJ expressions specified by the expression property
Custom: Use a customized Org.springframework.core.type.TypeFilter implementation class that is specified by the expression property
Regex: Filters scan for classes whose names match those of the regular expression specified by the Expression property
Finally, a completely larger note to reduce the XML file: @Configuration
<context:component-scan> automatically loads classes that use @configuration annotation callouts under Base-package
Examples:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-3.0.xsd " > <Context:component-scanBase-package= "Action"> </Context:component-scan></Beans>
PackageAction;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.stereotype.Component; Public classPersonA {@Value ("Lily") PrivateString name; @Value ("15") Private intAge ; PublicPersonA () {System.out.println ("Create Person"); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } }
Packagespring.studye.spring.bean2;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;Importaction. PersonA;Importaction. Springidolconfig; Public classAPP2 { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext actx=NewAnnotationconfigapplicationcontext (Springidolconfig.class); PersonA P= (PersonA) actx.getbean ("PersonA2"); System.out.println (P.getname ()); P.setname ("Kange"); PersonA P2= (PersonA) actx.getbean ("PersonA2"); System.out.println (P2.getname ()); }}
Also note the import package:
<Dependency> <groupId>Cglib</groupId> <Artifactid>Cglib-nodep</Artifactid> <version>2.2</version> </Dependency>
Annotationconfigapplicationcontext provides three constructors for initializing a container.
Annotationconfigapplicationcontext (): The constructor initializes an empty container that contains no Bean information and needs to register the configuration class later by calling its register () method and calling Refresh () method to refresh the container.
Annotationconfigapplicationcontext (Class<?> ... annotatedclasses): This is the most commonly used constructor, by passing the configuration class involved to the constructor, To implement the beans in the corresponding configuration class to be automatically registered in the container.
Annotationconfigapplicationcontext (String ... basepackages): This constructor automatically scans all classes under the given package and its child packages, and automatically recognizes all Spring beans and registers them in the container. It not only recognizes the configuration class of the callout @Configuration and resolves it correctly, but also recognizes classes that use @Repository, @Service, @Controller, @Component annotations.
Spring Learning Notes--annotations