Autowired of spring, Autowired of spring

Source: Internet
Author: User

Autowired of spring, Autowired of spring

The introduction of annotations in spring greatly simplifies the xml configuration and makes it easier to get started with spring. I also just started to use spring, and it took me a month to get confused. Now, the company can do all the basic spring configurations it needs. It can be seen that it is easier to get started with spring. However, it should be noted that spring's awesome framework should not be able to hold things in a month or two. Today, I stepped on two traps and recorded them.

The company's service calls adopt the remote call method. Each department exposes the externally provided service with a url such as 192.168.0.1: 4080/services/XXX, it can be called directly in the browser. When spring configures the service, it is not configured with a com. xx. # impl. Background.

When I referenced xxService, not only was it configured in the spring-bean.xml, but also used @ Autowired to mark it in the class that referenced it, and there was a set method, in short, I think all the Assembly-related conditions are added, and I think that there are multiple guarantees that there will be no bean initialization problems. As a result, it is still confusing. It took more than an hour to locate the problem.

The reason is that @ Autowired adopts the byType method to find the appropriate bean, which means that if I have an AService and an AServiceImpl that implements AService, if @ Autowired is marked for AService, you will first go to your code structure and find out if there is a class like AServiceImpl. If there is a class like AService, an error will be reported. As I have mentioned earlier, this is a remote service. In my code structure, I could not find this impl class, so even if I have configured a bean with the same name in the spring-bean.xml, autowired cannot be automatically assembled, and the solution is very simple. Just remove autowired, and it becomes the byName method. This story tells us not to draw a snake.

If Autowired is used forcibly...

You can also add @ Qualifier to limit the dead name so that Autowired can use byName to find it.

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.