In the process of using Spring, in order to avoid using a large number of Xml configuration files injected by Bean, we will use the automatic scan injection method provided by Spring, just add several lines of automatic injection configuration, you can complete
Service layer, Controller layer, and so on. during use, add @ Compopnet annotation to the implementation Class header in the Service layer, and add @ Controller annotation to the Controller Class header to complete the configuration. For example
In Controller, when we call a Service, we do not need to Set the method. We can directly annotate the Service object using the @ Autowried annotation: for example:
In Controller:
@Controller
@RequestMapping("/test")public class ExampleController { @Autowired private ExampleService service; }
In Service
@Component public class ExampleServiceImpl Implements ExampleService { @Autowired private ExampleDao exampleDao; }
XML configuration in Spring:
Generally, when Bean is added with @ Component annotation, when the service is started, the Service reports an exception in the following code in advance, check whether @ Component is correctly added to the Bean.
Annotation, but the @ Controller is not configured in the Controller layer, the Service may not encounter exceptions at startup, but you will find that the URL address in the page request is correct, at that time, no matter how you access
Check whether the @ Controller annotation and @ RequestMapping annotation have been added to the 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 autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
The @ Component and @ Controller annotations are described as follows:
- Org. springframework. stereotype. Component (implements java. lang. annotation. Annotation)
During spring initialization, spring will use all classes with the @ Component annotation added as alternative objects in the configuration path for automatic scan injection, and initialize spring @ Autowired.
When the corresponding Bean is annotated, the @ Autowired tag will automatically find the corresponding alternative object to complete bean injection.
- Org. springframework. stereotype. Controller (implements java. lang. annotation. Annotation)
@ Controller annotation is a special Component that allows the implementation class to perform automatic injection by scanning the class configuration path. Generally, @ Controller is used together with @ RequestMapping annotation.
Conclusion:
By understanding Spring annotations, we can improve the development efficiency during Spring development and enhance our understanding of Spring. In the process of using Spring development, I personally prefer to use annotations to reduce the configuration file code.
Author: Ziv Serena
Source: http://www.cnblogs.com/zivxiaowei/
About the author: programmers who focus on Java Technology keep a high interest in JS development. Like music, reading, FM, etc.
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide a connection to the original article on the article page.
If there is a problem, you can mail: wewoor@foxmail.com
Weibo: Ziv Xiaowei
Category: JAVA