Drop Down

Saturday, September 21, 2019

Spring_App_18_BankApp_AutoDiscovery_ServiceLayer_Impl

package com.amit.doa;

public interface AccountDao
{
public String create(String accNo, String accName, String accType, int balance);
public String search(String accNo);
public String update(String accNo, String accName, String accType, int balance);
public String delete(String accNo);


}

======================================================================
package com.amit.doa;

import java.sql.Connection;
import java.sql.PreparedStatement;

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

import oracle.jdbc.pool.OracleDataSource;

@Repository("accdao")     //reference for ioc , to avoid <bean id="acc_dao" clas....>
public class AccountDaoImpl implements AccountDao 
{
String status= " ";
@Autowired(required=true)   // injected without setter & getters
private OracleDataSource datasource;

@Override
public String create(String accNo, String accName, String accType, int balance) 
{
try 
{
Connection con = datasource.getConnection();
PreparedStatement pst = con.prepareStatement("insert into accout values (?,?,?,?)");
pst.setString(1, accNo);
pst.setString(2, accName);
pst.setString(3, accType);
pst.setInt(4, balance);
pst.executeUpdate();
System.out.println("r_val >> ");
/*if(r_val!=0)
{
*/
status = "success";
/*
* } else { status = "fail"; }
*/
}
catch(Exception e)
{
e.printStackTrace();
status = "not";
}
return status;
}

@Override
public String search(String accNo) 
{
 
return null;
}

@Override
public String update(String accNo, String accName, String accType, int balance) 
{
return null;
}

@Override
public String delete(String accNo) 
{
return null;
}

}

======================================================================

package com.amit.service;

public interface AccountServiceDao 
{
public String createAccount(String accNo, String accName, String accType, int balance);
public String searchAccount(String accNo);
public String updateAccount(String accNo, String accName, String accType, int balance);
public String deleteAccount(String accNo);
}

======================================================================
package com.amit.service;

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

import com.amit.doa.AccountDao;

@Service("accservice")
public class AccountServiceDaoImpl implements AccountServiceDao 
{
@Autowired(required=true)
private AccountDao dao;
@Override
public String createAccount(String accNo, String accName, String accType, int balance) 
{
return dao.create(accNo, accName, accType, balance);
}

@Override
public String searchAccount(String accNo) 
{


return null;
}

@Override
public String updateAccount(String accNo, String accName, String accType, int balance) 
{


return null;
}

@Override
public String deleteAccount(String accNo) 
{


return null;
}

}

=====================================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.service.AccountServiceDao;
import com.amit.service.AccountServiceDaoImpl;

public class Test 
{

public static void main(String[] args) 
{
  ApplicationContext context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
  AccountServiceDao service = (AccountServiceDao) context.getBean("accservice");
  String result = service.createAccount("8761001", "Manjhi", "Current", 800000);
      System.out.println(result);
}

}

=====================================================================
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.amit.service"/> <!-- Aee, IOC go to this package and scan it for (component,repository,service,controller) & no need to check for bean config-->
    <context:component-scan base-package="com.amit.dao"/>
    
    <bean id="datasource" class="oracle.jdbc.pool.OracleDataSource">  <!--Driver class Name is not required as it will take automatically -->
    <property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="user" value = "scott"></property>
    <property name="Password" value = "tiger"></property>
    </bean>
    
    <!-- <bean id="dao" class ="com.amit.Dao.AccountDaoImpl"> </bean>  -->
    <!-- Not required when we are using Auto-Discovery/streotype ..As ID (dao) is mentioned in AccontDAOImpl @Repository("dao") -->
</beans>

No comments:

Post a Comment

Java 8 Notes Pics