@Component single case of multiple cases

Source: Internet
Author: User
Tags lowercase
@Component Single case of multiple cases

@Component
Default Single Example

If you want to declare multiple examples

@Component
@Scope ("prototype")

Reference articles

< reprint >spring annotation @component, @Service, @Controller, @Repository


In addition to providing @Component annotations, Spring 2.5 also defines several annotations with special semantics, namely @Repository, @Service, and @Controller. In the current Spring version, these 3 annotations are equivalent to @Component, but from the naming of annotation classes It is easy to see that the 3 annotations correspond to the persistence layer, the business layer, and the control layer (the WEB layer) respectively. While these 3 annotations are nothing new than @Component, Spring will add special features to them in future releases. So, if a WEB application uses a classic three-tiered hierarchy, it's best to annotate the classes in the persistence layer, business layer, and control layer with @Repository, @Service, and @Controller, and use the @Component Comment on those classes that are more neutral .

In a slightly larger project, there are typically hundreds of components that, if configured with XML bean definitions, would obviously increase the size of the configuration file, and it would be inconvenient to find and maintain it. Spring2.5 introduces a component automatic scanning mechanism, where he can look for classes that annotate @Component, @Service, @Controller, @Repository annotations under the classpath and incorporate them into the spring container for management. Its role is the same as when using a Bean node configuration component in an XML file. To use the automatic scanning mechanism, we need to open the following configuration information:
Java code

1. <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "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-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd "
2. >
3.
4. <context:component-scan base-package= "Com.eric.spring" >
5. </beans>
* * Where Base-package is the package that needs to be scanned (including all child packages)

@Service is used to label business layer components,

@Controller used to annotate the control layer component (such as action in struts),

@Repository is used to annotate the data Access component, the DAO component,

@Component refers to components, we can use this annotation to annotate when components are not properly categorized.

*/


6. @Service public class Ventorserviceimpl implements Iventorservice {
7.} @Repository public class Ventordaoimpl implements Iventordao {
8.}

The default name for/*getbean is the class name (the first letter lowercase), and if you want to customize it, you can specify it @service ("AAAAA"), which by default is a single example, and you can use @service ("Beanname") if you want to change it @Scope ("prototype") to change. You can specify initialization methods and destruction methods (any of the method names) by using the following methods: @PostConstruct public void init () {

*/
9.}
@PreDestroy public void Destory () {
11.}

Injection mode:

Inject the DAO implementation class into the service implementation class, injecting the service's interface (note not the service's implementation class) into the action, not the injected class when injected, because spring will automatically inject, and if it is manually new, there will be an error. Then the attribute plus @autowired does not require the getter () and setter () methods, and spring is automatically injected. As for more specific content, such as the injection of the way more skilled will be a complete example.

Annotations:

In spring's configuration file, you simply add the <context:annotation-config/> and <context:component-scan base-package= "the package where the injected class is needed"/ You can use base-package= "*" to represent all classes.

<context:component-scan base-package= "Com.eric.spring" >

Where Base-package is the package that needs to be scanned (including all child packages)

The @autowired and @qualifier annotations on the front of the interface allow the interface to be injected into the container, which must be specified when the interface exists for two implementation classes, injected using a string with the first letter lowercase of the implementation class, such as:

@Autowired

@Qualifier ("Chinese")

Private Man Mans;
Otherwise you can omit, write only @autowired.

@Service service layer component, which is used to annotate a business-level component, to define a bean that automatically instantiates a lowercase bean for the first-write base based on the Bean's class name, such as Chinese instance to Chinese, if you need to change your name: @Service ("  You changed your own bean name ").

@Controller used to label the control layer component (such as action in struts)

@Repository persistence layer component, which is used to annotate the data Access component, the DAO component

@Component refers to components, we can use this annotation to annotate when components are not properly categorized.

My annotation: Use the @autowired general default variable name as the class name, and the first letter lowercase. However, you can use other variable names. For example, when JUnit tests are used uniformly

Instance
@Service
public class Ventorserviceimpl implements Iventorservice {
}

@Repository
public class Ventordaoimpl implements Iventordao {
}

The default name for Getbean is the class name (the first letter lowercase) and, if you want to customize it, you can specify it @service ("AAAAA"), which

The bean default is a single example, and if you want to change it, you can use the @service ("Beanname") @Scope ("prototype") to change it.

You can specify initialization methods and destruction methods (any of the method names) by using the following methods:

@PostConstruct

public void init () {

}

@PreDestroy

public void Destory () {

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.