久久久久久久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模板網!

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

相關文檔推薦

主站蜘蛛池模板: 久精品久久 | 国产精品一区在线观看 | 日本亚洲一区二区 | 国产乱肥老妇国产一区二 | 中文字幕一区在线观看视频 | 91精品国模一区二区三区 | 久久久91| 国产精品久久国产愉拍 | 黑人精品欧美一区二区蜜桃 | 日韩一区二区三区四区五区六区 | 亚洲乱码国产乱码精品精98午夜 | 国产激情91久久精品导航 | 久久精品免费观看 | 国产精品毛片一区二区在线看 | 欧美精品一区二区三区在线播放 | 欧美xxxx性xxxxx高清 | 久久国产精品视频免费看 | 欧美一级黑人aaaaaaa做受 | 精品国产不卡一区二区三区 | 一区二区三区成人 | 欧美a级网站 | 色狠狠桃花综合 | 国产精品欧美一区二区三区不卡 | 超碰导航| av成人在线观看 | 国产精品日韩欧美一区二区三区 | 欧产日产国产精品视频 | 天天天天天天天干 | 日本在线网址 | 成人精品在线观看 | 蜜桃视频在线观看www社区 | 精品久久久久久久 | 亚洲精品久久久久久久不卡四虎 | 久热电影| 日本欧美国产在线 | 欧美在线一区二区三区 | 色婷婷久久综合 | 亚洲精品麻豆 | 亚洲国产精品久久久久秋霞不卡 | 91精品国产综合久久婷婷香蕉 | 亚洲国产精品久久久久 |