@ Autowired: the injected object is NULL and @ autowirednull.

Source: Internet
Author: User

@ Autowired: the injected object is NULL and @ autowirednull.

Crying... this is a waste of time in the afternoon... A small project based on spring mvc and spring data jpa. After I wrote a controller test case, a blank pointer was reported during a run, followed by a dao with null. then let's take a look at the dao code:

@ Component @ Transactionalpublic class XXService {private Logger logger = LoggerFactory. getLogger (getClass (); @ Autowired private XXDao xxDao; public void add (XX xxl) {logger. debug ("Enter XXService. add (XX xx) method "); xxDao. save (xx); // a null pointer is reported here because xxDao is null.
}

At first, I didn't think this xxDao would be null and thought there was a problem with the parameter. However, the breakpoint followed and found that not only the parameter was correct, but there was nothing wrong with the whole process of the program, an error is reported only when dao is run .. because this dao is automatically implemented by jpa, I think it may be a problem in the process of generating and implementing a parameter or a ing problem, but now let's think about it, although the process of automatic dao implementation may fail, the empty pointer should not be reported. baidu circled and saw a post about @ Autowired injection failure. At this time, it was possible that dao injection failed and code again. It is true that dao is not implemented by injecting jpa, it is null. I have no idea about this. Don't worry about spring bugs or jar package conflicts. How can this problem be solved? When I was studying spring, I encountered a variety of strange problems. The wrong code obtained the correct result, but the correct code always reported errors for various reasons ..

Although there is no solution, some people mentioned in the original post that @ Autowired injection is required for all dao-used places, including services. Otherwise, it will become invalid. So let me check it, although I don't report hope, because this code was written by the leader half the time, and the probability of making mistakes is very small. however, we can see the following code:

@RestController@RequestMapping(value = "/xxx/xx")public class XXController {    Logger logger = LoggerFactory.getLogger(getClass());        private XXService xxService = new XXService();      .....}

I fell down... if you forget to inject the object, you can simply create a new object. In this way, no error is reported when you call the service... then dao layer injection directly fails ..

Here we have confirmed the point in the post we saw: @ Autowired injection must be performed in all places where dao is used, including the servcie that calls it, otherwise, the injection will fail ..

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.