<file_sep>package hibernateDemo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import hibernate.demo.entity.Course;
import hibernate.demo.entity.Instructor;
import hibernate.demo.entity.InstructorDetail;

public class CreateInstructorDemo {

	public static void main(String[] args) {
		
		//create session factory
		SessionFactory factory=new Configuration()
								.configure("hibernate.cfg.xml")
								.addAnnotatedClass(Instructor.class)
								.addAnnotatedClass(InstructorDetail.class)
								.addAnnotatedClass(Course.class)
								.buildSessionFactory();
		
		//create session
		Session session=factory.getCurrentSession();
		
		try {
					
			// create the objects
			Instructor instructor=new Instructor("Kartik","Sareen","<EMAIL>");
			
			InstructorDetail instructorDetail=new InstructorDetail("www.abc.com","Cricket");
			
			//associate the objects
			instructor.setInstructorDetail(instructorDetail);
			
			//start a transaction
			session.beginTransaction();
			
			//save the instructor object 
			//Note : it will also save instructorDetails object because of cascadeType.all
			session.save(instructor);
			
			// commit transaction
			session.getTransaction().commit();
		}
		finally {
			session.close();
			factory.close();
		}


	}

}
<file_sep>package spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration                // for using java configuration(no XML)
@ComponentScan("spring")      //package to scan for the components and create bean automatically
public class SportConfig {
	

}
<file_sep>foo.email=<EMAIL>
foo.team=XYZDFGHJ<file_sep>package hibernateDemo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import hibernate.demo.entity.Student;

public class UpdateStudentDemo {

	public static void main(String[] args) {
		
		//create session factory
		SessionFactory factory=new Configuration()
								.configure("hibernate.cfg.xml")
								.addAnnotatedClass(Student.class)
								.buildSessionFactory();
		
		//create session
		Session session=factory.getCurrentSession();
		
		try {
			
			
			//start a transaction
			session.beginTransaction();
			
			Student student=session.get(Student.class, 1);
			student.setEmail("<EMAIL>");
			
			// commit transaction
			session.getTransaction().commit();
			
			session=factory.getCurrentSession();
			
			session.beginTransaction();
			
			session.createQuery("update Student set email='<EMAIL>'").executeUpdate();
			
			session.getTransaction().commit();
		}
		finally {
			factory.close();
		}


	}

}
<file_sep>package hibernateDemo;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import hibernate.demo.entity.Student;

public class PrimaryKeyDemo {

	public static void main(String[] args) {
		//create session factory
		SessionFactory factory=new Configuration()
								.configure("hibernate.cfg.xml")
								.addAnnotatedClass(Student.class)
								.buildSessionFactory();
		
		//create session
		Session session=factory.getCurrentSession();
		
		try {
			
			//create 3 student objects	
			String theDateOfBirthStr = "31/12/1998";
            Date theDateOfBirth = DateUtils.parseDate(theDateOfBirthStr);

			Student student1=new Student("A","X","<EMAIL>",theDateOfBirth);
			Student student2=new Student("B","Y","<EMAIL>",theDateOfBirth);
			Student student3=new Student("C","Z","<EMAIL>",theDateOfBirth);
			
			//start a transaction
			session.beginTransaction();
			
			//save the student object 
			session.save(student1);
			session.save(student2);
			session.save(student3);
			
			// commit transaction
			session.getTransaction().commit();
		}
		catch (Exception exc) {
            exc.printStackTrace();
        } 
		finally {
			factory.close();
		}

	}

}
<file_sep>package aopdemo.dao;

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

import org.springframework.stereotype.Component;

import aopdemo.Account;

@Component
public class AccountDAO {
	
	private String name;
	private String serviceCode;
	
	public List<Account> findAccounts(boolean tripWire){
		
		if(tripWire) {
			throw new RuntimeException("Runtime Exception");
		}
		
		List<Account> accs=new ArrayList<>();
		
		//create sample accounts
		Account a1=new Account("John","Silver");
		Account a2=new Account("a","Platinum");
		Account a3=new Account("b","Gold");
		
		//add to list
		accs.add(a1);
		accs.add(a2);
		accs.add(a3);
		
		return accs;
		
	}
	
	public void addAccount(Account acc,boolean vipFlag) {
		System.out.println(getClass()+ " :Doing my db work adding an account");
	}
	
	public boolean doWork() {
		System.out.println(getClass()+ " :doWork() method");
		return true;
	}

	public String getName() {
		System.out.println(getClass()+ " :getName() method");
		return name;
	}

	public void setName(String name) {
		System.out.println(getClass()+ " :setName() method");
		this.name = name;
	}

	public String getServiceCode() {
		System.out.println(getClass()+ " :getServiceCode() method");
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		System.out.println(getClass()+ " :setServiceCode() method");
		this.serviceCode = serviceCode;
	}
	
	
}
<file_sep>package aopdemo.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import aopdemo.Account;

@Aspect
@Component
@Order(1)
public class MyDemoLoggingAspect {
	
	@Before("aopdemo.aspect.AopPointcutExpressions.forDAOPackageNoGetterSetter()")
	public void beforeAddAccountAdvice(JoinPoint joinPoint) {
		System.out.println("\n>>>Executing @Before advice on methods");
	
		//display the method Signature
		MethodSignature methodSig=(MethodSignature) joinPoint.getSignature();
		System.out.println("Method: "+methodSig);
		
		//display the method arguments
		Object[] args=joinPoint.getArgs();
		
		for(Object arg:args) {
			System.out.println(arg);
			
			if(arg instanceof Account) {
				//downcast and print AccountStuff
				Account acc=(Account)arg;
				System.out.println("Account name: "+acc.getName());
			}
		}
	}
}
<file_sep>package springdemo;

import java.util.Random;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class FileFortuneService implements FortuneService {

	private String filename="D:\\eclipse\\spring-demo-annotations\\src\\springdemo\\fortune-data.txt";
	
	private List<String> theFortunes;
	
	private Random myRandom=new Random();
	
	public FileFortuneService() {
		
		System.out.println("FileFortuneService: in constructor");
		theFortunes=new ArrayList<String>();
	}
	
	@PostConstruct
	public void doMyStartupStuff() {
		System.out.println("In FileFortuneService method loadTheFortunesFile");
		File file=new File(filename);
		
		try (BufferedReader br=new BufferedReader(new FileReader(file))){
			
			String line;
			while((line=br.readLine())!=null) {
				theFortunes.add(line);
			}
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public String getFortune() {
		int index=myRandom.nextInt(theFortunes.size());
		
		return theFortunes.get(index);
 	}

}
<file_sep>foo.email=<EMAIL>
foo.team=xyz<file_sep>package springdemo;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

//@Component("myCoach")       setting explicit bean id="myCoach"
@Component                    //setting default bean id="tennisCoach"  same name as the class but first letter lowercase
//@Scope("prototype")
public class TennisCoach implements Coach {

	private FortuneService fortuneService;
	
	@Autowired     //constructor injection
	public TennisCoach(@Qualifier("randomFortuneService")FortuneService fortuneService) { 
		System.out.println("In Tennis Coach Constructor");
		this.fortuneService = fortuneService;
	}
	
	@PostConstruct
	public void doMyStartupStuff() {
		System.out.println("In Tennis Coach doMyStartupStuff function");
	}
	
	@Override
	public String getDailyWorkout() {
		return "Practice your backhand volley";
	}

	@Override
	public String getDailyFortune() {
		return fortuneService.getFortune();
	}
	
	@PreDestroy
	public void doMyCleanupStuff() {
		System.out.println("In Tennis Coach doMyCleanupStuff function");
	}
}

	


<file_sep>package aopdemo.dao;

import org.springframework.stereotype.Component;

@Component
public class MembershipDAO {

	public boolean addAccount() {
		System.out.println(getClass()+ " :adding a membership account");
		return true;
	}
	
	public boolean doWorkMember() {
		System.out.println(getClass()+ " :doWorkMember() method");
		return true;
	}
}
<file_sep>package aopdemo;

import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import aopdemo.dao.AccountDAO;
import aopdemo.dao.MembershipDAO;

public class AfterReturningDemoApp {

	public static void main(String[] args) {
		
		//read the spring config java class
		AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(DemoConfig.class);

		//get the bean from spring container
		AccountDAO accountDAO=context.getBean("accountDAO",AccountDAO.class);
		
		List<Account> accs=accountDAO.findAccounts(false);

		System.out.println("\n\nMain Program:AfterReturningDemoApp");
		System.out.println(accs);		
		
		//close the context
		context.close();
	}

}
