思維導圖:

這邊我用一個案例來說明,
在personService中用注解@Autowired注入personDao這個類
開啟注解(也叫掃包)
要使用注解,首先要在組態檔中開啟注解:
//這里我是掃描我自己com.lbl的包
<context:component-scan base-package="com.lbl"></context:component-scan>
代碼:
PersonServiceTest
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);
}
}
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;
}
PersonDao
package com.lbl.dao;
import org.springframework.stereotype.Repository;
@Repository
public class PersonDao {
}
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/165855.html
標籤:java
下一篇:為什么浮點資料表示有誤差?
