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