In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail why spring configuration does not scan the service layer. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
Spring configuration does not scan service layer reason
I'll scan the contoller for springmvc, and then I'll hand over the rest to springscan.
Then it was found that Employee Service of the service layer did not scan for the following problems
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘springmvc.crud.service.EmployeeService’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)… 39 more
The final reason is that in the spring configuration file, context:exclude-filter is not added use-default-filters, while context:include-filter is added.
I have been looking for a long time!!
Spring configuration automatic scanning principle introduction
Spring uses IOC container to manage and maintain all beans in an orderly manner. In actual projects, it is impossible to create beans in xml files. Instead, Spring uses Spring's component automatic scanning mechanism to incorporate components into Spring container by automatic scanning in classpath. This greatly reduces programmer time spent on configuration XML files, making them clean, tidy, and easy to maintain.
The working steps of this mechanism are:
1. Configure the classes to be scanned;
2. Annotate the classes that need to be included in the Spring container;
Spring finds annotated classes in the classpath and manages them in Spring containers.
It does the same thing as using node configuration components in XML files.
Use of automatic scanning components
Step 1: Configure the applicationContext.xml file;
As follows:
Note: The node is used to inform the Spring container to scan components, and the base-package attribute is used to specify the package name of the component to be scanned.
Context:component-scan has a use-default-filters attribute, which defaults to true, which means that all classes labeled @Component@Service,@Repository under the specified base-package will be scanned and registered as beans.
1. context:include-filter Specify scan location
2. context:exclude-filter specified do not scan
When use-default-filters="true", the include-filter specified in this case has no effect, only if
use-default-filters="false", context:exclude-filter specified do not scan, context:include-filter specified scan
Step 2: Add comments for the classes to be scanned
There are several types of annotations:
@Service is used to label business layer components;
@Repository is used to annotate data access layer components;
@Controller is used to annotate control layer components (e.g. action in Struts)
@Component represents a generic component, which we can annotate when the component is not easy to classify.
Step 3: After annotating the class, we need to test whether the corresponding component is included in the Spring container, so we need to test whether the component is scanned
As follows:
@Testpublic void testAddUser(){UserBiz userBiz = (UserBiz)context.getBean("userBizImpl");System.out.println(userBiz);}
If the output is not empty, the test class has been scanned and included in the Spring container.
Summary of detailed problems
1. When we test, we use the getBean() method of the ApplicationContext object to look for components.
In the previous configuration file we will use the id attribute of the tag to define, how to get the id of the component after using the annotation?
In this case, Spring takes the annotated class name and lowercase the first letter of the class name into the getBean() method. For example, the component Id of UserBizImpl class will be userBizImpl, and when obtained, it will be context.getBean("userBizImpl");
So, can we customize the Id of a component when using annotations?
Of course you can.
We need to add a custom class name after the annotation when adding comments for the corresponding class, for example:
@Service("userBiz")public class UserBizImpl implements UserBiz {……}
context.getBean("userBiz") when we get the component;
2. In the configuration file we can set the scope of the component (bean), its default value is singleton mode, so in the case of adding annotations, how do we set the scope of the component?
We can set it directly with another annotation @Scope("prototype") at the same time as adding annotations to classes, as follows
@Service("userBiz")@Scope("prototype")public class UserBizImpl implements UserBiz {……}
3. Set initialization and destruction methods for components when using annotations
If we want to initialize or destroy a method in the corresponding annotated class, we can directly annotate the method as follows:
@PostConstructpublic void addItem() {System.out.println("Initialize Method");}@PreDestroypublic void testItem() {System.out.println("Free Resource");}
4. How do I do dependency injection after using Spring auto-scan components?
Use the annotations @Resource and @Autowired and set names for dependent objects, for example:
@Resource(name="userDao")private UserDAO userDao = null;
First it looks for components (beans) that Spring automatically scans and adds to the Spring container based on their names, and if they have the same name, it does dependency injection, if they don't. Components are looked for according to the type area.
About "spring configuration does not scan the service layer is what" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.