spring 주입 방식 의 AutoWired 주입

3497 단어 Spring
주해 방식 은 두 가지 가 있다.
DAO
public class PersonDao {
	public void add(){
		System.out.println("dao add  ");
	}
}

1. Service (변수 에 주석 달기)
public class Service {

	/*
	 * @Autowired  
	 *   *        ,           
	 *   *               set   (    )
	 *   
	 *             (spring       )
	 *      *        @Autowired    (private PersonDao personDao;)      .    com.xxc.iocc.autoWiredField.dao.PersonDao
	 *      
	 *      *           spring         com.xxc.iocc.autoWiredField.dao.PersonDao bean  
	 *              
	 *              
	 *      *       ,       private PersonDao personDao      
	 *      
	 *      * @Autowired   @Qualifier("personDao")    bean id         ,@Qualifier      
	 */
	
	@Autowired @Qualifier("personDao22")
	private PersonDao personDao;
	
	public void add(){
		System.out.println("service add  ");
		personDao.add();
	}
}

2. Service 주석 은 방법 상
public class Service {

	private PersonDao personDao;
	
	
	public PersonDao getPersonDao() {
		return personDao;
	}

	
	/*
	 * @Autowired  
	 *   *        ,           
	 *   *               set   (    )
	 *   
	 *             (spring       )
	 *      *        @Autowired  setPersonDao(PersonDao personDao),         
	 *      
	 *      *             spring         com.xxc.iocc.autoWiredMethod.dao.PersonDao bean  
	 *              
	 *              
	 *      *  bean                 setPersonDao(PersonDao personDao)           
	 *      
	 *      * @Qualifier("personDao")             ,           
	 */
	@Autowired 
	public void setPersonDao(@Qualifier("personDao22")PersonDao personDao) {
		this.personDao = personDao;
	}

	public void add(){
		System.out.println("service add  ");
		personDao.add();
	}
}

applicationContext.xml


	
     
	
	
	 
	


테스트 클래스
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/iocc/autoWiredField/applicationContext.xml");
		Service service = (Service)ac.getBean("service");
		service.add();
	}
}

좋은 웹페이지 즐겨찾기