1. 程式人生 > 其它 >#RMQ,動態開點線段樹#CF803G Periodic RMQ Problem

#RMQ,動態開點線段樹#CF803G Periodic RMQ Problem

使用註解開發

  • 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

  • 指定要掃描的包,這個包下的註解就會生效
<context:component-scan base-package="com.saxon.pojo"/>
<?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.saxon.pojo"/>
    
    <!--開啟註解的支援-->
    <context:annotation-config/>
</beans>
@Component  //等價於 <bean id="user" class="com.saxon.pojo.User"/>
@Data
public class User {
    private String name;
}

@Value

@Value 新增在欄位上或者set方法上,設定屬性的值

@Component  //等價於 <bean id="user" class="com.saxon.pojo.User"/>
@Data
public class User {
    //等價於<property name="name" value="kuangshen" />
    @Value("saxon")
    private String name;
}
@Component  //等價於 <bean id="user" class="com.saxon.pojo.User"/>
@Data
//id預設為pojo類的小寫
public class User {
    //等價於<property name="name" value="kuangshen" />
    @Value("saxon")
    private String name;
    public void setName(String name) {
        this.name = name;
    }
}

@Component 衍生的註解

@Component有幾個衍生註解,我們在web開發中,會按照mvc三層架構分層

這4個註解功能都是一樣的,都是代表某個類註冊到Spring容器中,自動裝配

@Scope作用域

@Scope(“singleton”)

新增到類上 設定為單例模式

小結

xml與註解:

  • xml更加萬能,適用於任何場合,維護簡單方便
  • 註解 不是自己類使用不了。維護相對複雜

xml與註解 最佳實踐:

  • xml用來管理bean
  • 註解只負責完成屬性的注入

我們在使用的過程中,只需要注意一個問題,必須讓註解生效就需要開啟註解的支援

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