思維導圖:
這邊我用一個案例來說明。
在personService中用註解@Autowired
注入personDao這個類
要使用註解,首先要在組態檔中開啟註解:
//這裡我是掃描我自己com.lbl的包
<context:component-scan base-package="com.lbl"></context:component-scan>
package com.lbl.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonServiceTest {
ClassPathXmlApplicationContext onctext=new
ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test01(){
//這裡用類名的小寫獲取
Object personService = onctext.getBean("personService");
System.out.println(personService);
}
}
package com.lbl.service;
import com.lbl.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonService {
@Autowired
PersonDao personDao;
}
package com.lbl.dao;
import org.springframework.stereotype.Repository;
@Repository
public class PersonDao {
}