omkar patkar

Ranch Hand
+ Follow
since Aug 25, 2005
Merit badge: grant badges
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by omkar patkar

Volodymyr Levytskyi wrote:If you specify init-method or destroy-method on <bean> tag they override default-init-method or default-destroy-method on <beans> tag.

Java class supplied in <bean> tag is bean definition (recipe) for creating actual instances. All instances behave identically as you configure them via annotations or in XML. Singleton scoped bean is created eagerly (when application is deployed or when spring container(ApplicationContext) is created ).
Prototype scoped bean is created every time it is requested.
You have two beans with the same bean definition(java class). The same initialization or detroy method is called for both beans.

Maybe tutorials at this site and this site will be helpful. Then you may cover this a little more difficult tutorial.
Finally you MUST pass this whole chapter.



Thanks a lot Volodymyr.....that helps a lot to understand the behavior ! ...i will go through these links now...
10 years ago
Hi guys...i am learning spring...and in order to learn spring life cycle i wrote a simple
1) a simple POJO class with an integer member variable and its getter and setter, a pair of default init and destroy method....and two more methods for specific initi and destroy
i have put sysouts in all the getter and setters as well as all the pairs of init and destroy methods.
2) in spring bean definition xml : -

i had two tags of dat bean : -



Now when i test by getting only beanA....i also see sysouts for beanB....allthough i am not getting beanB.....
this gets resolved....when i say scope of beanB as "prototype"..... why does the behavior change then ?

10 years ago
I wish there was some common thing between JDBC classes and the classes in collection framework....
for e.g. .... most general requirement is a ResultSet needs to be converted to some collection, list or map....
so i wish there were APIs in java such that....resultset can be converted to desired collection.
10 years ago
Hello Friends....

I am trying a simple program to see how idref works ... but i am getting exception, so wondering how it works and when to use it?
Please help

Class B: -



and class A: -

package org.spring.tut.helloworld;




the spring xml
: -



and my Tester class: -



i am getting following exception ....


Feb 10, 2013 11:06:14 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@933bcb: startup date [Sun Feb 10 23:06:14 IST 2013]; root of context hierarchy
Feb 10, 2013 11:06:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [DepInject.xml]
Feb 10, 2013 11:06:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@faadb1: defining beans [b,a2]; root of factory hierarchy
Feb 10, 2013 11:06:15 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@faadb1: defining beans [b,a2]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a2' defined in class path resource [DepInject.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.spring.tut.helloworld.B' for property 'objOfB'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.spring.tut.helloworld.B] for property 'objOfB': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.spring.tut.helloworld.testarea.DepInjectTester.main(DepInjectTester.java:15)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.spring.tut.helloworld.B' for property 'objOfB'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.spring.tut.helloworld.B] for property 'objOfB': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:465)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:496)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1376)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1129)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.spring.tut.helloworld.B] for property 'objOfB': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:450)
... 17 more




.... how does idref work ? .... i refered to the spring documentation reference but no explict explanation of the same !
11 years ago
Any one ..... any idea as to what happens when ANT is run, which paths are looked into, when and by which class loaders ?

Thanks and Regards
Omkar Patkar
14 years ago
Thank you for the reply Peter.

I think you are right, it has got something to do with the classloader.

What i did next was, i printed out the classloader being used, so i put following line in my Tester.java



When i run the build.xml (the one i modified to get successfull output!), it prints the classloader as :-



This means, the Sun Microsystem's VM's Classloader is not used. Immediately what i did next was, i ran the same program as a java command,
instead of running it as an ant task. So i executed the program as: -



When i do so, ... the program runs properly without any exception. The classloader displayed at this time is of Sun !



This time, when Tester was executed, as no exception was reported, that means, the Sun's classloader was able to find, necessary Log4j related classes
by looking into the CLASSPATH environment variable! (I found this by printing out the "java.class.path" system property and in case of Sun's classloader, it prints out my CLASSPATH environment variables, and in case of ANTCLASSLOADER, it prints the location where ONLY all the ant related jar files are present!)

... so i went a STEP further to understand the working of ANT.

I created another execute target in the build.xml, But this time i marked the "fork" attribute of this new target as "true" the build.xml looks as follows: -




I did this, so that instead of Ant's classloader .... the Sun's classloader is used, and that the log4j jar file will be picked up from the CLASSPATH directly.
In other words, i was expecting that "executeProject3" will run as if i ran the Tester class using the ordinary java command and that the program will run
successfully.

But i got the same exception as i got earlier of NoClassDefFound for the Logger class !

But surprisingly, although this time, the Sun's classloader is used, the CLASSPATH environment variable is not refered,
... even the system property "java.class.path" prints, the location that Ant class loader used to refer to !

Therefore, obviously, the program fails to execute.

Can someone please tell me, what happens when ANT is run, which paths are looked into, when and by which class loaders ?
Although, this is interesting....it is also confusing !

Thanks and Regards
Omkar Patkar
14 years ago
Hello friends,

I was trying a simple log4j program and using ant to compile and execute the program.
I somehow managed to execute the program, but it initially gave the problems.
I want to understand why after my last attempt the program got executed. Please can someone help me understand this.

Things that i am using:-
-------------------------------------------------
log4j version = 1.2.15
ANT version = 1.7.1
jdk version = 1.6
OS = Windows Vista Home Basic Edition.
-------------------------------------------------

I downloaded the log4j jar file. But as first step, i did not register it into the CLASSPATH environment variable.

First a look at the simple Java program that i wrote: -


Now using the <javac> task of ANT, i tried to compile....but it failed, as Logger class could not be found.

Therefore as first successfull step i registered, the log4j jar in the CLASSPATH environment variable.
After registering, when i ran the ant on the same build.xml it COMPILED SUCCESSFULLY !

Now, i went a step ahead to run the program using the <java> task of ANT.
This time it failed, saying

... "java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.omkar.tester.Tester.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
... 1 more"


....... isn't this incorrect ? ........ because log4j.jar has been registered in the CLASSPATH environment variable already, and a test of successful registration is that the compilation took place properly!

The <java> task in my build file looks like : -

So, another step forward, by reading the error i realized, that the the java needs to load the Log4j related classes only then my Tester class will execute.
So i modified my <java> task in build.xml to include the log4j.jar file (which i copied in lib directory inside my project folder) ..the modified target now looks as : -




.... interestingly now the Tester class got executed ! ... Initially why ANT did not pickup the log4j.jar from the CLASSPATH environment variable at the time of executing ?

I am wondering if at the time of compile it was possible for ANT to pick up the Log4j related classes from the log4j.jar, why it could not do so at the time of running the program ? Why there is a need to specify the log4j jar file explicitily at the time of executing ?

Thanks and Regards
Omkar Patkar
14 years ago
Thanks Tim,

Thats a nice suggestion which i will follow

Thanks and Regards
Omkar Patkar
14 years ago
Thanks Rob!

I tried accessing the file by using the getResourceAsStream() API ... and it worked!

I had to specify the file as if it was only another java file with its package structure (... "dot" replaced with "forward slash")

I mean i accessed the file as "/org/omkar/resources/test.txt" ... now since my package structure is not going to change ... even if i give this to my friend,
the code should run on his machine as well!

Thanks a lot ! ... but i was wondering .... if the way by which i was trying earlier to give relative position of the file by using the syntax of double dots ".." and the file separator is right? .... does JVM understand this syntax that "..\" means go one level up of current directory ? .. or this syntax is understood only by Linux based OS ?

Thanks and Regards
Omkar Patkar
14 years ago
Hello friends,

I want to read a file from a directory which is in parallel to the current directory.
For example, consider the following directory structure: -



In Tester.java i want to read the text.txt file which is actually in a directory parallel to the enclosing directory of Tester class.
I am trying to access the file by giving the path argument to the File class as


... but i get FileNotFoundException ... shouldn't the string argument that i provided go one level up( since i have specified two dots as we do while accessing directory in Linux) and then into the resources directory to access the test.txt ?

I want to access the the test.txt relative to the Tester.java ... because i can give the entire src folder to my friend ... he can put in any directory and try to run it without any change to the code!

How do i access the file relatively ? ... is my way of accessing the file relatively incorrect ?

P.S. By the way i am using Windows Vista Home Basic Edition OS and Java 1.6

Thanks and regards
Omkar Patkar
14 years ago
Hello guys,

Sorry for the late reply, actually internet connection @ my end had problems which got resolved today.
I did search over for finding if there is any way by which, non java files with its package (directory)
structure is maintained as is when source code is compiled... but i did not get any information where ant provides a readymade facility to do so
in compile task.

Therefore, i guess, ... as per David Newton and Diptirmay Rout i will have to copy non java files along with their directories
as a copy task separately.

Thank you so much David and Diptirmay...thank you Cheers !

Thanks and Regards
Omkar Patkar
14 years ago
Hi David,

Thank you for the reply.

Actually, in the user manual of Ant 1.7, the includes attribute has been defined as :-

includes Comma- or space-separated list of files (may be specified using wildcard patterns) that must be included; all .java files are included when omitted.

So i thought of using "includes" in my build file.
It says "list of files" .... and since i have used the wildcard "**" i thought, all directories and files will be considered.
But either my understanding is wrong and as you suggested i might have to use the "copy" task.
But in that case, does that mean for any non ".java" files, ... like ".xml" or ".properties" or ".log" or ".txt" etc.,
i will have to configure the ant build task to "copy" the files with their package structure individually ?
... is their any facility in ant that will take care of this automatically?

Thanks and Regards
14 years ago
Hi guys,

I am using Ant 1.7.0 2008 build and jdk 1.6 on Windows Vista Home Basic Edition OS. I am trying to compile my project using ant.
But the property file is not getting outputted in the classes folder. The folder and file organisation of my project is as follows: -



for the above structure i have made the build.xml as follows: -



I execute the ant tool as : -


The Build is successfull ... that is the Tester.class file is created in appropriate packag structure ....
but when i check the build directory which contains the classes created after compilation, then its found that the resources directory is not created, also the property file is not outputted as the resources directory is not available.
Tester.java is expected to use the property file, but currently Tester.java just contains one line of System.out.println("Hello World!"); that's it.

The structure after executing ant ...looks as follows: -


As you can see, the package structure for property file was not created.
Why does the ant task does not create the package structure for property file?
How do i make ant do that? ......So that i can refer to that in the Tester.java code later?

Thanks in advance!
14 years ago
Hello guys,

I would like to share, one more interesting thing i came across, while trying out further more on this example.

As we had analyzed, the instantiation of singleton beans by container at load time is done ONLY by APPLICATIONCONTEXT implementation of spring container.
Simple implementations of BEANFACTORY (.... i.e., NON-APPLICATIONCONTEXT based containers) will instantiate the beans only when requested, without even
specifying the "scope=prototype" or "lazy-init=true" for the bean declaration of the "task1" and "task2".

In other words, as per my first post, (the program in which i was getting same date time stamp for the 2 task objects), if instead of using ApplicationContext as the container, if i use the XMLBeanFactory (simple implementation of BeanFactory), I will get different timestamps for the task objects without the need of me declaring the "task1" and "task2" beans as "scope=prototype" or "lazt-init=true".



Thanks and Regards
14 years ago
Thank you Kengkaj ... i think i got your point.

I slightly amended the spring config file, and i got the expected results that is different dates for the Task objects.

What i did is, i changed the scope of the bean declarations of "task1" and "task2" objects to "prototype" and i got the expected result.
Or,
If i add the "lazy-init" attribute as "true" to the bean declaration of "task1" and "task2" objects, even then i get expected results.

From this i have concluded that, Spring instantiates all singleton beans FIRST and keeps them in the container.
And only when, call is made for instantion of non singleton beans, they are instantiated.

Base on this ... in our example, following things might have happened in sequence.
1) When context object was created, at that time, it loaded definitions of all singleton beans and tried to instantiate them.
2) In this process, the container came to know that "task1" and "task2" objects are SINGLETON beans.
3) Container comes across bean definition of "task1" first, so it tries to instantiate it.
4) During instantiation, it realizes there is dependency on another bean "currentDateTime" which is NON SINGLETON. So it creates a date object and assigns
reference to the "taskCreationDate" property of the "task1".
5) Similary container comes across bean definition of "task2", so it tries to instantiates it.
6) During instantiation, it realizes there is dependency on another bean "currentDateTime" which is NON SINGLETON. So it creates a date object and assigns
reference to the "taskCreationDate" property of the "task2".
7) The date objects created in step 4) and 6) are different as they differ in milliseconds which is not visible in the "toString()" method of Date class. Steps 2) to 3) and steps 5) to 6) were executed at load time, and hence there is hardly any difference in the values of date objects of the two classes.
8) When the "task1" object is asked to the container using the getBean(), it returns the one it created at load time.
9) After 2 seconds, "task2" object is asked to the container using the getBean(), it returns the one it created at load time, that is the reason why visibly there was no difference of 2 seconds in the date of the two task objects.
10) When "currentDateTime" object is asked to the container using getBean(), container knows, this is a "prototype" or "NONSINGLETON" bean, so it again creates a new instance... but by now 2 seconds have passed by since the last time it created a date object. That is the reason why the latest access of
"currentDateTime" displays a delay of 2 seconds.


The above life cycle was changed when the "task1" and "task2" objects were declared as "prototype" or there "lazy-init" was declared as "true".
By doing so, they were not instantiated at load time, ... instead they were instantiated only when requested at the call of "getBean()"
and hence the result were as expected.

I hope this understanding of mine is correct.
Kindly correct me if i am wrong.

Thanks and Regards
Omkar Patkar
14 years ago