Drop Down

Saturday, September 21, 2019

Spring_App_17_BankApp_AutoDiscovery

package com.amit.Dao;

public interface AccountDao
{
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.Dao;

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

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

import oracle.jdbc.pool.OracleDataSource;

@Repository("dao")    //-->making it autodiscover     //you can also use @component("dao") 
public class AccountDaoImpl implements AccountDao 
{
@Autowired   //-------> this will inject datasource object into AccountDao (setter/getters not required)
private OracleDataSource datasource;
String status = "";
@Override
public String createAccount(String accNo, String accName, String accType, int balance) 
{
  try 
  {
  Connection con = datasource.getConnection();
  PreparedStatement pst = con.prepareStatement("insert into account values (?,?,?,?)");
 
  pst.setString(1, accNo);
  pst.setString(2, accName);
  pst.setString(3, accType);
  pst.setInt(4, balance);
  pst.executeUpdate();
  status= "success";
  }
  catch(Exception e)
  {
  status = "failure";
  e.printStackTrace();
  }
return status;
}

@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;
}
public void showDetails()
{
}

}

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

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

import com.amit.Dao.AccountDaoImpl;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountDaoImpl dao = (AccountDaoImpl)context.getBean("dao");
String status = dao.createAccount("abc123", "Amit", "Saving", 20000);
if(status.equalsIgnoreCase("success"))
{
System.out.println("Account created successfully");
}
else 
{
System.out.println("Oops! something went wrong");
}
 
}

}

==============================================================
<?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.Dao"/> <!-- Aee, IOC go to this package and scan it for (component,repository,service,controller) & no need to check for bean config-->
    
    <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