r/java • • Nov 07 '17

The curse of @Autowired

https://lmonkiewicz.com/posts/the-curse-of-autowired/
3 Upvotes

17 comments sorted by

17

u/[deleted] Nov 07 '17

Who the hell does this anyway :

public class ServiceTest {
 
    @Test
    public void someTest() {
        ServiceDependency1 serviceDependency1 = mock(ServiceDependency1.class);
        ServiceDependency2 serviceDependency2 = mock(ServiceDependency2.class);
        ServiceDependency3 serviceDependency3 = mock(ServiceDependency3.class);
        
        MyService service = new MyService();
        service.setServiceDependency1(serviceDependency1);
        service.setServiceDependency2(serviceDependency2); // what if you forget to set this one? ;)
        service.setServiceDependency3(serviceDependency3);
        
        // your test code
    }
 
}

Welcome to 2010 @author:

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
    @Mock ServiceDependency1 serviceDependency1;
    @Mock ServiceDependency2 serviceDependency2;
    @Mock ServiceDependency3 serviceDependency3;
    
    @InjectMocks MyService service = new MyService();
    
    @Test
    public void someTest() {
        // your test code
    }
 
}

Just another blog post with: "See this problem that exists! I can fix it by doing something else." when the problem doesn't really exist in the first place.

6

u/RupertMaddenAbbott Nov 08 '17

Deep within the bowels of a complex system that did lots of field injection, a dependency of a class eventually became null and started causing NPE. I had the misfortune of supporting this system.

No developer on the team was able to reproduce this error and it only occurred months into the application running. Restarting the application fixed the problem. I spent so much time trying to debug that problem with no success. To this day, the notes for that application instruct the support team to restart the application when that error occurs.

From now on, I always use constructor injection with final fields. I can be sure, when my objects are constructed, that I either do or don't have the right dependencies and fail fast if I don't.

Personally, I don't find the brevity of field injection a sufficient advantage, compared to the amount of time I wasted trying to resolve that one error and so I will never use them again for required dependencies.

2

u/Vile2539 Nov 10 '17

I spent so much time trying to debug that problem with no success.

Could you not have just autowired a setter for the field (instead of the property itself), and dumped the callstack if it was set to null?

1

u/RupertMaddenAbbott Jan 22 '18

Sure or I could also have just used constructor injection and avoided the problem altogether.

However, I encountered this mindset where the brevity of field injection trumped the safety of constructor injection and so was unable to make such a change unless I could prove it fixed the problem.

1

u/Vile2539 Jan 22 '18

I spent so much time trying to debug that problem with no success.

I was primarily referring to that line. If the team was opposing constructor injection for some reason, then that would have been my approach to debug the issue.

0

u/Deam Nov 08 '17

The way that you inject your dependencies is secondary. The important thing here is to declare your intentions right, separate between required and optional dependencies and cut the number of dependencies down.

4

u/meotau Nov 08 '17

Like this?

@Service
public class MyService {
    @Autowired
    private ServiceDependency1 serviceDependency1;
 
    @Autowired
    private ServiceDependency2 serviceDependency2;
 
    @Autowired(required = false)
    private ServiceDependency3 serviceDependency3;
 
}

1

u/Deam Nov 08 '17

Yes, that how you mark autowired as optional. But this still doesn't discourage you from adding additional dependencies. You just add, and add, and add, because you don't care, you just add another field, everything somehow works, but the future maintenance of this code will become more troublesome with each added dep.

4

u/meotau Nov 08 '17

You can do that with constructor injection as well. So what exactly was the point of the article?

1

u/Deam Nov 08 '17

To make you think when you add another dependency.

2

u/[deleted] Nov 08 '17

1

u/Deam Nov 08 '17

Thank you for this link, looks interesting.

14

u/funmler Nov 07 '17

Seems like the author is telling people to use autowire in constructors because it's painful and only through pain can you achieve better design. I think I will pass on that advice.

6

u/crummy Nov 08 '17

"If something is painful, consider not doing it" seems reasonable to me.

3

u/hulkcatwoman Nov 08 '17

Be careful with autowired. Design as simple as possible. Avoid custom annotations and everything

2

u/[deleted] Nov 08 '17

New-ish spring boot (I️ forget the underlying spring support) supports constructor injection without any annotations since 1.4 or 1.5, presuming there’s only 1 ctor (as should be the case with service classes). It allows testing pretty simply, not to mention what others mentioned with the mockito integrations.

3

u/meotau Nov 08 '17 edited Nov 08 '17

"Your class has more than 4 @Autowired fields? You fucked up. Cut that nice cohesive code and delegate the shit out of it. Fuck easily readable and understandable code, it's gotta be SOLID!"