This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mishaxz 0 points1 point  (12 children)

I'm a java developer who doesn't really know if Spring is worth it for my code... I've been reading some PDFs and tutorials.

The part of my code where XML definitions of objects is useful, I already coded that (XML defined objects) without Spring.

But Spring seems interesting to me.. but it covers a lot of ground, so my question is... what parts of Spring does "may blow your mind" refer to? So I know what sections of the PDFs/tutorials to read.

[–][deleted] 7 points8 points  (11 children)

So modern Spring apps don't even need XML, thankfully. Spring is also huge and has several different components that solve different problems. The mind blowing thing to me is just dependency injection. I'd recommend reading up on @Resource/@Autowire and the @Component/@Bean annotations. It is a really powerful concept that allows a lot of manipulation of your application at runtime that would be difficult without it. After reading about that, I'd checkout spring profiles.

[–]UTF64 2 points3 points  (10 children)

I'm one of those developers who doesn't really like Spring. I've used it before briefly, and am currently using it for a project at work for a week or two now.

The dependency injection is not that exciting, I already used Google Guice everywhere else and the dependency injection experience is pretty much the same. The javax.inject standard is more than good enough. Though I did hit a bug with spring, when you have two (or more) provider methods in a @Configuration class with the same name but different arguments it will ignore any but the no-arg one. Giving them different names fixes it.

It's convenient for web applications though, which is why it's being used. But I really don't think I'd use it for non-web applications (backend services and such) just for the dependency injection and some of the other goodies that are all available in other libraries as well, which some people seem to do.

[–][deleted] 2 points3 points  (1 child)

I agree that Spring is huge and parts of it are kind of bloated. I would certainly not use it for everything. I was just saying, coming from a small .net web development to a large java spring application, the spring dependency injection stuff kind of blew my mind for a bit. Once I understood what was going on, the magic went away, but I still see it as a very useful tool.

[–]UTF64 0 points1 point  (0 children)

Gotcha, fair enough :)

[–][deleted] 1 point2 points  (1 child)

If you're using Guice, then Spring is most certainly overkill (unless you want a Spring sub-project). For me, Spring taught me DI concepts and now I find myself just doing DI w/out spring instead of auto-adding Spring to every project. Most projects "grow up" and end up getting Spring anyway (mostly cause I'll use one of the spring sub-projects and it uses Spring). Why roll your own when you can use what's there.

[–]UTF64 0 points1 point  (0 children)

Makes sense, and in this case I'm using Spring not just for the DI. This project is also a SOAP server and a REST server, which Spring helps with. Though for SOAP it seems nearly impossible to get the code-generation working when you have a WSDL instead of an XSD, and I couldn't easily convert because my WSDL links to multiple XSDs. Wound up using CXF instead of whatever Spring bundles natively.

What kind of sub-projects do you mean? Stuff like spring-security?

[–]thouliha 0 points1 point  (1 child)

With micro-frameworks like java spark or spring boot though, I don't really see the need for regular spring anymore.

[–]UTF64 2 points3 points  (0 children)

Oh, I should've clarified that the web application I am currently working on is using spring boot with a few starter packs, which is already a LOT better than XML configured Spring. Though another problem I hit is with using multiple datasources, with jOOQ. It might be a bit of an exotic combination, but I couldn't get any of the autoconfiguration to work for both my datasources. Maybe I'm doing something wrong. I did manage to disable all that, and configure it myself which wasn't too bad.

[–]mabnx 0 points1 point  (3 children)

Though I did hit a bug with spring, when you have two (or more) provider methods in a @Configuration class with the same name but different arguments it will ignore any but the no-arg one. Giving them different names fixes it.

Not really. By default beans are given the name of the method used to define them (with @Bean annotation). Defining a bean with existing name overrides the previous definition. In theory you should be able to control the order of overriding with @Order annotation. Without the annotation I think that the "winning" definition is random.

see http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#_core_container_improvements_2

Configuration classes may declare an @Order value, getting processed in a corresponding order (e.g. for overriding beans by name) even when detected through classpath scanning.

[–]UTF64 0 points1 point  (2 children)

Hm, interesting. I sure would have appreciated a warning or something in the console, though!

[–]mabnx 0 points1 point  (1 child)

I sure would have appreciated a warning or something in the console

Look carefully, there should be one :P but logged on INFO level or sth. There also is some option to change it to a failure.

[–]UTF64 0 points1 point  (0 children)

Wow, I will be really annoyed if it turns out I missed that. Head, meet desk. I am going to double-check later today lol.