Spring etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Spring etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

4 Ekim 2010 Pazartesi

Log4J Kullanarak Mail Gönderme

Yazılım projelerinde loglama için genelde Log4J kullanılır. Biz de bu yazımızda Log4J'nin bir özelliğine bakacağız. Ama Log4J'nin kullanımına dair değil. Log4J'nin mail gönderme özelliğinden. Bu özelliğe dair birçok örnek bulabilirsiniz. Ama Spring ve JavaMail Framwork'ü kullanarak Log4J'nin SMTP Appender sınıfını customize edebilirsiniz. Güle Güle Kullanın (GGK)!

MailingUsingLog4J.pdf

Projenin source kodları

1 Nisan 2010 Perşembe

Drop And Create Database Table

       Hibernate annotationlarını kullanarak pojo'larınızı oluşturduysanızve projenizde Spring frameworkten faydalanıyorsanız aşağıdaki class'ı kullanarak veritabanınızdaki tabloları kolaylıkla kaldırıp yeniden yaratabilirsiniz.

import java.lang.reflect.Field;
import javax.sql.DataSource;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;


public class DropAndCreateTables {
    public static void main(String [] args){
    ClassPathXmlApplicationContext context =  new ClassPathXmlApplicationContext(new String[] {
                "classpath:context/annotationBasedContext.xml", "classpath:context/dao.xml" });     

        LocalSessionFactoryBean localSessionFactoryBean =(LocalSessionFactoryBean) context.getBean("&baseSessionFactory"); 

        org.hibernate.cfg.Configuration configuration = localSessionFactoryBean.getConfiguration();

        try {

            Field declaredField =LocalSessionFactoryBean.class.getDeclaredField("configTimeDataSourceHolder");
            declaredField.setAccessible(true);
            ThreadLocal tl = (ThreadLocal) (declaredField.get(null));
            tl.set((DataSource) context.getBean("dataSource"));
        } catch (Throwable e) {
            e.printStackTrace();
        }
            SchemaExport export = new SchemaExport(configuration);
            export.setOutputFile("exportdb.ddl");
            export.setDelimiter(";");
            export.create(true, true);
    }
}


Class'ın içinde kullanılan annotationBasedContext.xml projenizin spring konfigürasyon dosyası. dao.xml dosyası ise veritabanı bağlantı parametreleri ve ayarlarının tutulduğu xml doyasıdır.


*********************************dao.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"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;

   <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref local="baseSessionFactory" />
   </property>
   </bean>
   <bean id="baseSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref local="dataSource" />
    </property>
    <property name="configLocation" value="${hibernate.config.file}"/>
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="hibernateProperties">
        <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
        <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
   </bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
    <property name="jdbcUrl" value="jdbc:oracle:thin:@${database.url}"></property>
    <property name="properties">
        <props>
        <prop key="c3p0.acquire_increment">1</prop>
        <prop key="c3p0.idle_test_period">100</prop>
        <prop key="c3p0.max_size">30</prop>
        <prop key="c3p0.min_size">10</prop>
        <prop key="c3p0.max_statements">100</prop>
        <prop key="c3p0.timeout">180</prop>
        <prop key="user">${database.user}</prop>
        <prop key="password">${database.password}</prop>
        </props>
    </property>
        <property name="maxPoolSize" value="100" />
        <property name="initialPoolSize" value="2" />
        <property name="minPoolSize" value="1" />
        <property name="maxStatements" value="200" />
        <property name="maxIdleTime" value="300" />
        <property name="acquireIncrement" value="10" />
        <property name="unreturnedConnectionTimeout" value="240" />
        <property name="maxConnectionAge" value="240" />
</bean>
</beans>

     
**************************annotationBasedContext.xml *******************************

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    <context:annotation-config />
    <context:component-scan base-package="com.akgun.akris" />
    <context:property-placeholder location="classpath:/configuration/jdbc.properties" />
    <aop:aspectj-autoproxy />
    <bean id="contextApplicationContextProvider" class="com.akgun.akris.context.ApplicationContextProvider"></bean>
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
</beans>


Bu xml dosyaları sizin projenizin ayarlarına göre farklılık gösterebilir. Örnek olması açısından paylaşmak istedim.
Benim işlerimi çok kolaylaştırmıştır bu class umarım herkese faydalı olur...