Spring IOC相關(guān)注解運(yùn)用(上篇)
前言
注解配置和xml配置對于Spring的IOC要實(shí)現(xiàn)的功能都是一樣的,只是配置的形式不一樣。
準(zhǔn)備工作:
- 創(chuàng)建一個新的Spring項(xiàng)目。
- 編寫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標(biāo)簽
- 作用:用于創(chuàng)建對象,放入Spring容器,相當(dāng)于 <bean id="" class="">
- 位置:類上方
- 注意:@Component 注解配置bean的默認(rèn)id是首字母小寫的類名。也可以手動設(shè)置bean的id值。
// 此時bean的id為studentDaoImpl @Component public class StudentDaoImpl implements StudentDao{ public Student findById(int id) { // 模擬根據(jù)id查詢學(xué)生 return new Student(1,"程序員","北京"); } // 此時bean的id為studentDao @Component("studentDao") public class StudentDaoImpl implements StudentDao{ public Student findById(int id) { // 模擬根據(jù)id查詢學(xué)生 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,確實(shí)可以
四、@Autowired
作用:從容器中查找符合屬性類型的對象自動注入屬性中。用于代替 <bean> 中的依賴注入配置。
位置:屬性上方、setter方法上方、構(gòu)造方法上方。
注意:@Autowired 寫在屬性上方進(jìn)行依賴注入時,可以省略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)); }
測試結(jié)果:
OK,也是可以的
五、@Qualifier
作用:在按照類型注入對象的基礎(chǔ)上,再按照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. 直接設(shè)置固定的屬性值
@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. 測試結(jié)果
測試方法
// 測試注解Value @Test public void t3(){ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); StudentService service = ac.getBean("studentService",StudentService.class); System.out.println(service); }
運(yùn)行結(jié)果
OK,應(yīng)該和上面設(shè)置的值一樣,說明可以使用,本篇就介紹到這幾個注解了,下篇會介紹完接下來的注解。
以上就是Spring IOC相關(guān)注解運(yùn)用(上篇)的詳細(xì)內(nèi)容,更多關(guān)于Spring IOC注解的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)UDP通信過程實(shí)例分析【服務(wù)器端與客戶端】
這篇文章主要介紹了Java實(shí)現(xiàn)UDP通信過程,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)UDP服務(wù)器端與客戶端相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05詳解Java如何通過裝飾器模式擴(kuò)展系統(tǒng)功能
這篇文章主要為大家詳細(xì)介紹了Java如何通過裝飾器模式擴(kuò)展系統(tǒng)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04最簡單的Spring Cloud教程第一篇:服務(wù)的注冊與發(fā)現(xiàn)(Eureka)
這篇文章主要給大家介紹了關(guān)于Spring Cloud服務(wù)的注冊與發(fā)現(xiàn)(Eureka)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring cloud具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-08-08Java 獲取指定日期的實(shí)現(xiàn)方法總結(jié)
以下是對Java中獲取指定日期的實(shí)現(xiàn)方法進(jìn)行了歸納總結(jié),需要的朋友可以參考下2013-07-07