Spring IOC相關注解運用(上篇)
前言
注解配置和xml配置對于Spring的IOC要實現(xiàn)的功能都是一樣的,只是配置的形式不一樣。
準備工作:
- 創(chuàng)建一個新的Spring項目。
- 編寫pojo,dao,service類。
- 編寫空的配置文件,如果想讓該文件支持注解,需要在bean.xml添加新的約束:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd"> <context:component-scan base-package="com.example"/> <context:property-placeholder location="db.properties"/> </beans>
一、@Component
@Component可以代替bean標簽
- 作用:用于創(chuàng)建對象,放入Spring容器,相當于 <bean id="" class="">
- 位置:類上方
- 注意:@Component 注解配置bean的默認id是首字母小寫的類名。也可以手動設置bean的id值。
// 此時bean的id為studentDaoImpl @Component public class StudentDaoImpl implements StudentDao{ public Student findById(int id) { // 模擬根據(jù)id查詢學生 return new Student(1,"程序員","北京"); } // 此時bean的id為studentDao @Component("studentDao") public class StudentDaoImpl implements StudentDao{ public Student findById(int id) { // 模擬根據(jù)id查詢學生 return new Student(1,"程序員","北京"); } }
二、@Repository、@Service、@Controller
作用:這三個注解和@Component的作用一樣,使用它們是為了區(qū)分該類屬于什么層。
位置:
- @Repository用于Dao層
- @Service用于Service層
- @Controller用于Controller層
@Repository public class StudentDaoImpl implements StudentDao{} @Service public class StudentService {}
三、@Scope
作用:指定bean的創(chuàng)建策略
位置:類上方
取值:singleton prototype request session globalsession
@Service @Scope("singleton") public class StudentService {}
測試一下:
@Test public void t1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); StudentDao studentDao = (StudentDao) ac.getBean("studentDao"); System.out.println(studentDao); StudentService service1 = (StudentService) ac.getBean("studentService"); System.out.println(service1.hashCode()); StudentService service2 = ac.getBean("studentService",StudentService.class); System.out.println(service2.hashCode()); }
OK,確實可以
四、@Autowired
作用:從容器中查找符合屬性類型的對象自動注入屬性中。用于代替 <bean> 中的依賴注入配置。
位置:屬性上方、setter方法上方、構造方法上方。
注意:@Autowired 寫在屬性上方進行依賴注入時,可以省略setter方法。
@Component public class StudentService { @Autowired private StudentDao studentDao; public Student findStudentById(int id){ return studentDao.findById(id); } } // 測試方法 @Test public void t2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); StudentService studentService = (StudentService) ac.getBean("studentService"); System.out.println(studentService.findStudentById(1)); }
測試結果:
OK,也是可以的
五、@Qualifier
作用:在按照類型注入對象的基礎上,再按照bean的id注入。
位置:屬性上方
注意:@Qualifier必須和@Autowired一起使用。
如下
@Component public class StudentService { @Autowired @Qualifier("studentDaoImpl2") private StudentDao studentDao; public Student findStudentById(int id){ return studentDao.findById(id); } }
六、@Value
作用:注入String類型和基本數(shù)據(jù)類型的屬性值。
位置:屬性上方以下說明一下用法:
1. 直接設置固定的屬性值
@Value("1") private int count; @Value("hello") private String str;
2. 獲取配置文件中的屬性值
編寫配置文件db.properties
jdbc.username=root jdbc.password=123456
spring核心配置文件(bean.xml)掃描配置文件
<context:property-placeholder location="db.properties"/>
注入配置文件中的屬性值
@Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password;
3. 測試結果
測試方法
// 測試注解Value @Test public void t3(){ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); StudentService service = ac.getBean("studentService",StudentService.class); System.out.println(service); }
運行結果
OK,應該和上面設置的值一樣,說明可以使用,本篇就介紹到這幾個注解了,下篇會介紹完接下來的注解。
以上就是Spring IOC相關注解運用(上篇)的詳細內(nèi)容,更多關于Spring IOC注解的資料請關注腳本之家其它相關文章!
相關文章
Java實現(xiàn)UDP通信過程實例分析【服務器端與客戶端】
這篇文章主要介紹了Java實現(xiàn)UDP通信過程,結合實例形式分析了java實現(xiàn)UDP服務器端與客戶端相關操作技巧與注意事項,需要的朋友可以參考下2020-05-05最簡單的Spring Cloud教程第一篇:服務的注冊與發(fā)現(xiàn)(Eureka)
這篇文章主要給大家介紹了關于Spring Cloud服務的注冊與發(fā)現(xiàn)(Eureka)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring cloud具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-08-08