Java?Spring的使用注解開發(fā)詳解
使用注解開發(fā)
在Spring4之后,要使用注解開發(fā),必須要保證aop的包導(dǎo)入了
使用注解需要導(dǎo)入context的約束,增加注解的支持
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟注解的支持--> <context:annotation-config/> </beans>
1.bean
2.屬性如何注入
//等價(jià)于<bean id="user" class="com.gongyi.pojo.User"/> //@Component組件 @Component public class User { // 相當(dāng)于<property name="name" value="gongyi"/> @Value("gongyi") public String name; //@Value("muzi") public void setName(String name) { this.name = name; } }
3.衍生的注解
@Component有幾個(gè)衍生注解,我們?cè)趙eb開發(fā)中,會(huì)按照mvc三層架構(gòu)分層!
- dao【@Repository】
- service【@Service】
- controller【@Controller】
這四個(gè)注解功能都是一樣的,都是代表將某個(gè)類注冊(cè)到spring中,裝配bean
4.自動(dòng)裝配
- Autowired:自動(dòng)裝配通過類型。名字 如果Autowired不能唯一自動(dòng)裝配上屬性,則需要通過 @Qualifier(value = "xxx") - @Nullable 字段標(biāo)記了這個(gè)注解,說明這個(gè)字段可以為null - Resource:自動(dòng)裝配通過名字。類型
5.作用域
@Component @Scope("singleton") public class User { // 相當(dāng)于<property name="name" value="gongyi"/> @Value("gongyi") public String name; //@Value("muzi") public void setName(String name) { this.name = name; } }
6.小結(jié)
xml與注解:
- xml更加萬能,適用于任何場合,維護(hù)簡單方便
- 注解 不是自己類使用不了(比如DataSource類),維護(hù)相對(duì)復(fù)雜
xml與注解最佳實(shí)踐:
- xml用來管理bean
- 注解只負(fù)責(zé)完成屬性的注入
- 我們?cè)谑褂玫倪^程中,只需要注意一個(gè)問題:必須讓注解生效,就需要開啟注解的支持
<!--指定要掃描的包,這個(gè)包下的注解就會(huì)生效--> <context:component-scan base-package="com.gongyi"/> <!--開啟注解的支持--> <context:annotation-config/>
代碼show
代碼結(jié)構(gòu)圖:
1.新建一個(gè)模塊:
spring-06-anno
2.新建pojo包及類
//等價(jià)于<bean id="user" class="com.gongyi.pojo.User"/> //@Component組件 @Component @Scope("singleton") public class User { // 相當(dāng)于<property name="name" value="gongyi"/> @Value("gongyi") public String name; //@Value("muzi") public void setName(String name) { this.name = name; } }
3.新建dao包及類
@Repository public class UserDao { }
4.新建service包及類
@Service public class UserService { }
5.新建controller包及類
@Controller public class UserController { }
6.新建配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--指定要掃描的包,這個(gè)包下的注解就會(huì)生效--> <context:component-scan base-package="com.gongyi"/> <!--開啟注解的支持--> <context:annotation-config/> </beans>
7.測試
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) context.getBean("user"); System.out.println(user.name); } }
彩蛋
1.被spring托管的類在idea中的顯示
1)未被托管前
2)配置托管
3)托管后
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
使用JAVA+Maven+TestNG框架實(shí)現(xiàn)超詳細(xì)Appium測試安卓真機(jī)教程
這篇文章主要介紹了使用JAVA+Maven+TestNG框架實(shí)現(xiàn)超詳細(xì)Appium測試安卓真機(jī)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java Spring動(dòng)態(tài)生成Mysql存儲(chǔ)過程詳解
這篇文章主要介紹了Java Spring動(dòng)態(tài)生成Mysql存儲(chǔ)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06spring boot與redis 實(shí)現(xiàn)session共享教程
這篇文章主要介紹了spring boot與redis 實(shí)現(xiàn)session共享教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04Eclipse 項(xiàng)目出現(xiàn)錯(cuò)誤(紅色嘆號(hào))解決方法
這篇文章主要介紹了Eclipse 項(xiàng)目出現(xiàn)錯(cuò)誤(紅色嘆號(hào))解決方法的相關(guān)資料,需要的朋友可以參考下2017-06-06MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解
這篇文章主要為大家介紹了MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07JSON.toJSONString()空字段不忽略修改的問題
這篇文章主要介紹了JSON.toJSONString()空字段不忽略修改的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02