Setting up spring framework is very easy.
Follow the steps below and you are good to go.
download latest version of spring framework from http://www.springsource.org/download
download apache commons logging binary jars from http://commons.apache.org/
create a java project using eclipse and set the jar file to the build path.
Now you can use the spring framework.
That is it.
Sunday, July 29, 2012
spring what is dependency injection?
Dependency injection is a main feature of spring framework. This basically enables the injection of dependencies to a bean.
The example below explains this concept.
The example below explains this concept.
Say for an example there is a college class. Then this entity has a list of students, books, lecturers etc... Now we know that the students, books, lecturers are dependencies of college. That is the college class will have to use instances of the above mentioned dependencies. So we can inject these dependencies through an xml configuration file. Now if we call for the college bean through the ApplicationContext class for a bean of college, it will automatically pass the other dependencies as well (a list of student, books and lecturers.). The bean will have initial configured status with the dependencies. Therefore eliminating the requirement of making a list of dependencies by using new keyword for each of the dependency objects.
By default the beans follows the singleton pattern unless you specify the scope to something else. If you specify the scope to prototype, then spring will create object when a new bean is requested from spring. Other than singleton and prototype, there are 3 other scopes and they are basically for web based applications.
Saturday, July 28, 2012
java apache commons logging
apache commons logging focuses on all aspects of reusable Java components.
It contains 3 main parts
The Commons Proper - A repository of reusable Java components.
The Commons Sandbox - A workspace for Java component development.
The Commons Dormant - A repository of components that are currently inactive.
It contains 3 main parts
Friday, July 27, 2012
spring framework charastaristics
spring is a framework used to make the life of a java programmer easy. Well when you talk about spring framework it has some important characteristics.
1) Dependancy Injection - When you write a complex java code, you write classes to be as much as independent as possible and DI helps to glue them together while keeping them independent.
2) Aspect oriented programming (AOP) - This helps you to decouple cross-cutting concerns like security, logging, declarative transactions, caching.
apache tomcat - how to change the default port
open your server.xml file (in ubuntu tomcat installation it is /etc/tomcat7/server.xml)
then locate the code similar to
< Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
or
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
then change the port number to any number you like.
adding usesrs to tomcat in linux
go to the /etc/tomcat7
then type sudo vim tomcat-users.xml
then enter the following in the tomcat-users element
a role name grants certain privileges to certain users. now you have created a tomcat user and in this case a user with manager privileges.
computational reflection
Java is a programming language that has the computational reflection. That is it has the ability to examine and modify the structure of the program at run time. The package java.lang.reflect has the above mentioned facility.
Tuesday, July 24, 2012
Java How to change jvm memory settings.
If you a using ecplise then you can set memory settings for a particular applicartion by simply setting VM arguments in it's run configuration as
-Xms128m -Xmx512m.In this particular instance -Xms means the minimum memory and it is 128mb. -Xmx is maximum memory and it is 512mb.
If you want to change the memory in system jvm then simply run java command in a terminal with -Xms281m -Xmx512m parameters given that the class path variable is set.
Sunday, July 22, 2012
Ebook Management software.
Recently I have been searching for an opensource ebook management software that supports all sorts of document types ( such as pdf, epub,mobi etc...). Then I found a nice ebook library software called calibre. Only weights 32 mb and has a nice interface. more importantly you can store your ebooks based on the genre and has the bookmarking capability. Try it out, you will have a whole library with lots of ebooks that you never imagined. :)
find pid of a process in linux
you can simply type
pidof <programm name>
or also you can do this
ps -aux | grep <programm name>
you can kill the process by typing as below
kill -9 <pid>
some other important linux commands
create a simlink : ln -s <current directory> <directory to which the link should go>
providing ownership of a certain directory to a specific user
sudo chown <user name> <directory to provide ownership> -R
pidof <programm name>
or also you can do this
ps -aux | grep <programm name>
you can kill the process by typing as below
kill -9 <pid>
some other important linux commands
create a simlink : ln -s <current directory> <directory to which the link should go>
providing ownership of a certain directory to a specific user
sudo chown <user name> <directory to provide ownership> -R
java block variables
class A{
int x = 0;
{x = 7; int x2=3;} //init block
void doStuff(){
//do something
}
}
x2 is an init block variable.
block variables cannot be accessed after the execution of the code block.
int x = 0;
{x = 7; int x2=3;} //init block
void doStuff(){
//do something
}
}
x2 is an init block variable.
block variables cannot be accessed after the execution of the code block.
Friday, July 20, 2012
Tuesday, July 17, 2012
java singleton, when you want to avoid multiple instances but one.
Singleton is a creational design pattern used to prevent from creating duplicate objects from a certain class. That is it limits the class to create only one object. Below example shows how to do this.
static Object o;
public static Object getObject(){
if(o==null)
o= new Object();
return o;
}
This is actually a specialization of java's static factory method design pattern.
static Object o;
public static Object getObject(){
if(o==null)
o= new Object();
return o;
}
This is actually a specialization of java's static factory method design pattern.
Java how to invoke a static method using a string value
You can invoke a static method as below
Let's say you need to call SimpleClass.simpleMethod(int x);
Method m =
SimpleClass.class.getDeclaredMethod("simpleMethod", Integer.TYPE); m.setAccessible(true); //if security settings allow this Object o = m.invoke(null, 20); //use null if the method is static
java invoke methods by a string name.
Let's assume that you have a string value and the method name also have the same name as the string value. If you want to invoke the the method using the
string value you can do it like this,
string value you can do it like this,
Object obj;//object class which the method has
String methodName = "getName" //string that you need to invoke the method.
THEN GET THE METHOD LIKE THIS
java.lang.reflect.Method method;
try {
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
// ...
} catch (NoSuchMethodException e) {
// ...
}
AFTER THAT YOU CAN INOKE IT LIKE THIS
try {
method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
Java associative array
Today I wanted to use an associative array in java and came to know that java does not have associative aarays. how ever there is a similar data structure you can use instead of an associative array. That is a hash map.
Map<String, Object> map = new HashMap<>(String, Object);
to put objects in the map,
map.put("key","value");
to retrieve, you can use the key as below.
map.get("key");
Map<String, Object> map = new HashMap<>(String, Object);
to put objects in the map,
map.put("key","value");
to retrieve, you can use the key as below.
map.get("key");
Jdbc driver time out.
Recently I deployed an app on glassfish and it worked really well for about half a day and then it threw an exception each time the service was called. Following is the excetion
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No
operations allowed after statement closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl. java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Deleg
Later found out that the jdbc driver connection automatically
expires after 12 hours.
Setting conncetTimeout to 0 in the
jdbc connect url as a parameter resolved the problem.
java split with "."
String a = "a.jpg";
String str = a.split(".")[0];
This will throw ArrayOutOfBoundException
Why is this?
This is because "." is a reserved character in regular expression, representing any character.
Instead, we should use the following statement:
String str = a.split("\\.")[0];
When the code is compiled, the regular expression is known as "\.", which is what we want it to be
Subscribe to:
Posts (Atom)