many people asked me to help them with a sample of spring to know the basic as well as about logging using log4j
So i have made an application that allows you to get a gist of how both of them work.
Before seeing the code go through the following data:
*********************************************************************
Layout pattern :
%d -> date
%p -> priority
%F -> filename
%t -> thread
%c -> class
%L -> line no
%M -> method
%m -> message
%n -> newline character
*********************************************************************
Steps:
1. create a package my.spring and copy the java files into it
2. paste the applicationContext.xml and the log4j.properties into the source folder but outside the package.
3. Add the requisite jar files as mentioned in the next section.
********************************************************************
Required jar files:
common-logging.jar
log4j-1.2.11.jar
spring-aop.jar
spring-beans.jar
spring-context-support.jar
spring-context.jar
spring-core.jar
spring-jdbc.jar//optional
spring-jms.jarspring-orm.jar//optional
spring-test.jar
spring-tx.jar//optional
spring-web.jar//optional
spring-webmvc-portlet.jar//optional
spring-webmvc-struts.jar//optional
spring-webmvc.jar//optional
spring.jar
The optional jars are to be used in case when the database and web service coding needs to be done
*********************************************************************
//person.java
package my.spring;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Person
{
static Logger l=Logger.getLogger("my.spring.Person");// creating the logger instance for the the respective class.
String name, address;
// constructor for the sake of constructor injection against setter injection...
public Person()
{
}
public Person(String name, String address)
{
BasicConfigurator.configure();// for writing the logger values to the console.
l.info("Using constructor injection.");//adding the statements to be printed.
this.name=name;
this.address=address;
}
/*Getter and Setter function definition for all the data members of the class.*/
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
}
//Trainee.java
package my.spring;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Trainee
{
static Logger l=Logger.getLogger("my.spring.Trainee");// creating the logger instance for the the respective class.
String eid,post ;
Person person;//object reference to the person class as a data member here
/*Getter and Setter function definition for all the data members of the class.*/
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
public String getEid()
{
return eid;
}
public void setEid(String eid)
{
this.eid = eid;
}
public String getPost()
{
return post;
}
public void setPost(String post)
{
this.post = post;
}
public String toString()//overriding of the function to print requisite values..
{
BasicConfigurator.configure();// for writing the logger values to the console.
l.info("Using the overridden toString method.");//adding the statements to be printed.
return "Employee id : "+getEid()+", Employee name :"+getPerson().getName()+", Employee Address : "+getPerson().getAddress()+", Post : "+getPost();
}
}
//TestMe.java
package my.spring;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
/*Use below import for ClassPathXmlApplicationContext calling*/
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*Use this import statement for the FileSystemXmlApplicationContext calling..*/
/* import org.springframework.context.support.FileSystemXmlApplicationContext;
*/
public class TestMe
{
static Logger l=Logger.getLogger("my.spring.TestMe");// creating the logger instance for the the respective class.
public static void main(String args[])
{
BasicConfigurator.configure();// for writing the logger values to the console.
l.info("Started the application");//adding the statements to be printed.
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
BeanFactory beanfactory = new ClassPathXmlApplicationContext("applicationContext.xml");//Loads the file for parsing and initializing
/*
BeanFactory beanfactory = new FileSystemXmlApplicationContext("C:/Users/Ajo.Koshy/Mage/workspace/SpringTry1/bin/applicationContext.xml");
*/
Trainee t=(Trainee)beanfactory.getBean("trainee");//Lazy loading of the bean required to be called.
try
{
System.out.println("Enter your name : ");
t.getPerson().setName(br.readLine());
//Add other properties as required to be added to set.
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(t.toString());
l.info("Exiting the application");
}
}
//applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Bean 1-->
<bean id="trainee" class="my.spring.Trainee">
<property name="eid" value="300695"/>
<property name="post" value="Software Engineer"/>
<property name="person" ><ref bean="person"/>
</property>
</bean>
<!--Bean 2 as reference to a value of Bean 1 -->
<bean id="person" class="my.spring.Person">
<!--Uncomment the following for dependency injection-->
<!--<property name="name" value="Ajo Koshy"/>
<property name="address" value="Noida"/>
--><!--This can be used for constructor setting-->
<constructor-arg index="0" value="Ajo Koshy"/>
<constructor-arg index="1" value="Noida"/>
</bean>
</beans>
//log4j.properties
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.Trainee=org.apache.log4j.RollingFileAppender
log4j.appender.Person=org.apache.log4j.RollingFileAppender
log4j.appender.TestMe=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=C:/mylog.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=*Calling from line no %L* [%d] (%p) %F %t %c {%M}- %m%n
log4j.appender.Trainee.File=C:/Traineelog.log
log4j.appender.Trainee.MaxFileSize=2MB
log4j.appender.Trainee.MaxBackupIndex=2
log4j.appender.Trainee.layout = org.apache.log4j.PatternLayout
log4j.appender.Trainee.layout.ConversionPattern=*Calling from line no %L* [%d](%p) %F %t %c {%M}- %m%n
log4j.appender.Person.File=C:/Personlog.log
log4j.appender.Person.MaxFileSize=2MB
log4j.appender.Person.MaxBackupIndex=2
log4j.appender.Person.layout = org.apache.log4j.PatternLayout
log4j.appender.Person.layout.ConversionPattern=*Calling from line no %L* [%d](%p) %F %t %c {%M}- %m%n
log4j.appender.TestMe.File=C:/TestMelog.log
log4j.appender.TestMe.MaxFileSize=2MB
log4j.appender.TestMe.MaxBackupIndex=2
log4j.appender.TestMe.layout = org.apache.log4j.PatternLayout
log4j.appender.TestMe.layout.ConversionPattern=*Calling from line no %L* [%d](%p) %F %t %c {%M}- %m%n
log4j.category.my.spring.TestMe = INFO, TestMe
log4j.category.my.spring.Person = INFO, Person
log4j.category.my.spring.Trainee = INFO, Trainee
log4j.rootLogger = INFO, rollingFile
for access to code go to https://docs.google.com/document/d/1kq9-kB946i2UjJES8iVWCHe5r43akbKEwY1H_YA4Tcw/edit?hl=en_US
ReplyDeletegreat wrk man... Copy tho thumney humsey seekhi ahh...
ReplyDeleteits not copied. the link is a repository on googledocs created by me
ReplyDeleteWhich Software is used to develop this application :)
ReplyDeleteeclipse
ReplyDeleteErzurum
ReplyDeleteElazığ
Konya
Zonguldak
Eskişehir
1TRH
Adana
ReplyDeleteElazığ
Kayseri
Şırnak
Antep
LB0
Muğla
ReplyDeleteSamsun
Eskişehir
Sakarya
Kars
RGWX
Mardin
ReplyDeleteistanbul
Çanakkale
Antep
Elazığ
VEGV0U
Iğdır
ReplyDeleteAdana
Karabük
Diyarbakır
Antep
ZJLCİ
C2E04
ReplyDeleteArtvin Lojistik
Samsun Lojistik
Bursa Parça Eşya Taşıma
Kars Lojistik
Isparta Parça Eşya Taşıma
A3F4E
ReplyDeleteMuş Şehir İçi Nakliyat
Tunceli Parça Eşya Taşıma
Trabzon Evden Eve Nakliyat
Batıkent Boya Ustası
Bartın Şehir İçi Nakliyat
Karapürçek Fayans Ustası
Kırşehir Evden Eve Nakliyat
Urfa Şehir İçi Nakliyat
Erzincan Şehir İçi Nakliyat
69662
ReplyDeleteDenizli Şehir İçi Nakliyat
Sincan Parke Ustası
Balıkesir Parça Eşya Taşıma
Ünye Televizyon Tamircisi
Okex Güvenilir mi
Rize Şehir İçi Nakliyat
Batman Lojistik
Ünye Çelik Kapı
Balıkesir Şehir İçi Nakliyat
31870
ReplyDeleteDüzce Parça Eşya Taşıma
Çerkezköy Çatı Ustası
Batıkent Fayans Ustası
Pursaklar Boya Ustası
Şırnak Evden Eve Nakliyat
Bitlis Şehir İçi Nakliyat
Çerkezköy Fayans Ustası
Diyarbakır Evden Eve Nakliyat
Adıyaman Parça Eşya Taşıma
88949
ReplyDeleteÇerkezköy Fayans Ustası
Erzurum Şehir İçi Nakliyat
Muş Şehir İçi Nakliyat
Van Şehirler Arası Nakliyat
İstanbul Lojistik
Ağrı Parça Eşya Taşıma
Denizli Parça Eşya Taşıma
Ünye Halı Yıkama
Bitlis Parça Eşya Taşıma
9DD14
ReplyDeletebinance %20 referans kodu
C6E22
ReplyDeletebinance indirim
82F7B
ReplyDeletekırşehir parasız görüntülü sohbet uygulamaları
elazığ rastgele sohbet odaları
edirne mobil sohbet
telefonda kızlarla sohbet
giresun sohbet chat
adana ücretsiz sohbet odaları
osmaniye en iyi görüntülü sohbet uygulaması
gümüşhane mobil sohbet siteleri
antep sohbet chat
3A564
ReplyDeleteagri mobil sesli sohbet
bayburt sesli sohbet sesli chat
muğla sesli sohbet sesli chat
bartın mobil sohbet et
bedava görüntülü sohbet sitesi
van nanytoo sohbet
rize kızlarla canlı sohbet
telefonda kadınlarla sohbet
canlı sohbet odaları
6F015
ReplyDeletekırıkkale bedava sohbet
izmir rastgele görüntülü sohbet
niğde en iyi ücretsiz sohbet uygulamaları
Afyon Sesli Sohbet
hakkari sesli görüntülü sohbet
kırşehir canlı sohbet odası
Sivas Parasız Görüntülü Sohbet
aksaray yabancı canlı sohbet
parasız görüntülü sohbet uygulamaları
E3644
ReplyDeleteerzurum kızlarla rastgele sohbet
Izmir Görüntülü Sohbet Kadınlarla
antalya canlı görüntülü sohbet siteleri
burdur kadınlarla görüntülü sohbet
en iyi ücretsiz sohbet siteleri
maraş tamamen ücretsiz sohbet siteleri
Çanakkale Görüntülü Canlı Sohbet
Kırıkkale Canlı Görüntülü Sohbet Uygulamaları
görüntülü sohbet uygulama
BA26A
ReplyDeleteCaw Coin Hangi Borsada
Facebook Sayfa Beğeni Hilesi
Bitcoin Madenciliği Nasıl Yapılır
Bitcoin Nasıl Alınır
Kwai Takipçi Satın Al
Binance'de Kaldıraç Var mı
Vector Coin Hangi Borsada
Twitch Takipçi Hilesi
Tiktok İzlenme Hilesi
GHNJMNMJHN
ReplyDeleteشركة تسليك مجاري الخبر
شركة مكافحة حشرات بالجبيل a1xod3LhoW
ReplyDeleteشركة عزل اسطح بالرياض PVPwXzAUYp
ReplyDelete932DFC7144
ReplyDeletetakipçi satın al gerçek