Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Turning On Spring Security with Java Config

Dmitry Ponomarenko
Dmitry Ponomarenko
18,175 Points

"Couldn't autowire" inspection on UserService in IntelliJ IDEA

Greetings! For some reason IntelliJ shows an error here:

http://i.imgur.com/NfKCl3i.png

Tried to load completed project but still have the same problem. When I run app everything works fine, but that's still annoying. I used @SuppressWarnings("SpringJavaAutowiringInspection") to supress that warning from IDE, but I wonder why Chris doesn't have that error.

5 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I think in our case with SecurityConfig Intellijidea simply does not know where to look for bean UserService. Trying here and there, I also tried adding @ComponentScan('com.teamtreehouse') on top of the SecurityConfig class. It seems to work as well.

It is just we have to tell Intellijidea where to look for beans. Judging by StackOverflow posts, people know where and how to create proper application configuration files with settings where is which. I'm unfortunately not so good at this yet.

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

The reason, Chris didn't get this error, because he is using Community Edition of IntellijIdea, whereas you probably are using Ultimate edition. Because only in Ultimate edition IntellijIdea is helping you with Spring Configuration. Chris used IntellijIdea Community Edition 2016.1.3 I think. I tried the same version - no error. Now I have ultimate version as well, and in it there is such error. If however you are using Community Edition, that error is strange...

Dmitry Ponomarenko
Dmitry Ponomarenko
18,175 Points

That makes sense, I still wonder why IntelliJ Ultimate shows this as an error though.

Dries Ketels
Dries Ketels
7,156 Points

I noticed that my UserServiceImpl wasn't annotated with @Service. Doing only that fixed my error. I did not have to annotate with @SuppressWarnings... or Componentscan either. Which makes sense to me because just annotating with @Service makes it being scanned on bootrun.

Adding this @ComponentScan("com.teamtreehouse.service") did not help in my case I am using Intellij 2016.2.2

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I edited answer: try writing (I mistyped package name):

@ComponentScan("com.teamtreehouse")

Also keep in mind that @SuppressWarnings will also help:

@SuppressWarnings("SpringJavaAutowiringInspection")
Jeremiah Shore
Jeremiah Shore
31,168 Points

Be sure that you aren't missing the @Service annotation on the UserServiceImpl class.

I had to suppress the warning to make it work

@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
private UserService userService;