The author of this article is original, reproduced please indicate the source: http://www.cnblogs.com/further-further-further/p/7717331.html
Solve the problem
The business logic Bean is registered into the spring container through Component-scan automatic scanning, and the XML configuration file bean is removed from the manual registration process, which reduces the complexity of XML configuration file.
Content Description
1. When registering a scan bean and using @autowired annotations to assemble automatically, you need to introduce <context:component-scan base-package= in the XML configuration file " Com.spring.example.scan "/>;
2, through the Component-scan automatic scanning definition base class package All beans, need to add the annotation @component before the class name, and can customize the bean id< @Component ("Instru"); The ID of the righteous bean is the default class name (all lowercase), and the scanned bean is loaded into the spring container;
3, after the spring container is started (get to the Spring container instance), through the autowired annotations and the Bean ID (Qualifier) to assemble and inject the corresponding instance object;
4, call the instance method;
Application Example (package name: Com.spring.example.scan)
The spring configuration file Component-scan.xml is as follows
<?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/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 " > <!--automatically scans the bean and defines the bean, and puts the bean into the spring container - <Context:component-scanBase-package= "Com.spring.example.scan"/> </Beans>
View Code
Instrument Interface Code
Public Interface Instrument { void play ();}
Guitar Implementing Interface Instrument code
@Component Public class Implements Instrument { @Override publicvoid play () { System.out.println ("Guitar ....");} }
Performer Interface Code
Public Interface Performer { void perform ();}
Instrumentalist implementing interface Performer code
@Component ("Instru")//Specify Bean ID Public classInstrumentalistImplementsPerformer { Publicinstrumentalist () {} @Value ("Yesterday Once more!") PrivateString Song; //@Autowired can assemble properties, methods, constructors, as long as the types are the same (this is the instrument type)//limit ambiguity dependence, use @autowired annotations to assemble automatically, satisfy the multiple bean of assembly ,//you can narrow the scope by specifying @qualifier.@Autowired @Qualifier ("Guitar")//specifying beans by ID Privateinstrument instrument; Public voidSetsong (String song) { This. Song =Song; } Public voidSetinstrument (instrument instrument) { This. Instrument =instrument; } @Override Public voidperform () {System.out.println ("Playing" + Song + ":"); Instrument.play (); }}
View Code
Test code
Public classDriverextendsApplication { Public Static voidMain (string[] args) {launch (args); } @Override Public voidStart (Stage primarystage) {Try{ApplicationContext CTX=NewClasspathxmlapplicationcontext ("Spring/component-scan.xml"); Performer Performer= (performer) Ctx.getbean ("Instru"); Performer.perform (); }Catch(Exception e) {e.printstacktrace (); } }}
View Code
Run results
Summary
Component-scan automatic scanning advantages and disadvantages:
Advantages: a> Component-scan Automatic Scanning registration assembly can effectively solve the shortcomings of annotation-config, do not need to specify the bean in the spring configuration file, so that the configuration file is very concise;
B> removes the process of defining beans in XML, assembling and injecting directly into the application, reducing the errors caused by changing class Mingshi;
Disadvantage: The third-party interface instance Bean cannot be obtained automatically;
Application Scenarios
applicable to the development of their own business logic;
This article describes may have the wrong or not the whole place, welcome everybody to spit the trough!
Spring Framework Application Series II: Component-scan Automatic Scan Registration assembly