JSF Projesine Spring Framework ve Hibernate(Oracle JDBC) kurulumu(Resimli Anlatım)

JSF Projesine Spring Framework ve Hibernate(Oracle JDBC) kurulumu(Resimli Anlatım)

Merhaba, JSF Spring Framework ve Hibernate projesi yapacağım.

Gerekenler(Daha önce yaptığımız adımlar)

1 )  Tomcat Kurulumu ( Yazıyı Oku ) ben 9 versiyonunu kurdum

2 )  JSF projesi oluşturma ( Yazıyı Oku )

3 )  JSF projesi üzerine Primefaces araçları eklenmesi ( Yazıyı Oku )

JSF HelloPrimefaces projemizin üstüne Spring Framework ve Hibernate ekleyeceğim bunun için 3. yazıdaki projeyi indirebilir yada ilk 3 adımı yaparak yeni bir proje oluşturabilirsiniz(Tavsiye Ederim).

1 )  POM XML DÜZENLENMESİ

Öncelikle Pom xml dosyamıza Spring Framework , Hibernate ve Oracle JDBC kütüphanelerimizi ekleyelim.

 

Maven dependency versiyon güncelleme kolay olması için(Spring ve Hibernate için) properties içinde(Değişken olarak atama diyebiliriz) tanımladım.UTF8 standartını da kullandım.


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<org.springframework.version>4.3.10.RELEASE</org.springframework.version>
		<hibernate.version>5.2.10.Final</hibernate.version>
	</properties>

Spring Framework Dependency


<!-- ***************** SPRİNG LİBRARY ************** -->

		<!-- Core utilities used by other modules. Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Expression Language (depends on spring-core) Define this if you use Spring Expression APIs (org.springframework.expression.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define this if you use Spring Bean APIs (org.springframework.beans.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans) This is the central artifact for Spring's Dependency Injection Container and is generally always defined -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration Define this if you need any of these integrations -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context) Define this if you use Spring Transactions or DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you need ORM (org.springframework.orm.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Web application development utilities applicable to both Servlet and Portlet Environments (depends on spring-core, spring-beans, spring-context) Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Support for testing Spring applications with tools such as JUnit and TestNG This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework.version}</version>
			<scope>test</scope>
		</dependency>

		<!-- ******************* SPRING LİBRARY END **************** -->

Hibernate Dependency

<!--*************************** Hibernate ***************** -->


		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${hibernate.version}</version>
		</dependency>


		<!-- ************************* Hibernate END ****************** -->

Oracle Dependency (Oracle jar lisans hakkından dolayı maven kütüphanesinde bulunmuyor.Bunun içim öncelikle Oracle JDBC jar dosyamızı indirip projemizde lib adında bir klasör oluşturup içine atacağım.Pom xmlde jar dosyasının adresi olarak burayı vereceğim sonra Plugin ile lib altındaki bu jar dosyasını Maven Lib altına taşıma işlemi yapacağım.  )

Oracle Dependency

	<!-- *********** ORACLE JDBC *********** -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc</artifactId>
			<version>12.1.2.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/src/lib/ojdbc-12.1.2-0-0.jar</systemPath>
<optional>true</optional>
		</dependency>
		<!-- *********** ORACLE JDBC END *********** -->

Oracle Jar dosyasını Maven Lib altına taşıma plugin

<!-- Jar dosyalarını maven libs altına taşı -->

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<webResources>

						<resource>
							<directory>${basedir}/src/lib</directory>
							<targetPath>WEB-INF/lib</targetPath>
							<includes>
								<include>**/*.jar</include>
							</includes>
						</resource>
					</webResources>
				</configuration>
			</plugin>

POM XML dosyamızın son hali

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.atakancoban</groupId>
	<artifactId>hellojsf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Hello JSF</name>



	<!-- PrimeFaces Repository -->
	<repositories>
		<repository>
			<id>prime-repo</id>
			<name>Prime Repo</name>
			<url>http://repository.primefaces.org</url>
		</repository>
	</repositories>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<org.springframework.version>4.3.10.RELEASE</org.springframework.version>
		<hibernate.version>5.2.10.Final</hibernate.version>
	</properties>



	<!-- JSF Dependency -->
	<dependencies>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-api</artifactId>
			<version>2.2.14</version>
		</dependency>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-impl</artifactId>
			<version>2.2.14</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>


		<!-- Primefaces -->
		<dependency>
			<groupId>org.primefaces</groupId>
			<artifactId>primefaces</artifactId>
			<version>6.0</version>
		</dependency>

		<!-- PrimeFaces Theme -->
		<dependency>
			<groupId>org.primefaces.themes</groupId>
			<artifactId>all-themes</artifactId>
			<version>1.0.10</version>
		</dependency>

		<!-- ***************** SPRİNG LİBRARY ************** -->

		<!-- Core utilities used by other modules. Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Expression Language (depends on spring-core) Define this if you use Spring Expression APIs (org.springframework.expression.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define this if you use Spring Bean APIs (org.springframework.beans.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans) This is the central artifact for Spring's Dependency Injection Container and is generally always defined -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration Define this if you need any of these integrations -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context) Define this if you use Spring Transactions or DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you need ORM (org.springframework.orm.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Web application development utilities applicable to both Servlet and Portlet Environments (depends on spring-core, spring-beans, spring-context) Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Support for testing Spring applications with tools such as JUnit and TestNG This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework.version}</version>
			<scope>test</scope>
		</dependency>

		<!-- ******************* SPRING LİBRARY END **************** -->




		<!--*************************** Hibernate ***************** -->


		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${hibernate.version}</version>
		</dependency>


		<!-- ************************* Hibernate END ****************** -->

		<!-- *********** ORACLE JDBC *********** -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc</artifactId>
			<version>12.1.2.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/src/lib/ojdbc-12.1.2-0-0.jar</systemPath>
<optional>true</optional>
		</dependency>
		<!-- *********** ORACLE JDBC END *********** -->

	</dependencies>

	<!-- Build Maven WAR -->
	<build>

		<plugins>



			<!-- Jar dosyalarını maven libs altına taşı -->

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<webResources>

						<resource>
							<directory>${basedir}/src/lib</directory>
							<targetPath>WEB-INF/lib</targetPath>
							<includes>
								<include>**/*.jar</include>
							</includes>
						</resource>
					</webResources>
				</configuration>
			</plugin>





<!-- Maven War plugin -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.2</version>
				<configuration>
					<!-- Java version -->
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>

	</build>




</project>

2 )  WEB XML DÜZENLENMESİ

Spring Listener Eklenmeli

<!-- Spring Listener -->
<listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>
<listener>
	<listener-class>
		org.springframework.web.context.request.RequestContextListener
	</listener-class>
</listener>

Faces Config xml adresi tanıtılmalı

<!-- Faces Config -->
 <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>
        WEB-INF/faces-config.xml
    </param-value>
  </context-param>

Application Context xml adresi tanıtılmalı

<!-- Application Context -->  
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

State Saving Method bilgisi eklenmeli

<!-- Server Saving Mode -->
<context-param>
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
		<param-value>server</param-value>
	</context-param>

Faces Config dosyası eklenilmeli (web xmlde belirttiğimiz isimde)

<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2">

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
    
    
</application>

</faces-config>

Application Context dosyası eklenilmeli (web xmlde belirttiğimiz isimde)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 	
 	
 	
 	<import resource="beans-atakan-coban.xml" />
 	<import resource="hibernate_configration.xml" />
 	
    <context:annotation-config />
    <context:component-scan base-package="com.atakancoban" />
    
 
</beans>

Application Context içinde beans-atakan-coban.xml ve hibernate_configration.xml import edildi.Sınıfları hangi paket altında arayacağını da belirttim. com.atakancoban şeklinde. Burada http://www.springframework.org/schema/beans/spring-beans-4.3.xsd şeklindeki xsd kısımları POM xmlde spring versiyonum 4.3 olduğu için 4.3 değişik versiyon ise burada da güncellemek gerekir.

Beans xml(beans-atakan-coban.xml) Bu dosyada Spring Dao ve Dao implement classlarımızı nasıl ilişkilendireceğini belirtiyoruz ve Session Factory property özelliğini ekleyerek uygulama açıldığında Session(Connection) alıp veri tabanı işlerimizi yapabiliyoruz.Ben örnek olarak CarDao ve CarDaoImpl sınıflarımı ilişkilendirdim ve CarDao ile veritabanı bağlantısı yaptım. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd burada da xsd 4.3 olarak görülmekte.

<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-4.3.xsd">

	<bean id="carDao" name="carDao" class="com.atakancoban.dao.CarDaoImpl" >
			<property name="sessionFactory" ref="sessionFactory" />
	</bean>

</beans>

Hibernate Configuration (xsd kısımları burada da 4.3 şeklinde)
Burada Hibernate Transaction manager ve Session Factory tanımlaması yaptım.
Buradaki sessionFactory beans xml kısmında dao ve daoImpl bağladığımız propertydeki sessionFactory.
Buradaki connection’ı dao sınıfımıza yolluyoruz aslında.
testcon.xml dosyasını burada gösterdim.(Connection bilgileri bu xmlde)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>file:testcon.xml</value>
		</property>
		
	</bean>
	
	
</beans>

TestCon.xml

Veritabanı ile bağlantı kurmak için gerekli bilgilerin girildiği xml.

connection ip ve username password alanlarını kendi veritabanı bilgilerinizi girmelisiniz

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="sessionFactorySRC">
		<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@ipadresiniz:1521:HELLO</property>
		<property name="connection.username">USERNAME GİRİNİZ</property>
		<property name="connection.password">PASSWORD GİRİNİZ</property>
		<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.acquire_increment">1</property>
		<property name="hibernate.c3p0.idle_test_period">0</property>
		<property name="hibernate.c3p0.min_size">1</property>
		<property name="hibernate.c3p0.max_size">15</property>
		<property name="hibernate.c3p0.timeout">0</property>
		<property name="show_sql">false</property>
		<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
	</session-factory>
</hibernate-configuration>

Burada dikkat etmemiz gereken Tomcat Server testcon.xml dosyasını Launch Configuration ayarındaki yerde arar. Bunu ayarlayalım.Bu xml dosyasını tomcatinizin baska çalışma ortamı var ise oraya da atabilirsiniz.Ben projemin içinde xml in olduğu yolu lokasyon olarak verdim.

Şu şekilde

Tomcat Server’ın üzerine çift tıklayalım

Açılan ayarlar sayfasında Open Launch Configuration menüsüne tıklayalım

Edit Configuration penceresinde Arguments menüsüne gelelim ve aşağıdaki Working Directory kısmında workspace butonuna basarak WEB INF klasorunu belirtelim.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3 JAVA SINIFLARINDA SERVICE VE DAO KULLANIMI

DAO ve DAO Implement

CarDao şeklinde bir interface oluşturdum.

package com.atakancoban.dao;

import java.util.List;

public interface CarDao {
	public List<String> getCarDetails();
	public String getSystemTime() throws Exception;
}

CarDaoImpl şeklinde CarDao sınıfını implemen eden sınıf oluşturdum

Burada Spring Frameworke bu sınıfın dao sınıfı olduğunu tanımladım @Repository(“carDao”) Buradaki carDao ismi beans-atakan-coban.xml configinde ilişkilendirme kısmında kullandığım id alanı oluyor.

package com.atakancoban.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.atakancoban.dao.CarDao;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

@Repository("carDao")
public class CarDaoImpl implements CarDao, Serializable {

	private SessionFactory sessionFactory;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1677161902821079349L;

	@Override
	public List<String> getCarDetails() {

		List<String> cars = new ArrayList<String>();

		cars.add(0, "LAMBORGHINI");
		cars.add(1, "BENTLEY");
		cars.add(2, "MASERATI");
		cars.add(3, "JAGUAR");
		cars.add(4, "MITSUBISHI");
		cars.add(5, "MERCEDES-BENZ");
		return cars;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public String getSystemTime() throws Exception {
		String result = null;
		try {

			Session session = getSessionFactory().getCurrentSession();
			String sql = "select to_char(sysdate,'dd.mm.yyyy hh24:mi:ss') zmn from dual";
			SQLQuery query = (SQLQuery) session.createSQLQuery(sql);
			if (query.uniqueResult() != null)
				result = query.uniqueResult().toString();
			else
				result = "deger boş";
		} catch (Exception e) {
			throw new Exception("getSystemTime() error. " + e.getMessage(), e);
		}

		return result;
	}

}

CarService ve CarServiceImpl

CarService isminde bir interface oluşturdum

package com.atakancoban.service;

import java.util.List;

public interface CarService {
	public List<String> getCarDetails();
	public String getSystemTime() throws Exception;
}

CarServiceImpl isminde CarService implement eden sınıf oluşturdum

package com.atakancoban.service;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlTransient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atakancoban.dao.CarDao;

@Service("carService")
@Transactional(readOnly=true)
public class CarServiceImpl implements CarService {
	
	@Autowired
	private CarDao cardao;

	
	@Override
	public List<String> getCarDetails() {
	if(cardao!=null)
		return cardao.getCarDetails();
	else{
		System.out.println("carDaou service icinde null");
		return null;}
	}

/*	@Transactional(rollbackFor = Exception.class, readOnly = false)
	public addname() {
	}*/
	
	public CarDao getCardao() {
		return cardao;
	}

	public void setCardao(CarDao cardao) {
		this.cardao = cardao;
	}

	@Override
	public String getSystemTime() throws Exception {
		return cardao.getSystemTime();
	}

	
	
}

CarServiceImpl kısmında @Service(“carService”) Buradaki carService ismi HelloBean sınıfından ManagedProperty olarak çağırılırken kullanılacak isimdir.

@Transactional(readOnly=true) Spring Transaction yönetimini ekliyoruz ben burada herhangi bir commit gerektirecek veritabanı işlemi yapmadığım için readOnly olarak yaptım. Eğer addUser() şeklinde bir metodumuz olsaydı hata anında ROLLBACK işlemi gerekseydi metodun başına @Transactional(rollbackFor = Exception.class, readOnly = false) şeklinde bir annotation eklemem gerekirdi.

@Autowired
private CarDao cardao; (Burada Spring tarafından CarDao implement sınıfımızı bağla komutu veriyoruz.beans xmlde tanımladığımız şekilde ilişkilendiriyor)

HelloBean

Bean sınıfımda oluşturduğum CarService sınıfımı bağladım.

@ManagedProperty(“#{carService}”)      (Burada CarServiceImpl sınıfındaki @Service(“carService”) ismi ile bağlantı kurdu)
private CarService carService;

package com.atakancoban.bean;

import java.io.Serializable;
import java.util.jar.Attributes.Name;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.atakancoban.service.CarService;

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

	private static final long serialVersionUID = -4823295172962937652L;

	@ManagedProperty("#{carService}")
	private CarService carService;

	private String message = "www.atakancoban.com";

	@PostConstruct
	public void init() {
		System.out.println("init()");
	}

	public void onClick() {
		try {

			System.out.println("Araba Listesi  : " + carService.getCarDetails());

			System.out.println("Veritabanı ile çekilen zaman   : " + carService.getSystemTime());

		} catch (Exception e) {
			System.out.println("Exception onClick() " + e.getMessage());
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}



}

onClick() metodu ile bir adet String dizi döndüren metod ve bir adet veritabanına bağlanıp tarih bilgisini döndüren metod çağırdım.

Son olarak RUN !

Projeyi github hesabımdan indirebilirsiniz.

Spring Framework , Hibernate  ve Java server faces ile uzmanlık gerektiren soru ve öneriler için : Şükrü ÇAKMAK

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir