Java?Spring的使用注解開發(fā)詳解
使用注解開發(fā)
在Spring4之后,要使用注解開發(fā),必須要保證aop的包導入了
使用注解需要導入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.屬性如何注入
//等價于<bean id="user" class="com.gongyi.pojo.User"/> //@Component組件 @Component public class User { // 相當于<property name="name" value="gongyi"/> @Value("gongyi") public String name; //@Value("muzi") public void setName(String name) { this.name = name; } }
3.衍生的注解
@Component有幾個衍生注解,我們在web開發(fā)中,會按照mvc三層架構分層!
- dao【@Repository】
- service【@Service】
- controller【@Controller】
這四個注解功能都是一樣的,都是代表將某個類注冊到spring中,裝配bean
4.自動裝配
- Autowired:自動裝配通過類型。名字 如果Autowired不能唯一自動裝配上屬性,則需要通過 @Qualifier(value = "xxx") - @Nullable 字段標記了這個注解,說明這個字段可以為null - Resource:自動裝配通過名字。類型
5.作用域
@Component @Scope("singleton") public class User { // 相當于<property name="name" value="gongyi"/> @Value("gongyi") public String name; //@Value("muzi") public void setName(String name) { this.name = name; } }
6.小結
xml與注解:
- xml更加萬能,適用于任何場合,維護簡單方便
- 注解 不是自己類使用不了(比如DataSource類),維護相對復雜
xml與注解最佳實踐:
- xml用來管理bean
- 注解只負責完成屬性的注入
- 我們在使用的過程中,只需要注意一個問題:必須讓注解生效,就需要開啟注解的支持
<!--指定要掃描的包,這個包下的注解就會生效--> <context:component-scan base-package="com.gongyi"/> <!--開啟注解的支持--> <context:annotation-config/>
代碼show
代碼結構圖:
1.新建一個模塊:
spring-06-anno
2.新建pojo包及類
//等價于<bean id="user" class="com.gongyi.pojo.User"/> //@Component組件 @Component @Scope("singleton") public class User { // 相當于<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"> <!--指定要掃描的包,這個包下的注解就會生效--> <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)托管后
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
使用JAVA+Maven+TestNG框架實現(xiàn)超詳細Appium測試安卓真機教程
這篇文章主要介紹了使用JAVA+Maven+TestNG框架實現(xiàn)超詳細Appium測試安卓真機教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Java Spring動態(tài)生成Mysql存儲過程詳解
這篇文章主要介紹了Java Spring動態(tài)生成Mysql存儲過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06spring boot與redis 實現(xiàn)session共享教程
這篇文章主要介紹了spring boot與redis 實現(xiàn)session共享教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04Eclipse 項目出現(xiàn)錯誤(紅色嘆號)解決方法
這篇文章主要介紹了Eclipse 項目出現(xiàn)錯誤(紅色嘆號)解決方法的相關資料,需要的朋友可以參考下2017-06-06JSON.toJSONString()空字段不忽略修改的問題
這篇文章主要介紹了JSON.toJSONString()空字段不忽略修改的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02