Spring. @Autowire , Dependency Injection

 package com.example.demo;


import org.springframework.stereotype.Component;


@Component("lap")

public class Laptop {


private int lid;

private String brand;

public int getLid() {

return lid;

}

public void setLid(int lid) {

this.lid = lid;

}

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

@Override

public String toString() {

return "Laptop [lid=" + lid + ", brand=" + brand + "]";

}

public void compile() {

System.out.println("compilig");

}

}

package com.example.demo;

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
//@Scope("singleton")
public class Alien {

private int aid;
private String aname;
private String tech;
@Autowired
@Qualifier("lap")
private Laptop laptop;
public Alien() {
super();
System.out.println("Object created");
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getTech() {
return tech;
}
public void setTech(String tech) {
this.tech = tech;
}
public Laptop getLaptop() {
return laptop;
}

public void setLaptop(Laptop laptop) {
this.laptop = laptop;
}

public void show() {
System.out.println("in show");
laptop.compile();
}
}
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
Alien a = context.getBean(Alien.class);
a.show();
}

}

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class