Drop Down

Saturday, September 21, 2019

Spring_App_19_BankApp_AutoDiscovery_Service_2

package com.amit.Dao;

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.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")    //-->to 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 create(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 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;
}
public void showDetails()
{
}

}

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

public interface AccountService 
{
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.Dao.AccountDao;
@Service("accService")
public class AccountServiceImpl implements AccountService 
{
@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.Dao.AccountDaoImpl;
import com.amit.service.AccountService;

public class Test 
{

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accserv = (AccountService)context.getBean("accService");
String status = accserv.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.service"/>
    <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>

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>

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>

Spring_App_16_BankApp_By_Autowing

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 oracle.jdbc.pool.OracleDataSource;

public class AccountDaoImpl implements AccountDao 
{
@Autowired   //-------> this will inject datasource object into AccountDao (to avoid setters and getters for datasource)
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:annotation-config/>  <!-- When we use @annotation in bean we have to use <contest:ann..../> -->
    
    <bean id="datasource" class="oracle.jdbc.pool.OracleDataSource">  <!-- this id "datasource" is not used any where you can change it -->
    <property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="user" value = "scott"></property>  <!--Driver class Name is not required as it will take automatically -->
    <property name="Password" value = "tiger"></property>
    </bean>
    
    <bean id="dao" class ="com.amit.Dao.AccountDaoImpl">  <!-- id "dao" is used by IOC container (getbean uses it) -->
    <!-- we don't need to set value of datasource, as we are using autowiring ie. 
    ....<property name="datasource" ref ="datasource"></property> -->
    </bean>
</beans>
===================================================================

Spring_App_15_Autowiring_By_Annotations

package com.amit.Beans;

public class BGV
{
private String fathersName;
private String mothersName;
private String state;
private String distict;
private String postoffice;
private String street;
private String house_No;
private String pincode;
private String phone;

public String getFathersName() {
return fathersName;
}
public void setFathersName(String fathersName) {
this.fathersName = fathersName;
}
public String getMothersName() {
return mothersName;
}
public void setMothersName(String mothersName) {
this.mothersName = mothersName;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getDistict() {
return distict;
}
public void setDistict(String distict) {
this.distict = distict;
}
public String getPostoffice() {
return postoffice;
}
public void setPostoffice(String postoffice) {
this.postoffice = postoffice;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getHouse_No() {
return house_No;
}
public void setHouse_No(String house_No) {
this.house_No = house_No;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}

}
====================================================================
package com.amit.Beans;

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

public class Employee 
{
private String emp_name;
private String emp_code;
private String joining_date;
 
//@Autowired(required = true) --> write here or above setter method of bgv (To inject bgv into Employee)
private BGV bgv;
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public String getEmp_code() {
return emp_code;
}
public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}
public String getJoining_date() {
return joining_date;
}
public void setJoining_date(String joining_date) {
this.joining_date = joining_date;
}
public BGV getBgv() {
return bgv;
}
@Autowired(required = true)  //--> we can keep it above declaration Line no 11
public void setBgv(BGV bgv) {
this.bgv = bgv;
}

public void getEmpDetails()
{
System.out.println("     Employee Details");
System.out.println("---------------------------------");
System.out.println("Emp Name :"+emp_name);
System.out.println("Emp Code :"+emp_code);
System.out.println("Joining Date     :"+joining_date);
System.out.println();
System.out.println("     Employee BGV Details");
System.out.println("---------------------------------");
System.out.println("Fathers Name :"+bgv.getFathersName());
System.out.println("Mothers Name :"+bgv.getMothersName());
System.out.println("State :"+bgv.getState());
System.out.println("Distict :"+bgv.getDistict());
System.out.println("Post Office :"+bgv.getPostoffice());
System.out.println("Street :"+bgv.getStreet());
System.out.println("House No. :"+bgv.getHouse_No());
System.out.println("Pincode :"+bgv.getPincode());
System.out.println("Phone :"+bgv.getPhone());
}

}

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

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

import com.amit.Beans.Employee;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("emp");
emp.getEmpDetails();
}

}

======================================================================
<?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:annotation-config/>
<bean id ="emp" class="com.amit.Beans.Employee">
<property name="emp_name" value="A. K. Manjhi"></property>
<property name="emp_code" value="1444"></property>
<property name="joining_date" value="4th Aug"></property>
<!-- <property name="bgv" ref="bgv"></property> -->
</bean>

<bean id = "bgv" class="com.amit.Beans.BGV">
<property name="fathersName" value="G. Manjhi"></property>
<property name="mothersName" value="B. Devi"></property>
<property name="state" value="Jharkhand"></property>
<property name="distict" value="Ranchi"></property>
<property name="postoffice" value="Mahuda"></property>
<property name="street" value="Power House"></property>
<property name="house_No" value="121"></property>
<property name="pincode" value="828305"></property>
<property name="phone" value="7532051119"></property>
</bean>

</beans>        

Spring_App_14_Autowiring_By_Constructor

package beans;

public class Account
{
public String acc_number;
public String ifsc;

public String getAcc_number() {
return acc_number;
}
public void setAcc_number(String acc_number) {
this.acc_number = acc_number;
}
public String getIfsc() {
return ifsc;
}
public void setIfsc(String ifsc) {
this.ifsc = ifsc;
}



}
=====================================================================
package beans;

public class Address 
{
public String city;
public String state;
public String pincode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}

Spring_App_13_Autowiring_By_Type

package beans;

public class Account
{
public String acc_number;
public String ifsc;

public String getAcc_number() {
return acc_number;
}
public void setAcc_number(String acc_number) {
this.acc_number = acc_number;
}
public String getIfsc() {
return ifsc;
}
public void setIfsc(String ifsc) {
this.ifsc = ifsc;
}



}

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

package beans;

public class Address 
{
public String city;
public String state;
public String pincode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}

=====================================================================
package beans;

public class Employee 
{
private String name;
private String emp_code;
private String salary;
private Address addr;   
private Account acc;
public String getName() {
return name;
}



public void setName(String name) {
this.name = name;
}



public String getEmp_code() {
return emp_code;
}



public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}



public String getSalary() {
return salary;
}



public void setSalary(String salary) {
this.salary = salary;
}



public Address getAddr() {
return addr;
}



public void setAddr(Address addr) {
this.addr = addr;
}



public Account getAcc() {
return acc;
}



public void setAcc(Account acc) {
this.acc = acc;
}



public void display_details()
{
System.out.println("--------BASIC DETAILS------------");
System.out.println("Name : "+name);
System.out.println("emp_code : "+emp_code);
System.out.println("salary : "+salary);
System.out.println("--------ACCOUNT DETAILS------------");
System.out.println("acc_number  : "+acc.getAcc_number());
System.out.println("ifsc        : "+acc.getIfsc());
System.out.println("----------ADDRESS DETAILS----------");
System.out.println("city        : "+addr.getCity());
System.out.println("state       : "+addr.getState());
System.out.println("pincode     : "+addr.getPincode());
}

}

=====================================================================
package Test;

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

import beans.Employee;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("emp");
emp.display_details();

}

}
<?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 id="account" class="beans.Account">      <!-- In byType autowiring ioc look for type LIMITATION: only supports for unique beans in config.  -->
<property name="acc_number" value="8789654410911"></property>
<property name="ifsc" value="ICIC00004839"></property>
</bean>

<bean id= "address" class = "beans.Address">
<property name="city" value ="Noid"></property>
<property name="state" value ="UP"></property>
<property name="pincode" value ="120130"></property>
</bean>

<bean id="emp" class ="beans.Employee" autowire="byType">
<property name="name" value ="Amit"></property>
<property name="emp_code" value ="NSTL-9372"></property>
<property name="salary" value ="3,00,000 p.m"></property>

<!-- 
<property name="addr" ref="address"></property>
<property name="acc" ref ="account"></property>  
-->

</bean>

</beans>

Java 8 Notes Pics