Spring Framework provides a wonderful abstraction layer for low-level resource access in Java (the Resource and ResourceLoader interfaces). I'm developing a library which should not be dependent on Spring, and am looking for an equivalent for this capability in a stand-alone Java library. Anyone familiar with one?
Why not just use the one from Spring by itself? It doesn't look like it has any dependencies on the rest of Spring.
Edit
Not sure I understand the problem - you want something that does exactly the same thing, but doesn't come from Spring? Is it a licensing issue?
You only need half a dozen classes from it, if you don't want to add them as an extra jar, move them to your own namespace (good idea anyway, in case you do use Spring at some point, after all) and distribute with your library. Again, assuming your licensing allows it.
If licensing isn't the problem, can you be more specific about why you can't use the Spring implementation in your own library?
You can use Jsr-303 (DI) to match your requirments. The most known implementation is Google Guice. It's a javaEE standard (maybe JavaSE).
Related
Is there anyway to map routes without conf/routes file in play framework. like #HttpRequest(url="/someurl")
Not out of the box. See this issue:
https://github.com/playframework/playframework/issues/1139
But, there are some reasons to not do it and I suggest you to don't fight the framework. If Play uses /conf/routes, stick to it because everything will evolve around this way of doing things. Per instance, by using a custom router that supports annotations, you will probably give up:
Reverse routing for your views and other controllers
Default controllers offered by the framework (controllers.Assets) or by module creators
Possible performance loss if your solution is not as fast as the default one.
Not sure if it is worth.
If you prefer to have your routing embedded in your controller rather than in a separate config file play 2.5 actually now supports this through its String Interpolating Routing DSL (a mouthful i know).
here's the docs for it:
https://www.playframework.com/documentation/2.5.x/ScalaSirdRouter
I am writing a library which contains a domain model and uses the Bean Validation API. My goal is to have minimal amount of dependencies. Hence, without CDI, Java EE and Spring. Allowed Dependencies are to APIs only like JSR-349 and JSR-330 API.
I can not make any assumptions about how my library is going to be used. It might be within a container or as desktop application. Forcing the library user to have an CDI, Spring, or validation implementation is not an option.
Right now, I use the bean validation API to allow the user of my library to validate the model itself. But I would also like to use Method Validation in some cases.
My questions are:
What options do I have if I want to use method validation within a
library project?
Do I have to ship my library with an aspectj runtime dependency?
Does it make sense to use Method Validation in a domain model?
How is your library going to be used? Chances are that applications using it are running within CDI, Spring or another kind of container anyways, then delegating method validation to the same would be sensible.
If you really want to go for your own solution, it really depends on how the instances of your domain model are created. If you have interfaces and the user obtains instances via a factory, you might have a look at JDK dynamic proxies. Alternatively, you could check out Javassist or Cglib if you don't work with interfaces but with classes. That'd still require that your domain model nodes are obtained via a factory in order to return properly proxied instances.
Whether it makes sense or not surely depends on your specific model and its use cases. When it comes to validation of a model, property (and class-level) validation surely is the more common case, but if you provide business methods on your model and want to validate their parameters or return values, it may make sense.
1) without CDI I would recommend apache BVal or any jsr303 specific implementation that does not require being container managed.
Since the jsr targets the java-ee platform it seems some container must manage it. what container you choose depends on the extra dependencies that must be shipped with the application.
Bval itself does not require anything other some core apache helper libraries.
2) Bval and other implementations of the jsr303 spec will need some kind of java ee container. It is a backing principle behind java ee. If no java-ee enviroment is present, bval will require a di framework to hook into.
If guice is chosen as the container; it uses cglib for its bytecode and it uses aop alliance interfaces for its aop (implemented internally with the help of cglib).
Spring does require aspectj.
If CDI is chosen, it would depend on the CDI implementation used.
3) If you are attempting to simply do method level validation, it does make sense to simply do the validation by hand in the class setter/getter itself. Specially if you want to remain platform independant.
Method level validation through the use of third party libraries only makes sense when you are using third party containers. If you are trying to have a base simple java se application, it does make a great deal of sense to put the validation in the data objects themselves and have your exception handling strategy take care of issues to the user.
The answer to three will always be rather subjective, but if you really are not looking to use a mass amount of frameworks, I don't think its bad practice to do the validation in the methods themselves.
I'm working on a 10-year-old Java webapp, and I would like to introduce some new technology into the project. One of the things I would like to start doing is dependency injection. I know the Spring Framework has the capability to do dependency injection, but I am having a hard time integrating the framework into the old project.
Could someone provide an example of what I would have to change in my web.xml, other files I would have to add, and other changes I would need to make? I want the smallest Spring footprint while still being bale to do dependency injection.
There are a lot of examples online about starting a new project using Spring, but I can't find any about integrating Spring into an old project.
Thanks.
You'll have to start by adding a context loader listener into your web.xml, along with the locations of the Spring configuration XML files.
You should configure the Spring DispatcherServlet to accept all URLs that you wish for it to handle.
You should write Controllers to bind and validate HTTP requests, call services, add data to ModelAndView for rendering, and map JSPs to success/failure views as needed.
You should put interfaces in front of your service and persistence tiers. Move implementations into implementation classes that Spring can inject.
Leverage Spring AOP for security and transactions and logging as needed.
Throwing new technology at a project wont make it faster\better, unless you introduce the new technology to all parts of the project. The idea behind DI is to lose dependencies between objects. The project probably is tightly coupled, so you'd have to rewrite at least parts of the thing. Depending on the size, this can be a monster to beat - ask yourself if this is worth it, if it has any positive effect on the project other than introducing new technology.
The reason why there are little to none tutorials about integrating DI container into an old project is quite simple: it usually doesn't make any sense. Either you use the pattern in all places, or none at all. The bastard child that would be creating by mixing both would be a horror to maintain. I'd really advise you think about why you want to introduce a DI container into that 10 year project. Unless there is a real good reason for doing it (and you are happy with rewriting a lot of code) don't do it.
we have a frontend application that that uses Swing. We use Spring framework, but currently it is used just to autowire few beans...
What are reasonable next steps to use Spring more often?
Is it worth for non web application?
What would be advantages and disadvantages?
The advantages of using Spring (or any other dependency-injection) framework, is that you get a (hopefully) loosely coupled system, i.e you classes does not create instances of their collaborators, so you can easily change the implementation.
This is widely known as the Inversion-of-control principle (IoC, also the I in SOLID), and this is a good principle to follow. This means that spring is not limited to web applications, but can be used in any application that want to use an IoC-container (which is basically what spring-core is).
Disadvantages:
This really depends on how you look at things. There is more code (you have to define a entry-point for the injected collaborators), but that also makes the code more testable (the entry-points are seams which you can use to inject mocks and stubs in testing).
Also, you can't look at the code and immediately see which implementation of the collaborators that are used. But that also makes for good code, since you depend on interfaces, not implementations.
You get more config: either in an xml-file (old-style spring), or with annotations. Up until recently you had to rely on non-standard spring annotations to inject (#Autowired) resources, but now you can use the standard java dependency injection annotations, which means that you can switch out spring as your IoC-container without changing your code.
There are probably alot more advantages and disadvantages to using spring in your application, but this should get you started on deciding if using Dependency Inversion is a good thing for your application
More to the point of your question about Swing and Spring. In an application I have been working on we have been using spring to wire up the whole application. The different dialogs get their logic injected (no application logic should (in my opinion) be located together with gui logic). We are using JPA/hibernate as the database-layer, so we use spring spring to create and inject the entitymanager to our DAOs, and set up transactional settings.
I've written swing UI's that are backed by spring.
cons
the startup can be slower but you have to have a large app for that to happen.
and a splashscreen is a good idea in those situations.
its easy to "overbean" or over-zealously make everything a bean, which gets messy.
pros
spring works fine behind a GUI.
it provides a lot of services you can use
the obvious dependency injection and decoupling
a global event system, simpifying some of your own event listeners, for events that will only ever be fired by one source
resource accessing
database access is eays in 2 tier apps
rpc for 3 tier apps is easy
There are other services the spring application context provides, but that I haven't used.
If you go this direction, also look into the java-based configuration for spring, which is new in 3.0. I find that helpful as well, as it makes my spring configuration type-safe.
One disadvantage of using Spring in a Swing application is that Spring DI will make startup slower.
well one would be , if you ever decide to migrate to a web app , all you need ( well almost) to change would be the views. That's the beauty of MVC applications.
For instance, I am using JSF + custom framework developed in our company. Now I want to use a third party validation framework that can be used as an plug-in and it should not create any dependency what ever may be the technical stack.
So my question is does spring provide any framework of that sort or if it's available how can I use that?
I am expecting a validation framework something like, which is configurable through XML.
Spring does have a validation framework, but if you want minimal dependencies, then I'd suggest that you go with a Bean validation provider. It's a new(ish) official validation standard, defined in JSR-303.
There are several implementations at the moment. I'd give Hibernate Validator a look.
I disagree. Hibernate Validator is an awful piece of software (at least the versions that were current about a year ago). Spring Validation is a nice piece of software, that goes together well with the BeanWrapper interface.
But it's true: Spring Hibernate resides inside the Spring Context jar, which is unnecessary overhead. Hopefully there will be a separate version sometime.