Spring注解與P/C命名空間超詳細(xì)解析
注解實(shí)現(xiàn)自動(dòng)裝配
@Autowire注解
@Autowire注解,自動(dòng)裝配通過(guò)類型,名字如果Autowire不能唯一自動(dòng)裝配上屬性,
則需要通過(guò)@Qualifier(value=“xxx”)
配置:
導(dǎo)入約束
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
開(kāi)啟注解支持
<context:annotation-config/>
實(shí)現(xiàn):
Dog/Cat類和方法的實(shí)現(xiàn)還是不變
People類
- 實(shí)現(xiàn)@Autowire注解、可以在屬性上使用、也可以在set方式使用
- 編寫(xiě)@Autowire注解后,set方法可以省略不寫(xiě)
public class People { @Autowired private Cat cat; @Autowired private Dog dog; private String name; }
beans.xml
<bean id="cat" class="com.wei.pojo.Cat"/> <bean id="dog" class="com.wei.pojo.Dog"/> <bean id="people" class="com.wei.pojo.People"/>
@Nullable 字段標(biāo)記了這個(gè)注解,說(shuō)明這個(gè)字段可以為null
可以在帶參構(gòu)造中使用@Nullable
public People(@Nullable String name) { this.name = name; }
@Qualifier注解
指定一個(gè)唯一的bean對(duì)象注入
People類
public class People { @Autowired @Qualifier(value = "cat111") private Cat cat; @Autowired @Qualifier(value = "dog222") private Dog dog; private String name; }
beans.xml
<bean id="cat" class="com.wei.pojo.Cat"/> <bean id="cat111" class="com.wei.pojo.Cat"/> <bean id="dog" class="com.wei.pojo.Dog"/> <bean id="dog222" class="com.wei.pojo.Dog"/> <bean id="people" class="com.wei.pojo.People"/>
@Resource注解
默認(rèn)按照名字裝配Bean,即會(huì)按照name屬性的值來(lái)找到具有相同id的Bean Definition 并注入
People類
public class People { @Resource(name = "cat2") private Cat cat; @Resource private Dog dog; private String name; }
beans.xml
<bean id="cat1" class="com.wei.pojo.Cat"/> <bean id="cat2" class="com.wei.pojo.Cat"/> <bean id="dog" class="com.wei.pojo.Dog"/> <bean id="people" class="com.wei.pojo.People"/>
總結(jié):
- @Autowire 通過(guò)byType方式實(shí)現(xiàn),而且必須要求對(duì)象存在,不能為null
- @Resource 默認(rèn)通過(guò)byname的方式實(shí)現(xiàn),如果找不到名字,則通過(guò)byType實(shí)現(xiàn),都不行則報(bào)錯(cuò)
@Component
放在類上,說(shuō)明這個(gè)類被Spring管理了,等同于bean配置
Java類
此處使用注解@Component @Value 則 beans.xml則無(wú)需書(shū)寫(xiě)bean
//等價(jià)于<bean id="user" class="com.wei.pojo.User Component:組件 @Component public class User { @Value("wei_shuo") //相當(dāng)于 <property name="name" value="wei_shuo"/> public String name; }
@Component的衍生注解,在web開(kāi)發(fā)中,按照MVC三層架構(gòu)分層
四個(gè)注解功能、作用相同,都是代表將某個(gè)類注冊(cè)到Spring中、裝配Bean
- pojo —> @Component
- dao —> @Resource
- service —> @Service
- controller —> @Controller
@Scope
作用域注解,限定Spring Bean的作用范圍,在Spring配置文件定義Bean時(shí),通過(guò)聲明scope配置項(xiàng),可以靈活定義Bean的作用范圍
@ComponentScan
@ComponentScan(“com.wei.pojo”) 掃描包
@Bean
- 注冊(cè)一個(gè)bean,就相當(dāng)于bean標(biāo)簽
- 方法的名字,相當(dāng)于bean標(biāo)簽的id屬性
- 方法的返回值,相當(dāng)于bean標(biāo)簽的class屬性
@Bean public User getUser(){ return new User(); //就是返回要注入到bean的對(duì)象 }
@Configuration
@Configuration注解會(huì)被Spring容器托管,因?yàn)樗旧砭褪且粋€(gè)@Component
@Configuration 代表這是一個(gè)配置類,相當(dāng)于beans.xml
@Configuration @ComponentScan("com.wei.pojo") @Import(WeiConfig2.class) public class WeiConfig { @Bean public User getUser(){ return new User(); } }
@Value
屬性注入值
Java類
此處使用@Scope注解使用prototype多實(shí)例注解模式
//等價(jià)于<bean id="user" class="com.wei.pojo.User Component:組件 @Component @Scope("prototype") //作用域注解 public class User { @Value("wei_shuo") //相當(dāng)于 <property name="name" value="wei_shuo"/> public String name; }
P命名空間注入
p命名空間注入,對(duì)應(yīng)Set注入方式
需要?jiǎng)?chuàng)建set/get方法
p命名空間注入,可以直接注入屬性的值:property
User類
package com.wei.pojo; public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
userbeans.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--上面添加 xmlns:p="http://www.springframework.org/schema/p" --> <!--p命名空間注入,可以直接注入屬性的值:property--> <bean id="user" class="com.wei.pojo.User" p:name="秦疆" p:age="18"/> </beans>
Test測(cè)試
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml"); // 指定類型User.class則不需要強(qiáng)制轉(zhuǎn)換 User user = context.getBean("user", User.class); // 強(qiáng)制轉(zhuǎn)換 // User user = (User) context.getBean("user"); System.out.println(user);
C命名空間注入
c命名空間注入,對(duì)應(yīng)構(gòu)造器注入
需要?jiǎng)?chuàng)建無(wú)參/帶參構(gòu)造方法
c命名空間注入,通過(guò)構(gòu)造器 注入:construct-args
User類
此處增加無(wú)參/帶參構(gòu)造方法
public class User { private String name; private int age; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
userbeans.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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--上面添加 xmlns:c="http://www.springframework.org/schema/c" --> <!--c命名空間注入,通過(guò)構(gòu)造器 注入:construct-args--> <bean id="user2" class="com.wei.pojo.User" c:name="wei_shuo" c:age="18"/> </beans>
Test測(cè)試
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml"); User user = context.getBean("user2", User.class); System.out.println(user);
總結(jié):
p命名空間注入/c命名空間注入 都需要導(dǎo)入xml約束,不能直接使用
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
Spring開(kāi)發(fā)包名解釋
dao包
數(shù)據(jù)庫(kù)操作,crud 即增刪改查,對(duì)于數(shù)據(jù)庫(kù)的增刪改查的操作都在這里
pojo包
簡(jiǎn)單java對(duì)象
service包
service 服務(wù)器層,也叫業(yè)務(wù)邏輯層,調(diào)用dao中的方法
Java方式配置
使用Java的方式配置Spring,也就是不寫(xiě)bean.xml配置,使用注解代替
實(shí)體列
//@Configurable放在類上,說(shuō)明這個(gè)類被Spring管理了,等同于bean配置,注冊(cè)到容器中 @Configurable public class User { private String name; public String getName() { return name; } @Value("CSDN") //屬性注入值 public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
配置文件
- @Configuration注解會(huì)被Spring容器托管,因?yàn)樗旧砭褪且粋€(gè)@Component
- @Configuration 代表這是一個(gè)配置類,相當(dāng)于beans.xml
- @Import注解,功能就是和Spring XML里面的一樣. @Import注解是用來(lái)導(dǎo)入配置類或者一些需要前置加載的類.通俗的將就是將類放入到IOC容器中
- @Bean注解,注冊(cè)一個(gè)bean,就相當(dāng)于bean標(biāo)簽,方法的名字,相當(dāng)于bean標(biāo)簽的id屬性,方法的返回值,相當(dāng)于bean標(biāo)簽的class屬性
//@Configuration注解會(huì)被Spring容器托管,因?yàn)樗旧砭褪且粋€(gè)@Component //@Configuration 代表這是一個(gè)配置類,相當(dāng)于beans.xml @Configuration @ComponentScan("com.wei.pojo") //掃描包 @Import(WeiConfig2.class) //@Import注解,功能就是和Spring XML里面的一樣. @Import注解是用來(lái)導(dǎo)入配置類或者一些需要前置加載的類.通俗的將就是將類放入到IOC容器中 public class WeiConfig { //注冊(cè)一個(gè)bean,就相當(dāng)于bean標(biāo)簽 //方法的名字,相當(dāng)于bean標(biāo)簽的id屬性 //方法的返回值,相當(dāng)于bean標(biāo)簽的class屬性 @Bean public User getUser(){ return new User(); //就是返回要注入到bean的對(duì)象 } }
測(cè)試類
public class MyTest { //如果完全使用了配置類方式去做,只能通過(guò)AnnotationConfig 上下文來(lái)獲取容器,通過(guò)配置類的class對(duì)象加載 public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(WeiConfig.class); User getUser = (User) context.getBean("getUser"); //此處取方法名 System.out.println(getUser.getName()); } }
到此這篇關(guān)于Spring注解與P/C命名空間超詳細(xì)解析的文章就介紹到這了,更多相關(guān)Spring注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn)
這篇文章主要介紹了Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java設(shè)計(jì)模式編程中簡(jiǎn)單工廠與抽象工廠模式的使用實(shí)例
這篇文章主要介紹了Java設(shè)計(jì)模式編程中簡(jiǎn)單工廠與抽象工廠模式的使用實(shí)例,簡(jiǎn)單工廠與抽象工廠都可以歸類于設(shè)計(jì)模式中的創(chuàng)建型模式,需要的朋友可以參考下2016-04-04AsyncHttpClient?RequestFilter請(qǐng)求篩選源碼解讀
這篇文章主要為大家介紹了AsyncHttpClient?RequestFilter請(qǐng)求篩選源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Intellij IDEA Debug調(diào)試技巧(小結(jié))
這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10簡(jiǎn)單了解java標(biāo)識(shí)符的作用和命名規(guī)則
這篇文章主要介紹了簡(jiǎn)單了解java標(biāo)識(shí)符的作用和命名規(guī)則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Spring Cloud Gateway 緩存區(qū)異常問(wèn)題及解決方案
最近在測(cè)試環(huán)境spring cloud gateway突然出現(xiàn)了異常,接下來(lái)通過(guò)本文給大家介紹Spring Cloud Gateway 緩存區(qū)異常問(wèn)題解決方案,需要的朋友可以參考下2024-06-06SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限信息的方法
這篇文章主要介紹了SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定需要的朋友可以參考下2022-01-01