5 使用注解開發
module:spring-06-anno
說明
在Spring4之后,想要使用注解,必須要引入aop的jar包

在組態檔中還必須要引入一個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
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/>
</beans>
bean的實作
之前都是使用 bean 的標簽進行bean注入,但是實際開發中,我們一般都會使用注解!
1、配置掃描哪些包下的注解
<context:component-scan base-package="com.zzb"/>
2、在指定包下撰寫類,并且增加注解
package com.zzb.pojo;
import org.springframework.stereotype.Component;
// 相當于組態檔中 <bean id="user" />
@Component("user")
public class User {
public String name = "ZZB";
}
3、測驗
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.name);
}
測驗結果:
ZZB
成功輸出!
屬性注入
使用注解注入屬性
1、可以不用提供set方法,直接在屬性欄位名上添加@Value("值")注解
package com.zzb.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 相當于組態檔中 <bean id="user" />
@Component("user")
public class User {
@Value("ZZB")
// 相當于組態檔中 <property name="name" value="https://www.cnblogs.com/zzbstudy/p/秦疆"/>
public String name;
}
2、如果提供了 set方法,可以在set方法上添加@Value("值")注解
package com.zzb.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("user")
public class User {
public String name;
@Value("ZZB")
public void setName(String name) {
this.name = name;
}
}
衍生注解
這些注解其實就是替代了組態檔中的配置步驟,更加的方便快捷!
@Component三個衍生注解
為了更好的進行分層,Spring可以使用其它三個注解,功能一樣,目前使用哪一個功能都一樣,
- @Controller:web層
- @Service:service層
- @Repository:dao層
寫上這些注解,就相當于將這個類交給Spring管理裝配了!
自動裝配注解
前文已經介紹過!
作用域
@scope
- singleton:默認的,Spring會采用單例模式創建這個物件,關閉工廠 ,所有的物件都會銷毀,
- prototype:多例模式,關閉工廠 ,所有的物件不會銷毀,內部的垃圾回識訓制會回收,
package com.zzb.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("user")
@Scope("singleton")
public class User {
public String name;
@Value("ZB")
public void setName(String name) {
this.name = name;
}
}
總結:
XML與注解比較
XML與注解比較
- XML可以適用任何場景 ,結構清晰,維護方便
- 注解不是自己提供的類使用不了,開發簡單方便
XML與注解整合開發 :推薦最佳實踐
- xml管理Bean
- 注解完成屬性注入
- 使用程序中, 可以不用掃描,掃描是為了類上的注解
注意開啟注解:
<context:annotation-config/>
作用:
- 進行注解驅動注冊,從而使注解生效
- 用于激活那些已經在spring容器里注冊過的bean上面的注解,也就是顯示的向Spring注冊
- 如果不掃描包,就需要手動配置bean
- 如果不加注解驅動,則注入的值為null!
基于Java類進行配置
module:spring-07-appconfig
JavaConfig 原來是 Spring 的一個子專案,它通過 Java 類的方式提供 Bean 的定義資訊,在 Spring4 的版本, JavaConfig 已正式成為 Spring4 的核心功能 ,
1、撰寫一個物體類,Dog
package com.zzb.pojo;
import org.springframework.stereotype.Component;
@Component // 可以被組態檔掃描自動裝配
public class Dog {
public String name = "dog";
}
2、新建一個config配置包,撰寫一個MyConfig配置類
package com.zzb.config;
import com.zzb.pojo.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //代表這是一個配置類
public class ZzbConfig {
@Bean //通過方法注冊一個bean,這里的回傳值就Bean的型別,方法名就是bean的id!
public Dog getDog(){
return new Dog();
}
}
3、測驗
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(ZzbConfig.class);
// 通過@Bean配置
Dog dog = context.getBean("getDog", Dog.class);
System.out.println(dog.name);
}
測驗結果:
dog
結果成功輸出!
匯入其他配置如何做呢?
1、我們再撰寫一個配置類!
@Configuration //代表這是一個配置類
public class ZzbConfig2 {
}
2、在之前的配置類中我們來選擇匯入這個配置類
package com.zzb.config;
import com.zzb.pojo.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.zzb.pojo")
@Import(ZzbConfig2.class)
public class ZzbConfig {
@Bean
public Dog getDog(){
return new Dog();
}
}
3、測驗
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ZzbConfig.class);
// 通過@Bean配置
Dog dog = context.getBean("getDog", Dog.class);
// 通過@Component配置,@ComponentScan("com.zzb.pojo")
Dog dog2 = context.getBean("dog", Dog.class);
System.out.println(dog.name);
System.out.println(dog2.name);
}
關于這種Java類的配置方式,我們在之后的SpringBoot 和 SpringCloud中還會大量看到,我們需要知道這些注解的作用即可!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232851.html
標籤:Java
上一篇:33 檔案IO流(二)
下一篇:6 代理模式
