Java Code Geeks

Wednesday, February 24, 2016

how to use org.springframework.beans.factory.FactoryBean

1 . In general we do not need to implement one custom factory bean , as the spring IOC container has the most factory bean classes implemented for the most of the cases . FactoryBean in spring helps to create custom factory bean. Custom factory bean can initialize bean in the same way as we configure in spring XML. The advantage of defining our own custom factory bean is custom initialization. All we need to do that we will write as class implementing FactoryBean and will override all the methods of FactoryBean and this custom factory bean should be registered in spring XML. And then we can fetch the bean as usual initialized by our custom factory bean. Find the complete example.
 

package com.ramesh.common;
import org.springframework.beans.factory.FactoryBean;
public class CustomerFactoryBean implements FactoryBean {
private Customer customer = new Customerundefined);
 @Override

 public Object getObjectundefined) throws Exception {

  // TODO Auto-generated method stub

  return customer;

 }

 @Override

 public Class<Customer> getObjectTypeundefined) {

  // TODO Auto-generated method stub

  return Customer.class;

 }

 @Override

 public boolean isSingletonundefined) {

  // TODO Auto-generated method stub

  return true;

 }

}
and now spring application context file

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
 
    <bean class="com.ramesh.common.CustomFactoryBean">
     
</bean></beans>

 now in the main class
 
public static void main(String[] args) {
  System.out.println("...Loading beans...");
  AbstractApplicationContext  context = new ClassPathXmlApplicationContext("context.xml");
  
  System.out.println("...fetch Customerto get Object....");
  Customer myBean=context.getBean(Customer.class);
  myBean.doTask();
 }

Friday, September 26, 2014

Why the password filed data need to be stored in the char array

It is a recommended practice to “empty” the read password string once its use is over. This is a secure programming practice to avoid malicious reads of program data to discover password strings.With a char array, as soon as the password is validated, it is possible to empty it and remove the trace of the password text from memory; with a String object, which is garbage collected, it is not as easy as
with a char array.

Monday, September 22, 2014

Javascript Custom Utility Method to check a variable is undefined

var obj=[];
if(Object.prototype.toString.call(obj)=="[object Array]")
{
console.log("as");
}

console.log(obj.toString());

Sunday, September 21, 2014

How to access “Applications” menu in Ubuntu Unity Desktop

open terminal
    or

type  ctrl+ alt+T

and type the following commands

sudo apt-add-repository ppa:diesch/testing
sudo apt-get update
sudo apt-get install classicmenu-indicator

Saturday, September 20, 2014

Think before running these linux commands

wget http://example.com/something -O – | sh


this has two commands in series to execute 

1 . download something from the http://example.com/something  using wget 
2. just pass this downloaded to sh command where sh is just execute the shell script if the downloaded content is shell script 

do be aware when you are running such type of command above .

Thursday, August 21, 2014

send email with body as html

'''
Created on 21-Aug-2014

@author: bambo
'''
from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

import smtplib

'''
    prepare Header Of Message - Start
'''
fromAddr = "bambobabu@gmail.com"
toAddr = "bambobabu@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromAddr
msg['To'] = toAddr
msg['Subject'] ="Python EMail"

body = "<h1>Python Body</h1>"

msg.attach(MIMEText(body,'html'))


'''
    prepare Header Of Message - End
'''

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("bambobabu@gmail.com", "Mca_503911")
text = msg.as_string()
print text
server.sendmail(fromAddr, toAddr, text)

Sending Email Using Python

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Ramesh
#
# Created:     21-08-2014
# Copyright:   (c) Ramesh Babu Y2014
# Licence:     Sky
#-------------------------------------------------------------------------------

import smtplib
fromaddr = 'From Email Id'
toaddrs  = 'To EMail Id'
msg = 'Enter you message here'

server = smtplib.SMTP("smtp.gmail.com:587")

server.starttls()

server.login("<<Your GMail Id>>","GMail Id Password")

server.sendmail(fromaddr, toaddrs, msg)