Categories
discuss

Getting Spring Application Context

Is there a way to statically/globally request a copy of the ApplicationContext in a Spring application?

Assuming the main class starts up and initializes the application context, does it need to pass that down through the call stack to any classes that need it, or is there a way for a class to ask for the previously created context? (Which I assume has to be a singleton?)

Answer

If the object that needs access to the container is a bean in the container, just implement the BeanFactoryAware or ApplicationContextAware interfaces.

If an object outside the container needs access to the container, I’ve used a standard GoF singleton pattern for the spring container. That way, you only have one singleton in your application, the rest are all singleton beans in the container.

Categories
discuss

Difference between int[] array and int array[]

I have recently been thinking about the difference between the two ways of defining an array:

  1. int[] array
  2. int array[]

Is there a difference?

Answer

They are semantically identical. The int array[] syntax was only added to help C programmers get used to java.

int[] array is much preferable, and less confusing.

Categories
discuss

Is it possible to compile a Rails app to a Java VM JAR file?

Essentially the only thing I can deploy to my deployment machine is a JAR file. I can’t install JRuby, nor can I install Glassfish or Tomcat.

Is it possible to package up a Rails application (including Rails, vendored, of course) to a JAR file such that I can do

c:my_server> java rails_app.jar

and have it run WEBRick or Mongrel within the JVM?

Answer

I’d recommend that you checkout Jetty. The process for Embedding Jetty is surprisingly easy, and it should be possible to give it your servlets from your current jar file. I haven’t used Ruby/Rails, though, so I’m not sure if there are any complications there.

Is it normally possible to embed all of your rails templates/models into a jar inside of a war file for deployment on Tomcat? If so, then you should be able to get embedded Jetty to pull it from your single jar as well.

Categories
discuss

Java, Swing: how do I set the maximum width of a JTextField?

I’m writing a custom file selection component. In my UI, first the user clicks a button, which pops a JFileChooser; when it is closed, the absolute path of the selected file is written to a JTextField.

The problem is, absolute paths are usually long, which causes the text field to enlarge, making its container too wide.

I’ve tried this, but it didn’t do anything, the text field is still too wide:

fileNameTextField.setMaximumSize(new java.awt.Dimension(450, 2147483647));

Currently, when it is empty, it is already 400px long, because of GridBagConstraints attached to it.

I’d like it to be like text fields in HTML pages, which have a fixed size and do not enlarge when the input is too long.

So, how do I set the max size for a JTextField ?

Answer

It may depend on the layout manager your text field is in. Some layout managers expand and some do not. Some expand only in some cases, others always.

I’m assuming you’re doing

filedNameTextField = new JTextField(80); // 80 == columns

If so, for most reasonable layouts, the field should not change size (at least, it shouldn’t grow). Often layout managers behave badly when put into JScrollPanes.

In my experience, trying to control the sizes via setMaximumSize and setPreferredWidth and so on are precarious at best. Swing decided on its own with the layout manager and there’s little you can do about it.

All that being said, I have no had the problem you are experiencing, which leads me to believe that some judicious use of a layout manager will solve the problem.

Categories
discuss

Why is it not a good idea to use SOAP for communicating with the front end (ie web browser)?

Why is it not a good idea to use SOAP for communicating with the front end? For example, a web browser using JavaScript.

Answer

  • Because it’s bloated
  • Because JSON is natively understandable by the JavaScript
  • Because XML isn’t fast to manipulate with JavaScript.
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..