How to create spring configuration the easy way - java

Is there any way to create the spring configuration more easily? I mean if I want to create a spring+hibernate project then there should be some wizard which asks location of the DAO file, Service file, Controller and then the wizard will automatically create all these classes with required annotations. In this way we will not forget to type any annotations or need to keep track of so many classes. Besides there could be some dialog which would allow to input the value of different configuration parameters such as connection URL, user name, etc so that I don't have to type and misspell any configuration parameter name. For instance, instead of having to type "hibernate.dialect" there could be a drop down list from where we can select the all available dialects. I am not sure whether this kind of tool or plugin exists. I think it would make things super fast to deploy because much time is wasted for fixing configuration issues even for experienced developer.
Please let me know whether any such tool exists.
Thanks

You can use Spring initializer in order to generate the skeleton for your project, with the relevant spring dependencies.
Aside from that, you can try looking for some maven archetypes that might meet your needs (look here)

Related

What's the recommended way in Java to make a piece of code available only when needed?

The question regards an endpoint that I want to make available only for demoing and should not be part of the project in production. Therefore I need to find a way of making the piece of code that reveals this endpoint available only when it should be.
I thought of using a different .properties file when it is needed, but this requires creating another one and changing the configuration and if there is a more simple way I would like to know.
Maybe building with a different Maven profile? Can I use the Maven profile name inside the code?

How should I externalize configuration for a jar in a tomcat webapp?

I'm working on a small project for my company for converting currency. The plan is to provide a jar file with a minimal interface to keep things simple for my coworkers. The problem I'm running into is determining the best way to provide configuration for the jar.
See the jar needs to know some information that I don't want the client code to necessarily need to worry about, specifically the location of the web service that is being consumed.
Here is the interface for context, but please don't get too distracted critiqing it :)
public static ConversionResult convert(String amount,
String currencyCodeFrom, String currencyCodeTo);
The user will get the jar during the maven build and then used within tomcat webapp. Then they will need to setup configuration somehow, and I'm having a hard time deciding which route to take.
Here are the options I'm considering:
Setup JNDI in Tomcat
Resource bundles
Spring PropertyOverrideConfigurer
Something custom
Given this setup, what is the best way to do this? Is there a better option that I'm missing?
You have given good options. One more would be Dependency Injection. Guice is a good framework for that.

How to move hibernate related code to its own 'project' so I can share it?

If I want to be able to re-use my hibernate related code with multiple IntelliJ solutions, what should I do?
Should I move my models (with annotations) and Dao's and service classes to their own module?
How would I then be able to re-use this module/project with other intellij solutions?
I guess they would have to compile down to a seperate .jar right?
It is possible to configure an IDEA project to point to a module in an external location. So you could configure multiple IDEA projects to point to the same hibernate module. This is a solution for a one-man show, primarily (although see here about using a variable to make this location configurable).
In order to make this distributable and sharable among multiple developers, you are looking at building a jar out of one module, or if it has no particular meaning to any specific project, making a new project that has the code and produces the jar, which other projects then have as a library.
You can use Spring or Guice for dependency injection. Refactor your dao/services to use generic, so if your children modules don't share the same pojo you can still reuse all your hibernate codes (for dao and services) without any duplications (although you might want to make them abstract, in this case)

Is it possible to check that class exists while editing Spring XML files?

I use Spring with long xml files for beans.
Is it possible at edit time to check if the names of the classes are correct and all references exist?
Try using STS, customized eclipse IDE with plugins, from Springsource. It have even auto-completion, error-checking features for writing spring's configuration(bean.xml) files.
UPDATE
For further details & features: Features of STS
As others have mentioned, this is the responsibility of an IDE before application runtime, or a simpler class that attempts to load the configuration if spinning up the app is cost-prohibitive.
The major IDEs (Elipse, NetBeans, and Intellij) all have strong Spring support. This includes class completion, jumping between config file usage(s) and definition, gutter marks to indicate a class is a Spring bean, etc.
Many Spring supported IDEs make suggestion of what the class name should be during editing. For example, Netbeans suggests this while you are typing the class names or other known attributes. However, it cannot be forced to check whether the class names you use really exist.
Without additional coding effort (for example writing your own xml editor), I do not think so. That's why annotations have been invented. You can move your configuration from xml files to annotated Java classes and methods. Then your compiler will do the work automatically.

Implementing dynamic plugins in Java

I'd like to implement a dynamic plugin feature in a Java application. Ideally:
The application would define an interface Plugin with a method like getCapabilities().
A plugin would be a JAR pluginX.jar containing a class PluginXImpl implementing Plugin (and maybe some others).
The user would put pluginX.jar in a special directory or set a configuration parameter pointing to it. The user should not necessarily have to include pluginX.jar in their classpath.
The application would find PluginXImpl (maybe via the JAR manifest, maybe by reflection) and add it to a registry.
The client could get an instance of PluginXImpl, e.g., by invoking a method like getPluginWithCapabilities("X"). The user should not necessarily have to know the name of the plugin.
I've got a sense I should be able to do this with peaberry, but I can't make any sense of the documentation. I've invested some time in learning Guice, so my preferred answer would not be "use Spring Dynamic Modules."
Can anybody give me a simple idea of how to go about doing this using Guice/peaberry, OSGi, or just plain Java?
This is actually quite easy using plain Java means:
Since you don't want the user to configure the classpath before starting the application, I would first create a URLClassLoader with an array of URLs to the files in your plugin directory. Use File.listFiles to find all plugin jars and then File.toURI().toURL() to get a URL to each file. You should pass the system classloader (ClassLoader.getSystemClassLoader()) as a parent to your URLClassLoader.
If the plugin jars contain a configuration file in META-INF/services as described in the API documentation for java.util.ServiceLoader, you can now use ServiceLoader.load(Plugin.class, myUrlClassLoader) to obatin a service loader for your Plugin interface and call iterator() on it to get instances of all configured Plugin implementations.
You still have to provide your own wrapper around this to filter plugin capabilites, but that shouldn't be too much trouble, I suppose.
OSGI would be fine if you want to replace the plugins during runtime i.g. for bugfixes in a 24/7 environment. I played a while with OSGI but it took too much time, because it wasn't a requirement, and you need a plan b if you remove a bundle.
My humble solution then was, providing a properties files with the class names of plugin descriptor classes and let the server call them to register (including quering their capabilities).
This is obvious suboptimal but I can't wait to read the accepted answer.
Any chance you can leverage the Service Provider Interface?
The best way to implement plug-ins with Guice is with Multibindings. The linked page goes into detail on how to use multibindings to host plugins.
Apologize if you know this, but check out the forName method of Class. It is used at least in JDBC to dynamically load the DBMS-specific driver classes runtime by class name.
Then I guess it would not be difficult to enumerate all class/jar files in a directory, load each of them, and define an interface for a static method getCapabilities() (or any name you choose) that returns their capabilities/description in whatever terms and format that makes sense for your system.

Categories

Resources