실습 3일차_@Autowired

 

 

 

 Spring @Autowired

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service = ctx.getBean("service", UserService.class);
		List<String> list = service.list();
		System.out.println(list);
		
		UserService service2 = ctx.getBean("service2", UserService.class);
		List<String> list2 = service2.list();
		System.out.println(list2);
		
		UserService service3 = ctx.getBean("service3", UserService.class);
		List<String> list3 = service3.list();
		System.out.println(list3);
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
		return list;
	}
}

UserService.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.dao.UserDAO;

public class UserService {

	@Autowired
	UserDAO dao;
	
	// user.xml 2. property는 기본 생성자가 있어야 에러가 안남
	public UserService() {
		
	}
	
	// 생성자 주입
	public UserService(UserDAO dao) {
		this.dao = dao;
	}
	
	// setter 메서드 주입
	public void setDao(UserDAO dao) {
		this.dao = dao;
	}

	public List<String> list(){
		return dao.list();
	}
}

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

	<bean id="dao" class="com.dao.UserDAO" />
	
	<!-- @ 어노테이션 활성화  -->
	<context:annotation-config />
	
	<!-- 1. xml의 constructor를 이용한 의존성 주입_Injection -->
	<bean id="service" class="com.service.UserService">
		<constructor-arg name="dao" ref="userDAO" />
	</bean>
	
	<!-- 2. xml의 setter 메서드를 이용한 의존성 주입_Injection -->
	<bean id="service2" class="com.service.UserService">
		<property name="dao" ref="userDAO" />
	</bean>
	
	<!-- 3. @Autowired를 이용한 자동 의존성 주입_Injection -->
	<bean id="service3" class="com.service.UserService" />

</beans>

 

<!-- @ 어노테이션 활성화 -->
<context:annotation-config />

* context 활성화 방법

[Namespaces 탭] → [context] Check → Configure Namespaces 알림창 OK 버튼 마우스로 클릭 

 

 

 

* 출력 화면 

 

 Spring @Autowired_required_false

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service3 = ctx.getBean("service3", UserService.class);
		List<String> list3 = service3.list();
		System.out.println(list3);
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
		return list;
	}
}

UserService.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.dao.UserDAO;

public class UserService {

	@Autowired(required = false) // 가장 많이 사용
	UserDAO dao;

	public List<String> list(){
		return dao.list();
	}
}

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

	<bean id="dao" class="com.dao.UserDAO" />
	
	<!-- @ 어노테이션 활성화  -->
	<context:annotation-config />
	
	<!-- 3. @Autowired를 이용한 자동 의존성 주입_Injection -->
	<bean id="service3" class="com.service.UserService" />

</beans>

* 출력 화면 

 

 Spring @Autowired_@Qualifier

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service3 = ctx.getBean("service3", UserService.class);
		List<String> list3 = service3.list();
		System.out.println(list3);
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
		return list;
	}
}

UserService.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.dao.UserDAO;

public class UserService {

	@Autowired // 가장 많이 사용
	@Qualifier("userDAO") // 명시적으로 누구를 쓸 것인지 찍어서 설정하면 에러가 안남 ㅋ
	UserDAO dao;

	public List<String> list(){
		return dao.list();
	}
}

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

	<!-- @ 어노테이션 활성화  -->
	<context:annotation-config />
	
	<!-- 3. @Autowired를 이용한 자동 의존성 주입_Injection -->
	<bean id="service3" class="com.service.UserService" />
	<bean id="userDAO" class="com.dao.UserDAO" />
	<bean id="userDAO2" class="com.dao.UserDAO" />

</beans>

 

* 출력 화면 

 

 출처

 

@Autowired : 

+ 강의 교재

 

+ Recent posts