BACK END/Spring

3일(08.19) Autowired

라미보 2022. 8. 19. 14:52

 

 

@Autowired 으로 객체 생성하고 setter주입하기

 

 

 

 

📂 myPkg1

 

 

 

💾 Shape.java(Interface로 생성)

package myPkg1;


public interface Shape {	//shape은 자식이 둘 point x, point y
	String make();
	String delete();
}

 

 

💾 PointX.java

package myPkg1;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

public class PointX implements Shape{
	//shape은 자식이 둘 point x, point y
	public PointX() {
		System.out.println("PointX()");
	}

	@Override
	public String make() {
		// TODO Auto-generated method stub
		return "X를 만들다";
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return "X를 지우다";
	}

}

 

 

 

💾 PointY.java

package myPkg1;

import org.springframework.stereotype.Component;
 
public class PointY implements Shape{
	//shape은 자식이 둘 point x, point y
	public PointY() {
		System.out.println("PointY()");
	}
	
	@Override
	public String make() {
		// TODO Auto-generated method stub
		return "Y를 만들다";
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return "Y를 지우다";
	}

}

 

 

 

💾 Circle.java(Interface로 생성)

package myPkg1;

public interface Circle {
	int getX();
	int getY();
	String make();
}

 

 

💾 CircleImpl.java

package myPkg1;

public class CircleImpl implements Circle{

	Shape pointX; //new PointX, Y()
	int x;
	int y;
	int radius;
	
	public CircleImpl() {
		System.out.println("CircleImpl의 생성자");
	}
	
	
	
	
	
	public Shape getPointX() {
		return pointX;
	}

	public void setPointX(Shape pointX) { // x,y
		this.pointX = pointX; //넘어온값이 point x로 간다.
	} 





	@Override
	public int getX() {
	
		return x;
	}

	@Override
	public int getY() {
	
		return y;
	}

	@Override
	public String make() {
		
		return pointX.make(); //PointX에 담긴 객체의 make() 호출한 곳으로 리턴
	}

}

 

 

 

💾 Rectangle.java(interface로 생성)

package myPkg1;

public interface Rectangle{
	int getX();
	int getY();
	String make();
}

 

 

 

 

 

💾 RectangleImpl.java

package myPkg1;

public class RectangleImpl implements Rectangle{

	Shape pointY;
	int x;
	int y;
	int radius;
	
	
	public Shape getPointY() {
		return pointY;
	}

	public void setPointY(Shape pointY) { //pointY = Point X, point Y를 받아서  Shape에 넣는다.
		this.pointY = pointY;
	}

	
	
	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return x;
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return y;
	}

	@Override
	public String make() {
		// TODO Auto-generated method stub
		return pointY.make(); //pointY 변수에 들어가는 클래스의 make메서드를 호출 한것을 리턴
	}

}

 

 

 

💾 Main.java

package myPkg1;

public class Main {

	public static void main(String[] args) {
	
		Shape px = new PointX();
		Shape py = new PointY();
	
		
		CircleImpl circle = new CircleImpl();
		circle.setPointX(px);. 
		System.out.println(circle.make()); 

		
		
		RectangleImpl rec = new RectangleImpl();
		rec.setPointY(py);
		System.out.println(rec.make());
	}

}

Shape px = new PointX();  : 객체를 만드니까 생성자로 가서 출력이 된다.
Shape py = new PointY();  : shape 의 자식이기만 하면 들어올수있다. point x, point y 다 들어올수있다.

 

CircleImpl circle = new CircleImpl();  : 객체를 만들었기때문에 생성자로 간다.
circle.setPointX(px);  : py객체를 setter 주입하므로 shape PointX에 들어가게 된다. 
System.out.println(circle.make());  : CircleImpl클래스의 make()를 호출한다.

 

 

 

 

 

📂 myPkg2

 

 

💾 Shape.java(interface로 생성)

package myPkg2;


public interface Shape {	//shape은 자식이 둘 point x, point y
	String make();
	String delete();
}

 

 

💾 PointX.java

package myPkg2;

import org.springframework.stereotype.Component;

@Component("PointX")//객체 생성

PointX px = new PointX();*/

public class PointX implements Shape{
	//shape은 자식이 둘 point x, point y
	public PointX() {
		System.out.println("PointX()");
	}

	@Override
	public String make() {
		// TODO Auto-generated method stub
		return "X를 만들다";
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return "X를 지우다";
	}

}

@Component() :객체 만들때 사용한다. 객체 만들때 클래스 이름과 동일하게해도 된다

 

 

 

💾 PointY.java

package myPkg2;

import org.springframework.stereotype.Component;

@Component("PointY")
public class PointY implements Shape{
	//shape은 자식이 둘 point x, point y
	public PointY() {
		System.out.println("PointY()");
	}
	
	@Override
	public String make() {
		// TODO Auto-generated method stub
		return "Y를 만들다";
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return "Y를 지우다";
	}

}

 

 

 

 

 

 

💾 Circle.java(Interface로 생성)

package myPkg2;

public interface Circle {
	int getX();
	int getY();
	String make();
}

 

 

 

 

💾 CircleImpl.java

package myPkg2;

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

@Component("myCircleImpl")
public class CircleImpl implements Circle{

	
@Autowired	//객체 정보를 넣는다.  작성한 바로 아래 변수에만 적용된다.
@Qualifier("PointX")
//직접 setter주입 shape의 자식 중 어떻것을 넣을 정해줘야 한다. 자식이 여러개인 경우 어떤 자식이 적합한지 작성해준다. 자식 하나일때는 안씀
	Shape pointX; //new PointX, Y()

	int x;
	int y;
	int radius;
	
	public CircleImpl() {
		System.out.println("CircleImpl의 생성자");
	}
	
	
	
	
	
	public Shape getPointX() {
		return pointX;
	}

	public void setPointX(Shape pointX) { // x,y
		this.pointX = pointX; //넘어온값이 point x로 간다.
	} 





	@Override
	public int getX() {
	
		return x;
	}

	@Override
	public int getY() {
	
		return y;
	}

	@Override
	public String make() {
		
		return pointX.make(); //PointX에 담긴 객체의 make() 호출한 곳으로 리턴
	}

}

@Autowired : 객체 정보를 넣는다.  작성한 바로 아래 변수에만 적용된다.
@Qualifier("PointX")

: 직접 setter주입 shape의 자식 중 어떻것을 넣을 정해줘야 한다. 자식이 여러개인 경우 어떤 자식이 적합한지 작성해준다. 자식 하나일때는 안씀

 

 

 

 

 

 

💾 Rectangle.java(interface로 생성)

package myPkg2;

public interface Rectangle{
	int getX();
	int getY();
	String make();
}

 

 

💾 RectangleImpl.java

package myPkg2;

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

@Component("myRectangleImpl")
public class RectangleImpl implements Rectangle{

	@Autowired //객체를 주입하고 싶을때 사용한다.
	@Qualifier("PointY") //PointY객체를 밀어 넣어라, shape의 자식이 여럿이므로 무엇이 적합한지 알려줘야한다.
	Shape pointY;
	int x;
	int y;
	int radius;
	
	
	public Shape getPointY() {
		return pointY;
	}

	public void setPointY(Shape pointY) { //pointY = Point X, point Y를 받아서  Shape에 넣는다.
		this.pointY = pointY;
	}

	
	
	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return x;
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return y;
	}

	@Override
	public String make() {
		// TODO Auto-generated method stub
		return pointY.make(); //pointY 변수에 들어가는 클래스의 make메서드를 호출 한것을 리턴
	}

}

@Autowired  - 객체를 주입하고 싶을때 사용한다.
@Qualifier("PointY") - PointY객체를 밀어 넣어라, shape의 자식이 여럿이므로 무엇이 적합한지 알려줘야한다.

 

 

 

 

💾Main.java

package myPkg2;

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

public class Main {

	public static void main(String[] args) {
	
		/*
		Shape px = new PointX(); //객체를 만드니까 생성자로 가서 출력이 된다.
		Shape py = new PointY();
		//shape 의 자식이기만 하면 들어올수있다. point x, point y 다 들어올수있다.
		
		CircleImpl circle = new CircleImpl();//객체를 만들었기때문에 생성자로 간다.
		circle.setPointX(px); //py객체를 setter 주입하므로 shape PointX에 들어가게 된다. 
		System.out.println(circle.make()); //주입되는것에 따라 CircleImpl클래스의 make()를 호출한다.

		
		RectangleImpl rec = new RectangleImpl();
		rec.setPointY(py);
		System.out.println(rec.make());
		*/
		
		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
		//xml에 작성된 코드에 따라 myPkg2를 스캔한다.
		//@Component()로 객체를 만들고, 
		
		Circle circle =(Circle)context.getBean("myCircleImpl"); //myCircleImpl =CircleImpl 참조변수
		System.out.println(circle.make()); //myCircleImpl을 관리하는 circle이 make메서드를 호출한다.
	
		Rectangle rec = (Rectangle)context.getBean("myRectangleImpl");
		System.out.println(rec.make());
		
	}

}

 

 

ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
- xml에 작성된 코드에 따라 myPkg2를 스캔한다.

Circle circle =(Circle)context.getBean("myCircleImpl"); - myCircleImpl =CircleImpl 참조변수
System.out.println(circle.make()); - myCircleImpl을 관리하는 circle이 make메서드를 호출한다.


 

 

 

💾 appContext.xml 

<?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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


		<context:component-scan base-package="myPkg2"/>
	<!-- myPkg2에 가서 스캔을 해보아라 -->

</beans>

 

 

 

 

 

 

 

 

📂 myPkg3

 

💾 Car.java (interface로 생성)

package myPkg3;

public interface Car {
	
	String drive();

}

 

 

💾 Morning.java

package myPkg3;

import org.springframework.stereotype.Component;

@Component("Morning") //객체생성
public class Morning implements Car{

	
	public Morning() { //객체생성하고 생성자로 넘어온다.
		System.out.println("Morning");
	}
	
	@Override
	public String drive() {
		
		return "Morning-drive";
	}

}

 

 

💾 Grandeur.java

package myPkg3;

import org.springframework.stereotype.Component;

@Component("Grandeur")
public class Grandeur implements Car{

	public Grandeur() {
		System.out.println("Grandeur");
	}
	
	@Override
	public String drive() {
	
		return "Grandeur-drive";
	}

}

 

 

💾 Person.java(Interface로 생성)

package myPkg3;

public interface Person {
	
	public void setName(String name);
	public String getName();
	
	public void setAge(int age);
	public int getAge();
	
	public String personDrive();

}

 

 

 

 

💾 Consumer.java

package myPkg3;

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

@Component("myConsumer")
public class Consumer implements Person{

	private String name;
	private int age;
	
	@Autowired
	@Qualifier("Morning")
	private Car car;
	
	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int getAge() {
		return age;
	}

	@Override
	public String personDrive() {
		return car.drive();
	}

}

 

 

 

💾 Main.java

package myPkg3;

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

public class Main {

	public static void main(String[] args) {
		//구입하는 사람 이름과 나이
		//Persondriver를 호출했을때, 모닝객체를 넣으면 모닝의드라이브 출력 그랜져 객체넣으면 그랜져의 드라이브가 나오도록 출력해본다.
		//어노테이션 이용

		
		
		
	ApplicationContext context =new ClassPathXmlApplicationContext("appContext2.xml");
	
	
	Person con=(Person)context.getBean("myConsumer");
	con.setName("철수");
	con.setAge(30);
	System.out.println(con.getName());
	System.out.println(con.getAge());
	System.out.println(con.personDrive());
	
	}

}