Spring @component Annotations, @Controller annotations (NET pick)

Source: Internet
Author: User

In the process of using spring, to avoid extensive use of the bean-injected XML configuration file, we use the spring-provided automatic scan injection method,

Just add a few lines of auto-injected configuration, you can complete the service layer, controller layer and so on injection configuration.

In the process of use, @compopnet annotations are added to the implementation class header in the service layer, and the controller class header and @controller annotations are completed.

For example

The set method is not required when we invoke a service in the controller, and the service object is annotated directly with the @autowried annotation:

For example

In the controller:

@Controller

@RequestMapping ("/test")

public class Examplecontroller {

@Autowired

Private Exampleservice service;

}

In the service

@Component

public class Exampleserviceimpl Implements Exampleservice {

@Autowired

Private Exampledao Exampledao;

}

XML configuration in Spring:

<!--automatically scan Service,controller components--

<context:component-scan base-package= "com.example.service.*"/>

<context:component-scan base-package= "com.example.controller.*"/>

Typically, in cases where the bean is adding @component annotations, when the service is started, the service will report the exception in the following code in advance, and the appropriate bean should be checked for the correct addition of the @component annotation. When @controller is not configured in the controller layer, the service may not explode at startup, but you will notice that the URL address in the page request is correct, and that the corresponding method in the controller is not accessible at that time. This time you need to check if @controller annotations and @requestmapping annotations have been added to class.

Org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' example '

No matching bean of type [com.example.ExampleService] found for dependency:expected at least 1 bean which qualifies as AU Towire candidate for this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}

The following is a detailed introduction to the following @component, @Controller notes:

Org.springframework.stereotype.Component (Implements Java.lang.annotation.Annotation)

At the time of automatic service, spring initialization, Spring initializes all classes that add @component annotations as alternate objects under the configuration path using automatic scanning, while initializing [email protected]

When the corresponding Bean is annotated, the @Autowired tag automatically looks for the appropriate alternate object to complete the injection of the bean.

Org.springframework.stereotype.Controller (Implements Java.lang.annotation.Annotation)

The @Controller annotation is a special component that allows the implementation class to complete automatic injection by scanning the class configuration path, usually @controller in conjunction with @requestmapping annotations.

Conclusion:

Understanding spring's annotations can help us improve development efficiency while using the spring development process, as well as strengthen our understanding of spring. In the process of using spring development, I personally prefer to use annotations to reduce the configuration file code.

Spring @component Annotations, @Controller annotations (NET pick)

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.