主頁 > 後端開發 > SpringBoot進階教程(六十四)注解大全

SpringBoot進階教程(六十四)注解大全

2020-11-14 23:42:55 後端開發

在Spring1.x時代,還沒出現注解,需要大量xml組態檔并在內部撰寫大量bean標簽,Java5推出新特性annotation,為spring的更新奠定了基礎,從Spring 2.X開始spring將xml配置中的物件ioc程序轉化成了注解,Spring Boot之所以能夠輕松地實作應用的創建及與其他框架快速集成,最核心的原因就在于它極大地簡化了專案的配置,最大化地實作了“約定大于配置”的原則,但是注解種類之繁多,還能容易引起混淆,這才有了本文《SpringBoot進階教程(六十四)注解大全》,

要想對SpringBoot注解有個更全面更清晰的認識,就需要分個類,分別是Spring注解、Spring Web注解、Spring Boot注解、Spring Scheduling注解和注解集合,大致可以將注解分為5大類,其中前4類是為了便于理解,分別從4個類別中抽取了一些單獨介紹,而最后一個為注解集合,即可能會包含前面4種注解,

vSpring注解

在Spring Core注解中,主要討論Spring DI和Spring IOC中使用的Spring核心注釋,眾所周知,Spring DI和Spring IOC是Spring框架的核心概念,所以介紹 org.springframework.beans.factory.annotation org.springframework.context.annotation 包中的注解,這兩個包中注解有很多,就抽取其中的15個注解,

Spring Core Annotations:

  • @Autowired
  • @Qualifier
  • @Bean
  • @Required
  • @Value
  • @DependsOn
  • @Lazy
  • @Lookup
  • @Primary
  • @Scope
  • @Profile
  • @Import
  • @ImportResource
  • @PropertySource
  • @PropertySources

單單 org.springframework.context.annotation 這個包下面,注解就有這老些,所以很難列出所有注解舉例,只能抽一些常用的,文末會給出其它注解的作用和定義(盡量給全),

1.1 @Autowired

@Autowired是一種注解,可以對成員變數、方法和建構式進行標注,來完成自動裝配的作業,@Autowired標注可以放在成員變數上,也可以放在成員變數的set方法上,也可以放在任意方法上表示,自動執行當前方法,如果方法有引數,會在IOC容器中自動尋找同型別引數為其傳值,

這里必須明確:@Autowired是根據型別進行自動裝配的,如果需要按名稱進行裝配,則需要配合@Qualifier使用;

1.1.1 構造器注入

@RestController
public class UserController {
    private UserService userService;
 
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
}

1.1.2 setter方法注入

@RestController
public class UserController {
    private UserService userService;
 
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

1.1.3 field反射注入

@RestController
public class UserController {
    @Autowired
    private UserService userService;
}

1.2 @Qualifier

上面已經說到@Autowired按型別裝配Spring Bean,如果容器中有多個相同型別的bean,則框架將拋出NoUniqueBeanDefinitionException, 以提示有多個滿足條件的bean進行自動裝配,程式無法正確做出判斷使用哪一個,通過將@Qualifier注解與我們想要使用的特定Spring bean的名稱一起進行裝配,Spring框架就能從多個相同型別并滿足裝配要求的bean中找到我們想要的,

@Component("studentInfo")
public class StudentInfo implements UserInfo {
    public String userName() {
        return "student";
    }
}

@Component("teacherInfo")
public class TeacherInfo implements UserInfo {
    public String userName {
        return "teacher";
    }
}

@Component
public class UserService {
    @Autowired
    @Qualifier("studentInfo")
    private UserInfo userInfo;

    //todo 
}

1.3 @Bean

@Bean是一個方法級別上的注解,主要用在@Configuration注解的類里,也可以用在@Component注解的類里,添加的bean的id為方法名,

@Configuration
public class BeanConfig {

    @Bean
    public Person userInfo() {
        return new UserInfo("toutou", 18);
    }
}

這個配置就等同于之前在xml里的配置:

<bean id="userInfo" class="com.test.UserInfo">
    <property name="age" value="18"/>
    <property name="name" value="請叫我頭頭哥 http://toutou.cnblogs.com"/>
</bean>

1.4 @Required

@Required 注釋應用于 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時必須放在 XML 組態檔中,否則容器就會拋出一個 BeanInitializationException 例外,

@Required
void setUserName(String name) {
    this.name = name;
}
<bean class="com.test.UserInfo">
    <property name="name" value="請叫我頭頭哥 https://www.cnblogs.com/toutou/" />
</bean>

1.5 @Value

@Value將外部的值動態注入到Bean中,"注入外部的值"可以有很多種,它可以注入普通字串、注入java 系統變數、注入運算式結果、注入其他Bean屬性、將組態檔 *.properties 或 *. yml 里 配置的 屬性 注入、注入檔案資源和注入url資源等,

1.6 @DependsOn

Spring容器載入bean順序是不確定的,Spring框架也沒有約定特定載入順序邏輯規范,@DependsOn注解可以定義在類和方法上,比如說A組件要依賴于B組件,那就是B組件需要比A組件先注冊到IOC容器中,

public class FirstBean {

    @Autowired
    private SecondBean secondBean;
}

public class SecondBean {
    public SecondBean() {
        System.out.println("SecondBean init");
    }
}
@Configuration
public class BeanConfig {

    @Bean("firstBean")
    @DependsOn(value = {
        "secondBean"
    })
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean("secondBean")
    public SecondBean secondBean() {
        return new SecondBean();
    }
}

1.7 @Lazy

@Lazy注解用于標識bean是否需要延遲加載,Spring IoC容器一般都會在啟動的時候實體化所有單實體bean,如果想要Spring在啟動的時候延遲加載A,即在呼叫B的時候再去初始化,則可以使用@Lazy注解,

public class FirstBean {
    public void test() {
        System.out.println("FirstBean Class");
    }
}
public class SecondBean {
    public void test() {
        System.out.println("SecondBean Class");
    }
}
@Configuration
public class AppConfig {

    @Lazy(value = true)
    @Bean
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean
    public SecondBean secondBean() {
        return new SecondBean();
    }
}

1.8 @Lookup

@Lookup的注解是一個作用在方法上的注解,被其標注的方法會被重寫,然后根據其回傳值的型別,容器呼叫BeanFactory的getBean()方法來回傳一個bean,

1.9 @Primary

@Primary與@Qualifier類似,都是解決@Autowired時容器中有多個相同型別bean的問題,Primary可以理解為默認優先選擇,同時不可以同時設定多個,

@Component("studentInfo")
public class StudentInfo implements UserInfo {
    public String userName() {
        return "student";
    }
}

@Component("teacherInfo")
@Primary
public class TeacherInfo implements UserInfo {
    public String userName {
        return "teacher";
    }
}

@Component
public class UserService {
    @Autowired
    @Qualifier("studentInfo")
    private UserInfo userInfo;

    //todo
}

1.10 @Scope

@Scope注解是springIoc容器中的一個作用域,在 Spring IoC 容器中具有以下幾種作用域:基本作用域singleton(單例)(默認作用域)、prototype(多例),Web 作用域(reqeust、session、globalsession),自定義作用域

1.11 @Profile

@profile注解的作用是為了應對多環境開發,比如開發環境使用dev, 生產環境使用prod,就可以使用@Profile注解實作不同的開發環境使用不同的資料源,spring3.2之前 @Profile注解用在類上,spring3.2 之后 @Profile注解用在方法上

1.12 @Import

@Import用于注入指定的類,匯入組件id默認是組件的全類名,

@Configuration
public class ConfigA {

    @Bean
    public A a() {
        return new A();
    }
}

@Configuration
@Import(ConfigA.class)
public class ConfigB {

    @Bean
    public B b() {
        return new B();
    }
}

1.13 @ImportResource

@ImportResource注解用于匯入Spring的組態檔,讓組態檔里面的內容生效;(就是以前寫的springmvc.xml、applicationContext.xml)

@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}

1.14 @PropertySource

@PropertySource注解加載指定的組態檔

@Configuration
@PropertySource("classpath:config.properties")
public class ProperySourceDemo implements InitializingBean {

    @Autowired
    Environment env;

    @Override
    public void afterPropertiesSet() throws Exception {
        setDatabaseConfig();
    }

    private void setDatabaseConfig() {
        DataSourceConfig config = new DataSourceConfig();
        config.setDriver(env.getProperty("jdbc.driver"));
        config.setUrl(env.getProperty("jdbc.url"));
        config.setUsername(env.getProperty("jdbc.username"));
        config.setPassword(env.getProperty("jdbc.password"));
        System.out.println(config.toString());
    }
}

1.15 @PropertySources

@PropertySources顧名思義就是可以指定多個@PropertySource來匯入組態檔,

@PropertySources({
  @PropertySource("classpath:config.properties"),
  @PropertySource("classpath:db.properties")
 })
 public class AppConfig {
  //todo...
 }

vSpring Web注解

2.1 @RequestBody

主要用來接收前端傳遞給后端的json字串中的資料的,

@RestController
@RequestMapping("/api/v1")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user")
    public UserInfo createUser(@Valid @RequestBody UserInfo user) {
        return userService.save(user);
    }
}

2.2 @RequestMapping

@RequestMapping是一個用來處理請求地址映射的注解,可用于類或方法上,也就是通過它來指定控制器可以處理哪些URL請求,

@Controller
class UserController {
    @RequestMapping(value = "/user/index", method = RequestMethod.GET)
    String index() {
        return "index";
    }
}

2.3 @GetMapping

@GetMapping注釋將Http GET請求映射到特定的處理程式方法, 它是一個組合的注釋,@RequestMapping(method = RequestMethod.GET)的快捷方式,

    @GetMapping("/users")
    public List<user> getAllUser() {
        //...
    }
    @GetMapping("/users/{id}")
    public ResponseEntity<User> getUserById(@PathVariable(value = "https://www.cnblogs.com/toutou/archive/2020/11/14/id") Long userId)
            throws ResourceNotFoundException {
        user user = userRepository.findById(userId)
                .orElseThrow(() -> new ResourceNotFoundException("user not found for this id :: " + userId));
        return ResponseEntity.ok().body(user);
    }

2.4 @PathVariable

@PathVariable是spring3.0的一個新功能:接收請求路徑中占位符的值

2.5 @PostMapping

@PostMapping注釋將HTTP POST請求映射到特定的處理程式方法, 它是一個組合的注釋,@RequestMapping(method = RequestMethod.POST)的快捷方式,

@PostMapping("/user/add")
public User addUser(@Valid @RequestBody User user) {
 return userRepository.save(user);
}

其它擴展: @PutMapping、@DeleteMapping、@PatchMapping(這三種相對用的比較少,一筆帶過,)

2.6 @ControllerAdvice

增強型控制器,對于控制器的全域配置放在同一個位置,可以用于定義@ExceptionHandler、@InitBinder、@ModelAttribute,可處理全域例外處理、全域資料系結和全域資料預處理,

@ControllerAdvice(basePackages = {"com.toutou.controller"} )
public class GlobalControllerAdvice {
    @InitBinder
    public void dataBinding(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, true));
    }
    @ModelAttribute
    public void globalAttributes(Model model) {
        model.addAttribute("msg", "Hello World!");
    }
    @ExceptionHandler(FileNotFoundException.class)
    public ModelAndView myError(Exception exception) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", exception);
        mav.setViewName("error");
        return mav;
    }
}

2.7 @ExceptionHandler

@ExceptionHandler統一處理某一類例外,從而能夠減少代碼重復率和復雜度,

2.8 @InitBinder

@InitBinder只在@Controller中注解方法來為這個控制器注冊一個系結器初始化方法,方法只對本控制器有效,

2.9 @ModelAttribute

@ModelAttribute注解用于將方法的引數或方法的回傳值系結到指定的模型屬性上,并回傳給Web視圖,

2.10 @ResponseBody

@ResponseBody將controller里方法回傳的物件通過適當的轉換器轉換為Json寫入到response物件的body區.

2.11 @Controller

@Controller用于標記在一個類上,使用它標記的類就是一個SpringMvc Controller物件,分發處理器會掃描使用該注解的類的方法,并檢測該方法是否使用了@RequestMapping注解,

2.12 @RestController

@RestController在Spring中的作用等同于@Controller + @ResponseBody,

2.13 @RequestParam

@RequestParam將請求引數系結到你控制器的方法引數上(是springmvc中接收普通引數的注解)

2.14 @CrossOrigin

@CrossOrigin支持跨域,可用于Controller上,也可用于方法上,

@CrossOrigin(origins = "http://toutou.com", maxAge = 3600)
@RequestMapping("/index")
String index() {
    return "Hello World!";
}

vSpring Boot注解

3.1 @SpringBootApplication

@SpringBootApplication是Sprnig Boot專案的核心注解,目的是開啟自動配置,由于@Configuration,@EnableAutoConfiguration和@ComponentScan三個注解一般都是一起使用,于是spring boot提供了一個統一的注解@SpringBootApplication,即:@SpringBootApplication=@Configuration + @EnableAutoConfiguration + @ComponentScan,

@SpringBootApplication // 等于:@Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如上代碼,是不是簡潔了不少,

3.2 @EnableAutoConfiguration

可以根據classpath中的jar依賴,自動注冊bean,一般用于類或介面上,它嘗試根據您添加的jar依賴項自動配置Spring應用程式,自動載入應用程式所需的所有Bean——這依賴于Spring Boot在類路徑中的查找,

@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3.3 @ConditionalOnClass、@ConditionalOnMissingClass

3.4 @ConditionalOnBean、@ConditionalOnMissingBean

@ConditionalOnBean         //    當給定的在bean存在時,則實體化當前Bean
@ConditionalOnMissingBean  //    當給定的在bean不存在時,則實體化當前Bean
@ConditionalOnClass        //    當給定的類名在類路徑上存在,則實體化當前Bean
@ConditionalOnMissingClass //    當給定的類名在類路徑上不存在,則實體化當前Bean

3.5 @ConditionalOnProperty

@ConditionalOnProperty可以通過組態檔中的屬性值來判定configuration是否被注入,

3.6 @ConditionalOnResource

@ConditionalOnResource是注解在Configuration bean上,在其加載之前對指定資源進行校驗,是否存在,如果不存在,拋出例外;該注解支持傳入多個變數,

3.7 @ConditionalOnWebApplication、@ConditionalOnNotWebApplication

@ConditionalOnWebApplication主要的用處是: 當Spring為web服務時,才使注解的類生效;通常是配置類;@ConditionalOnNotWebApplication不是web應用,

3.8 @Conditional

@Conditional的作用是按照一定的條件進行判斷,滿足條件給容器注冊bean,

vSpring Scheduling注解

4.1 @Scheduled

@Scheduled可以作為一個觸發源添加到一個方法中,

@Scheduled(fixedDelay=1000)
public void doSomething() {
    //...
}

4.2 @EnableScheduling

@EnableScheduling 在配置類上使用,開啟計劃任務的支持(類上),

@Configuration
@EnableScheduling //通過@EnableScheduling注解開啟對計劃任務的支持
public class TaskScheduleConfig {
    //...
}

4.3 @Async

@Async標注的方法,稱之為異步方法;這些方法將在執行的時候,將會在獨立的執行緒中被執行,呼叫者無需等待它的完成,即可繼續其他的操作,有時候我們會呼叫一些特殊的任務,任務會比較耗時,重要的是,我們不管他回傳的后果,這時候我們就需要用這類的異步任務啦,

4.4 @EnableAsync

@EnableAsync注解啟用了Spring異步方法執行功能

@Schedules

@Schedules作用跟@Scheduled一樣,@Schedules內部包含多個@Scheduled注解,可以表示一個方法可以存在多個調度設定,

v注解集合

@ComponentScan:表示將該類自動發現掃描組件,個人理解相當于,如果掃描到有@Component、@Controller、@Service等這些注解的類,并注冊為Bean,可以自動收集所有的Spring組件,包括@Configuration類,我們經常使用@ComponentScan注解搜索beans,并結合@Autowired注解匯入,可以自動收集所有的Spring組件,包括@Configuration類,我們經常使用@ComponentScan注解搜索beans,并結合@Autowired注解匯入,如果沒有配置的話,Spring Boot會掃描啟動類所在包下以及子包下的使用了@Service,@Repository等注解的類,

@Repository:使用@Repository注解可以確保DAO或者repositories提供例外轉譯,這個注解修飾的DAO或者repositories類會被ComponetScan發現并配置,同時也不需要為它們提供XML配置項,

@Inject:等價于默認的@Autowired,只是沒有required屬性;

@Component:泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注,

@JsonBackReference:解決嵌套外鏈問題,

@JsonIgnore:作用是json序列化時將Java bean中的一些屬性忽略掉,序列化和反序列化都受影響,

@ConfigurationProperties:Spring Boot可使用注解的方式將自定義的properties檔案映射到物體bean中,比如config.properties檔案,

@ConditionalOnSingleCandidate:組合@Conditional注解,當指定的class在容器中只有一個Bean,或者同時有多個但為首選時才開啟配置,

@ConditionalOnCloudPlatform:組合 @Conditional 注解,當指定的云平臺激活時才開啟配置,

@ConditionalOnJndi:組合 @Conditional 注解,當指定的 JNDI 存在時才開啟配置,

@ConditionalOnJava:組合@Conditional 注解,當運行的 Java JVM 在指定的版本范圍時才開啟配置,

@ConditionalOnExpression:組合 @Conditional 注解,當 SpEL 運算式為 true 時才開啟配置,

@WiselyConfiguration: 組合注解可以替代@Configuration和@ComponentScan

@Transcational: 事務處理

@Target (ElementType.TYPE):元注解,用來指定注解修飾類的那個成員 -->指定攔截規則

@Cacheable: 資料快取

@ActiveProfiles: 用來宣告活動的 profile

@RunWith: 運行器

其他參考/學習資料:

  • Spring Boot Annotations
  • Spring Boot Annotations - HowToDoInJava
  • springboot注解
  • SpringBoot三大注解
  • Frequently Used Annotations in Spring Boot Applications
  • Spring MVC Annotations with Examples

v原始碼地址

https://github.com/toutouge/javademosecond/tree/master/hellospringboot


作  者:請叫我頭頭哥
出  處:http://www.cnblogs.com/toutou/
關于作者:專注于基礎平臺的專案開發,如有問題或建議,請多多賜教!
著作權宣告:本文著作權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文鏈接,
特此宣告:所有評論和私信都會在第一時間回復,也歡迎園子的大大們指正錯誤,共同進步,或者直接私信我
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角【推薦】一下,您的鼓勵是作者堅持原創和持續寫作的最大動力!

<style>#comment_body_3242240 { display: none }</style>

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/216180.html

標籤:其他

上一篇:Python 代碼除錯神器:PySnooper

下一篇:Python 代碼除錯神器:PySnooper

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more