웹 & 프레임워크

Spring @component 이용법

늘근이 2014. 6. 27. 14:27

<!-- @Component쓰더라도 안에  찾는 놈이 필요하다

default 아까 했던 애노테이션 컨피그 같은놈을 내장하고 있다.

근데  설정파일로만 보면  알수가 없다.-->

<context:component-scan base-package="com.gorakgarak.sample2.autoscan"/>

 

다음은 전체코드임

 /////////////////////////////////////////////////////////////////////////////////////////////////

 

package com.gorakgarak.sample2.autoscan;

 

import org.springframework.stereotype.Component;

 

@Component("outputterConsole")

public class OutputServiceImplConsole implements OutputService {

 

                  @Override

                  public void output(String message) {

                                   System.out.println(message);

 

                  }

 

}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

@Component("outputter")

public class OutputServiceImplFile implements OutputService {

 

                  private String fileName;

                 

                  /*@Value(value=="")*/

                  @Value("log.txt")

                  public void setFileName(String fileName) {

                                   this.fileName = fileName;

                  }

 

                  @Override

                  public void output(String message) {

                                   PrintWriter out =null;

                                   try {

                                                     out = new PrintWriter(new FileWriter(fileName,true));

                                                     //true이면 append, false  overwrite

                                                     out.println(message);

                                   } catch (Exception e) {

                                                     e.printStackTrace();

                                   }finally{

                                                     if(out != null){

                                                                       out.close();

                                                     }

                                   }

 

                  }

 

}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@Component("greeting")

public class GreetingServiceImpl implements GreetingService {

 

                  // DI, 넣어줄수있어야한다

                  // 여기서는 생성자를 이용하는방법과 setter 이용하는 방법이있다

                  //여기는 인터페이스만 가져온다. (spring이선택하게함)

                 

                  private OutputService outputter;

                 

                  @Autowired

                  public void setOutputter(OutputService outputter) {

                                   System.out.println("set.... : " + outputter);

                                   this.outputter = outputter;

                  }

 

                  public void sayHello(String name) {

                                   outputter.output("Hello" + name);

                  }

                 

}