久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Spring詳解使用注解開發流程

這篇文章主要為大家詳細介紹了Spring如何使用注解開發,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,需要的可以參考一下

在Spring4之后 要使用注解開發 必須保證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>

@Component:組件放在類上 說明這個類被Spring管理了 就是bean

import org.springframework.stereotype.Component;
//等價于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
    public String name = "xxx";
}

@Value

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等價于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
    @Value("xxx")
//等價于<property name="name" value="xxx"/>
    public String name;
}

或者

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等價于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {  
    public String name;
    @Value("xxx")
    public void setName(String name) {
        this.name = name;
    }
}

@Component有幾個衍生的注解 我們在Web開發中會按照MVC三層架構分層

·dao[@Repository]

·service[@Service]

·controller[@Controller]

這四個注解功能一樣 都是代表將某個類注冊到Spring中 裝配Bean

注解的作用域@Scope

@Scope 放在類上,默認是單例模式

@Scope(prototype)是原型模式,每次創建的都是一個新的對象

其作用等價于

補充:

@Scope("singleton") 或者@Scope 單例模式 下面代碼輸出結果為true

@Scope("prototype")下面代碼輸出結果為false

import com.kero.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println(user==user2);
    }
}

xml vs 注解

·xml更加萬能 適用于任何場合 維護簡單方便

·注解 不是自己類使用不聊 維護相對復雜

最佳實踐:xml用來管理bean

注解只負責完成屬性的注入

我們在使用的過程中 需要注意 使用以下代碼

<!--指定要掃描的包 這個包下的注解就會生效->-->
    <context:component-scan base-package="com.kero"/>
    <!--開啟注解的支持-->
    <context:annotation-config/>

針對最佳實踐的例子

<?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.kero"/>
    <!--開啟注解的支持-->
    <context:annotation-config/>
    <bean id="user" class="com.kero.pojo.User" scope="prototype"/>
</beans>
import org.springframework.beans.factory.annotation.Value;
public class User {
    @Value("XXX")
    public String name;
    public void setName(String name) {
        this.name = name;
    }
}

到此這篇關于Spring詳解使用注解開發流程的文章就介紹到這了,更多相關Spring注解內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

主站蜘蛛池模板: 国产精品美女久久久久aⅴ国产馆 | 亚洲欧美一区二区三区1000 | 欧美激情精品久久久久久 | 人人干人人超 | 亚洲成人一区二区 | 国产免费又黄又爽又刺激蜜月al | 亚洲综合色视频在线观看 | 久草在线青青草 | 99精品国产在热久久 | 日韩亚洲欧美综合 | 四虎影院在线观看av | 欧美一a一片一级一片 | 美女国内精品自产拍在线播放 | 精品久久影院 | 日韩久久综合网 | 日日干日日操 | 欧美日韩亚洲一区二区 | 免费午夜视频 | 亚洲精品aⅴ | 波多野结衣一区二区 | 日韩精品1区2区3区 成人黄页在线观看 | 国产日韩av一区二区 | 激情网站在线 | 精品久久一区 | 欧美美女爱爱 | 成人做爰www免费看 午夜精品久久久久久久久久久久 | 福利社午夜影院 | 黄网免费看 | 欧美a v在线 | 看羞羞视频 | heyzo在线| 成人欧美一区二区三区在线播放 | 色视频网站在线观看 | 精品在线一区二区 | 亚洲精品天堂 | 黑人巨大精品欧美一区二区免费 | 91久久综合亚洲鲁鲁五月天 | 国产99久久精品一区二区永久免费 | 亚洲国产精品va在线看黑人 | 99久久精品国产毛片 | 日韩高清中文字幕 |