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

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

相關文檔推薦

主站蜘蛛池模板: 精品99视频 | 日本高清www | 欧美专区在线观看 | 丁香激情五月 | 快播少女爱欢乐 | 久久香蕉国产 | 岛国av噜噜噜久久久狠狠av | 波多野吉衣一二三区乱码 | 乳大翘臀1v1h糙汉 | 成人羞羞国产免费游戏 | 一区二区三区成人 | 好好的日com | 日韩成人在线播放 | 亚洲精品国产精品乱码不卡 | 欧美在线观看一区二区 | 精品久久久久久一区二区里番 | 黄色网免费 | 免费欧美视频 | 国产天堂av | 80日本xxxxxxxxx96 国产成人在线免费视频 | 亚洲人在线观看 | 日日夜夜干 | 黄色免费观看视频 | 黄色一级大片在线免费看国产一 | 三上悠亚一区 | 国产精品2| www色| 久久久不卡 | 青青久久久| 日本韩国欧美中文字幕 | 国产精品区二区三区日本 | 国产黄色av网站 | 免费一区二区视频 | 国产精品二区一区二区aⅴ污介绍 | 黄色片视频免费 | 日韩午夜片 | 毛片中文字幕 | 欧美一级淫片免费视频黄 | 久久高清免费视频 | 免费色片 | 亚洲视频免费看 |