2020年11月30日 星期一

[SpringBoot 1.5] 的Profile-配置多環境

1 多Profile文件

我們在主配置文件編寫的時候, 文件名可以是applpication-{profile}.properties/yml

默認使用application.properties的配置;


2 yml 支持多文檔塊方式

注意~這裡如果使用yml, 那application.properties的部分就要先註掉, 相關properties結尾也是

然後, 貼下以下這些程式碼, 試著在active: 的後面加上記得要空格 dev或是prod

另外---做為區隔模塊的部分, 必須要靠最左邊才有用

server:
port: 8084
spring:
profiles:
active:
---
server:
port: 8085
spring:
profiles: dev
---
server:
port: 8086
spring:
profiles: prod


3激活指定profile


在配置文件中指定spring.profiles.acrive=dev

步驟:

首先在resouse中產生一個新的file檔案 application-dev.properties檔案

內容為 server.port=8082


另外產生一個新的file檔案application-prod.properties檔案

內容為server.port=80


然後我們在原本的application.properties中加入以下這段

server.port=8081

spring.profiles.active=dev

這時候我們運行, 可以發現




而當我們改成

spring.profiles.active=prod

結果為




這樣我們就學會怎麼使用了

另外~也可以

從Configuration中的Environment區域去塞參數

如下




2020年11月23日 星期一

[SpringBoot 1.5] 配置 @PropertySource、@ImportResource、@Bean

 我們前面看過@ConfigurationProperties與application.properties的合用方式

那如果我們有一些配置文件想要單獨提取出來使用

可以另外產生一個properties檔案專為其使用

以下我們先產生一個person.properties檔案如下

person.lastname=李四
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

然後我們在原本的類別上加入@PropertySource

@PropertySource:加載指定配置文件

@PropertySource(value={"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

//@Value("${person.lastname}")
//@Email
private String lastname;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;

這裡不同的是要給value, 裡面放classpath加指定

@ImportResource: 導入Spring的配置文件, 讓配置文件裡面的內容生效

由於Spring Boot裡面沒有Spring的配置文件, 我們自己編寫的配置文件, 也不能自動識別;

想讓Spring的配置文件生效, 加載進來; @ImportResource標註在一個配置類上

首先先造一個類為 service.HelloService

public class HelloService {
}

接下來產生一個beans.xml作管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>

然後在測試類中加入以下

@Autowired
ApplicationContext ioc;

@Test
public void testHelloService(){
boolean b= ioc.containsBean("helloService");
System.out.println(b);
}

我們會發現結果印出來為false

這是因為剛才提到的Spring Boot 裡面沒有Spring配置文件

如果需要使用到

我們要在SpringBootApplication的類上面加入以下這段

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {

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

}

我們會發現, 這樣run test就能印出true了

然而

SpringBoot並不推薦使用beans.xml方式作管理

而是使用配置類作管理

@Configuration:指明當前類別是一個配置類; 就是來替代之前的Spring配置文件

@Bean: 將方法的返回值添加到容器中; 容器中這個組件組件默認的id就是方法名

接下來我們產生一個config.MyAppConfig檔案如以下

@Configuration
public class MyAppConfig {

@Bean
public HelloService helloService(){
System.out.println("配置類@Bean給容器中添加組建了...");
return new HelloService();
}
}

然後並且把剛剛在SpringBootApplication類別上面的註解先註解掉

//@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {

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

}

再來run一下我們的測試

@Test
public void testHelloService(){
boolean b= ioc.containsBean("helloService");
System.out.println(b);
}

這裡要注意"helloService"是對到helloService()方法

如果改成helloService02(), 那要用"helloService02"才找得到



2020年11月20日 星期五

[SpringBoot 1.5] @ConfigurationProperties 與 @Value 使用介紹與主要差異

 前面一篇我們已經看過yaml與@ConfigurationProperties的合用

現在我們來看看@Value的使用

在appplication.properties為如下的情況

person.lastname=張三
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
我們只要在屬性上加上
@Value("${person.lastname}")
這樣就能夠取到值了
如下圖

但是要特別注意, 如果是複雜的類型封裝是不支持的
也就是下面的Map是無法用value注入
而另外一方面@ConfigurationProperties還有一個特殊功能-資料驗證
要做到這個要再加兩件事
1在類別上加上@Validated
2在屬性上加入要驗證的如@Email
另外阿~有可能@Email會報紅字
就需要在dependencies中加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
我加了之後再重新下載maven資源, 發現他還是報錯
後來我重開之後, 他pom檔案沒有紅字, 試一下就能用
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

//@Value("${person.lastname}")
@Email
private String lastname;
我們的lastname為我們的驗證項目
但是其中的字串為"張三", 並不是email格式
於是run之後就會有以下這一段







這樣就具有驗證效果了

下面我們示範在Controller中@Value的使用方式
我們先造一個HelloController
另一個私有的變數並冠以@Value

@RestController
public class HelloController {
@Value("${person.lastname}")
private String name;

@RequestMapping("/sayHello")
public String sayHello(){
return "Hello "+name;
}
}

然後我們開啟服務, 訪問一下,就能得到下圖
那@ConfigurationProperties 與 @Value 使用介紹與主要差異
為以下下圖


如果說, 我們只是在某個業務邏輯中需要獲取一下配置文件中的某項值, 使用@Value;
如果說, 我們專門編寫了一個javaBean來和配置文件進行映射, 我們就值接使用@ConfigurationProperties

以上內容請參考 尚硅谷IT培訓學校
https://www.youtube.com/watch?v=eyqiiWbBlMs&list=PLmOn9nNkQxJEFsK2HVO9-WA55Z7LZ2N0S&index=13




















[PLSQL] 測試 現有的方法

 declare 

  p_service d_services.id%type;

begin

  -- Test statements here

  p_service :=方法包.get_latest_instance(p_service=>700562803);

   dbms_output.put_line(p_service);

end;


特別要注意的是=的前面要加上兩個點

dbms_output.put_line(); 方法也蠻常用



2020年11月18日 星期三

[Java] Collapsible "if" statements should be merged (java:S1066)

 Merging collapsible if statements increases the code's readability.

Noncompliant Code Example

if (file != null) {
  if (file.isFile() || file.isDirectory()) {
    /* ... */
  }
}

Compliant Solution

if (file != null && isFileOrDirectory(file)) {
  /* ... */
}

private static boolean isFileOrDirectory(File file) {
  return file.isFile() || file.isDirectory();
}
以上內容的意思是~
當if條件句中, 又只有一個if條件句
這兩個if條件句就可以合併成一個
中間用&&隔開~真是有時候並不會發現的小毛病呢~

2020年11月16日 星期一

[SpingBoot 1.5] 配置文件YAML 與 @ConfigurationProperties 搭配使用

 首先造一個 Person 類別

加入@Component使其能夠被注入 

並加入@ConfigurationProperties , 告知對應prefix的文字為person


類別屬性為以下, 並加入setter, getter和toString方法

private String lastname;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;

並在application.yaml中加入以下這段

person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12

為了要使用@ConfigurationProperties, 我們需要在pom.xml檔案中的
depenedencies中加入以下這段

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

然後將整個spring重run 就可以使用
為了方便測試, 我們在test中進行
位置為以下


內容則為如下

@SpringBootTest
class SpringBoot02ConfigApplicationTests {

@Autowired
Person person;

@Test
void contextLoads() {
System.out.println(person);
}

}

我們run的結果則為
Person{lastname='hello', age=18, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, 
maps={k1=v1, k2=12}, lists=[lisi, zhaoliu], dog=Dog{name='小狗', age=12}}
這樣就完成了~

另外如果不在yaml檔案中賦值, 也可以在application.properties檔案中賦值
長相為如下
person.lastname=張三
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

相關內容:
https://blog.csdn.net/yingxiake/article/details/51263071





2020年11月11日 星期三

[SpingBoot 1.5] 配置文件及 YAML Cannot resolve configuration property 'server' 錯誤

SpiringBoot 配置文件
除了可以使用application.properties外 
還可以使用application.yml 
而以前的配置文件大多是使用xxxx.xml文件 
YAML: 以數據為中心, 比json, xml更適合用來做配置文件 
舉例: 

YAML:
server:
   port: 8082
XML:
<server>
  <port>8081</port>
</server>
在這邊寫yaml檔案要小心
在分號: 後面必須要有空格
不然會發生 Cannot resolve configuration property 'server'
也就是他格式錯了, 他就沒辦法辨認

yaml檔案放的位置如下
基本上放在resources中就可以用(application.yml)



[leetcode] [KMP] KMP

ABCDABD... ABCDABF... 簡單的說, 傳統解兩字串匹配部分 可能會來個雙迴圈, 哀個比對, 當不匹配的時候, 會將下方列再後移1位 然後不匹配再後移 然而 如果像上放已經有4個屬於匹配的字串, 她就應該直接往後移四位來匹配, 而不是只移動1位 隱藏的思維是, 當...