Saturday, February 25, 2017

Spring Advice Annotation Examples

package beans.annotation;

import org.springframework.stereotype.Component;

@Component
public class Arithmatic {

public int add(int a, int b){
return a+b;
}

public int sub(int a, int b){
return a-b;
}

public int mul(int a, int b){
return a*b;
}

public int div(int a, int b){
return a/b;
}
}

package beans.annotation;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component("cours")
public class Course {

@Value(value = "100")
private int courseId;

private String courseName;

public int getCourseId() {
return courseId;
}

public void setCourseId(int courseId) {
this.courseId = courseId;
}

public String getCourseName() {
return courseName;
}
   
@Value(value = "JAVA")
@Required
public void setCourseName(String courseName) {
this.courseName = courseName;
}

@Override
public String toString() {
return "Course [courseId=" + courseId + ", courseName=" + courseName + "]";
}
}

package beans.annotation;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {
@Before("execution(public * *(..))")
//@AfterReturning("execution(public * *(..))")
//@AfterThrowing("execution(public * *(..))")
public void sayHello(){
System.out.println("MyAdvice sayHello method is invoked");
}
}


package beans.annotation;

import javax.inject.Qualifier;

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

@Service
public class Student {

private int studentNo;
private String studentName;
   
@Autowired
private Course course;
public Student() {
super();
System.out.println("Default constructor called");
}

@Autowired
public Student(@Value(value = "001") int studentNo, @Value(value = "Bala") String studentName) {
super();
this.studentNo = studentNo;
this.studentName = studentName;
System.out.println("2-arg constructor called");
}
public int getStudentNo() {
return studentNo;
}
//@Value(value = "002")
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
//@Value(value="Balaji")
public void setStudentName(String studentName) {
this.studentName = studentName;
}

public Course getCourse() {
return course;
}

public void setCourse(Course course) {
this.course = course;
}

@Override
public String toString() {
return "Student [studentNo=" + studentNo + ", studentName=" + studentName + ", course=" + course + "]";
}
}



No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...