0

I'm working in a webapp and this is the first time that I'm using Java based configuration. I have a bunch of class to configure all:

  • ApplicationContext
  • PersistenceContext
  • SecurityContext
  • WebAppInitializer
  • WebMvcContext

Now I'm defining Spring Data repositories and the service layer so I need to inject the repositories there. Normally I would use Autowired but I've read that it is preferable to define the injections manually so the question is, where?

Maybe neither of the previous configuration classes is suitable for such task but, do I have to create a single class to define all the injections or is better to have on for each function? What happens if the project grows too much?

I think that the main question would be what is best way to organize dependencies in a Spring project. What do you do?

I add here an image of the structure of the project as a petition. I'm trying to decouple layers and now I need to inject UserRepository to UserService.

enter image description here

5
  • 1
    Can you add some example code. It helps to show what you are trying. Commented Dec 18, 2014 at 21:13
  • 1
    Please link to but I've read that it is preferable to define the injections manually or add some context. Commented Dec 18, 2014 at 21:13
  • I added the link. Sorry for that. Commented Dec 18, 2014 at 21:22
  • @superbAfterSemperPhi I added an image with the project structure. Hope this help. Commented Dec 18, 2014 at 21:29
  • 1
    The advice against autowiring is outdated. Autowiring did cause difficulties when XML was all you had, but with annotations you always clearly see the type of the property being autowired. The rare cases where autowiring by type doesn't work are well-served by @Qualifier. Commented Dec 18, 2014 at 21:54

2 Answers 2

2

No, I would not define a single class to do all the injections. All your classes are coupled that way.

I don't understand what "define the injections manually" means. You have to specify them in either XML or annotations. There's no other way that I know of.

You don't say if you're using XML or annotation configuration. I find myself using the latter more of the time, with only enough XML configuration to tell the Spring app context to scan for annotations.

The Spring idiom would have you specify your configuration in layers if you're using XML. It's a moot point for annotations, because they go into your source code.

Your application will read the Spring context on start up, instantiate all the beans, and wire together the necessary dependencies. You're good to go from then on.

I disagree with the link you provided. Avoid autowiring? No.

Sign up to request clarification or add additional context in comments.

8 Comments

I'm using annotations (Java based configuration as I said in the post) and I could use Autowired anotation instead of manually define them in a Java file or in a XML.
You add those annotations to your source code, which is a Java file. You need the XML to tell Spring to scan for annotations and the packages to look in.
I don't use any XML file right now. I use an annotation to do that.
Could you explain why you disagree? I'm really interested. I've found some people commenting that autowired should be avoided and I would like to hear opions in behalf of using it.
Different implementations in the same app? You'll have different bean ids with properties that distinguish which one you want. Those do lend themselves to XML configuration, not annotations. An example would be a single web service client class that makes connections to several different endpoint URLs. "Mess to maintain in huge projects" - not in my experience. Don't say it until you've tried it.
|
0

The article said that he recommends using XML configuration for large projects. This is a very small project at this point. It seems to me that auto wiring with annotations would be fine even by the article's author's words.

1 Comment

Sure. I started the project two days ago but I'll have much more. I don't want to move all the injections later if I can do it now (if needed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.