웹 & 프레임워크

Spring Auto-Wire, ByType, ByName

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

beans.xml 을 그냥 복사하면 설정을 해줘야한다

오른쪽 눌러서, spring tools에 가서 add 해준다

 

<?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:p="http://www.springframework.org/schema/p"

                  xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

 

                  <!-- byName, byType둘다 될거임 byType 초기화를 하다가 집어넣으려고 그랬더니 두놈이나 나옴

                  그래서 NPE 아닌 이상한 에러가 나는거임 unsatisfiedDependencyException

                  설정을 읽는 과정에서 에러가남사용하는과정에서 에러에러byType 같은타입여러개면 안됨

                  정확하게 같은 타입의 bean  한개만 등록되어있을때 쓸수있음

-->

<bean id="greeting" class="com.gorakgarak.sample3.autowire.GreetingServiceImpl"

autowire="byType" />

 

<!--  밑의 주석을 풀면, byType 사용을 못한다왜냐면 두개놈이나 있기때문에, id 맞추든 안맞추든

동작이 안되고 에러가 난다두개를 해놓고 하려면 byName! 따라서 setter 이름으로 id 주는게

제일 활용도가 높을것이다.

byContructer 매개변수 type기반으로 해줌. byType이랑 같은건데 차이점은 생성자기반으로 세팅할때

거시기인것이다. -->

<!-- <bean id="outputter" class="com.gorakgarak.sample3.autowire.OutputServiceImplConsole" /> -->

<bean id="outputterFile" class="com.gorakgarak.sample3.autowire.OutputServiceImplFile"

p:fileName="data.dat"/>

 

</beans>