ThinkPad x1 Carbon Gen 5 を中古で買って、Manjaro を入れた
最初は VM で試して、うまくいったから実際にインストールしてみたら、うまくいかなかった。
でも、結局、インストールできた。よかった。
Manjaro の Xfce を入れた。
少しの間、メインで使ってみる。
今、Windows をメインで使っているけど、実際に触ってるのは WSL2 の Ubuntu だからやることは変わらないかも。
Manjaro は Arch Linux をもとにして作られているらしく、カスタマイズとかするの楽しそう。 Neovim と Manjaro で時間が溶けるやつかもしれない。
最近は、Neovim と Lua にハマっていて、 telescope.nvim とか LSP クライアントとかいじっている。ほんのちょっと前までやっていたのは、 Neovim の組み込みの LSP クライアントの写経をしていた。サーバー側を実装するにはその言語のパーサーとか書かないといけないけど、クライアントならできそうだったからやってみた。
結構、得るものが多い。というか楽しい。自分で書いたものが実際に動くのが楽しい
だらだらと文章を書いてみた。たまにはいいかもこれ。
Scrapbox を1ヶ月使ってみた
難しいというか、正しく使えていない気がする...
中途半端なメモが散乱してしまって、過去のページを見ていない気がする...
知識の関連付けができていなさそう
Scrapbox の使い方とかの本を読むべきかも
【Hibernate】NativeQuery.setProperties でパラメータのセットにPOJOを使う
hibernate のSQL実行時のパラメータセットにPOJOが使えたのが便利そうだからメモっておく
org.hibernate.query.NativeQuery.setProperties()
を使うことでパラメータのセットにPOJOが使える
// Dao.java @Override public void insert(People people) { Session session = sessionFactory.getCurrentSession(); NativeQuery query = session.createNativeQuery("insert into people (first_name, last_name, division_id) values (:firstName, :lastName, :divisionId)"); query.setProperties(people); query.executeUpdate(); }
// People.java @Data public class People { private String firstName; private String lastName; private Integer divisionId; }
//Service.java @Override @Transactional public void insertUser() { People people = new People(); people.setFirstName("taro"); people.setLastName("yamada"); peopleDao.insert(people); }
:フィールド名
としたら、自動でマッピングしてくれた
NULLが許可されているカラムに対応するフィールドがnull
の場合、null
をINSERTしてくれた
+-----------+------------+-----------+-------------+ | person_id | first_name | last_name | division_id | +-----------+------------+-----------+-------------+ | 1 | first | last | <null> | +-----------+------------+-----------+-------------+
参考文献
- [Query (Hibernate API Documentation) https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#setProperties(java.lang.Object)]
- [Java Code Examples org.hibernate.Query.setProperties https://www.programcreek.com/java-api-examples/?class=org.hibernate.Query&method=setProperties]
【Java】Spring Bootで Java Config を使用し、Propertiesファイルを読み込む
SpringBootを使って、Propertiesファイルを読みたかったから、やってみた
以下の2つのJavaクラスを作った
sample/config/Config.java
package sample.config; import java.io.IOException; import java.util.Properties; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @Configuration @EnableAutoConfiguration public class Config { @Bean public Properties sqlProperties() throws IOException { PropertiesFactoryBean propFactoryBean = new PropertiesFactoryBean(); propFactoryBean.setLocation(new ClassPathResource("sample.properties")); // これ大事! propFactoryBean.afterPropertiesSet(); return propFactoryBean.getObject(); } }
Properties
を返すBeanを定義Properties
を生成するためのPropertiesFactoryBean
を用意location
にClassPathResource
のインスタンスをセットsrc/main/resources/sample.properties
だとしたら、new ClassPathResource('sample.properties')
とする- SpringBootで
src/main/resources
はクラスパス内のため
PropertiesFactoryBean.afterPropertiesSet()
がないとうまく値を取れなかったため、必ず実行すること- 何をしているのかはよくわからない
PropertiesFactoryBean.getObject()
でPropertiesのインスタンスを返す
sample/App.java
package sample; import java.util.Properties; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { private static final Logger log = LoggerFactory.getLogger(App.class); @Resource private Properties sqlProperties; public static void main(String[] args) { log.info("Hello World!"); try(ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args)) { App app = ctx.getBean(App.class); app.run(args); } catch (Exception e) { e.printStackTrace(); } } public void run(String... args) throws Exception { log.info(sqlProperties.getProperty("samplevalue")); } }
- DIしたい
Properties
型の変数に@Resource
をつける@Resource
はBean名でDIされるらしい- 今回の場合、
sqlProperties
- 今回の場合、
getProperty()
で取得
src/main/resources/sample.properties
samplevalue=hello world
実行
20:30:49.252 [main] INFO sample.App - Hello World! . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-07-22 20:30:49.827 INFO 42668 --- [ main] sample.App : Starting App on Plus-OP021 with PID 42668 (C:\pleiades\workspace\spring-boot-properties-load\target\classes started by t-ohsaka in C:\pleiades\workspace\spring-boot-properties-load) 2019-07-22 20:30:49.828 INFO 42668 --- [ main] sample.App : No active profile set, falling back to default profiles: default 2019-07-22 20:30:50.507 INFO 42668 --- [ main] sample.App : Started App in 1.052 seconds (JVM running for 1.604) 2019-07-22 20:30:50.510 INFO 42668 --- [ main] sample.App : hello world!
わーできた!!簡単!!
今回のプロジェクトはここ に置いた
エラーが出たときのメモ
- Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'java.util.Properties' available: expected single matching bean but found 2: sqlProperties,systemProperties
- SpringのDIは型で一致したものをDIするため、同じ型のものがあると、どっちかわからない
@Resource
で変数名を@Bean
をつけたメソッド名と同じにする
- Bean named 'sqlProperties' is expected to be of type 'java.util.Properties' but was actually of type 'org.springframework.beans.factory.support.NullBean'
- 正しく
@Bean
で返せていなかった PropertiesFactoryBean.afterPropertiesSet()
を実行する- SpringのDIコンテナはbeanを生成しプロパティがセットされた後にafterPropertiesSet()メソッドを呼び出します
- 5.DIされたときの動作の制御(カスタマイズ)について - soracane
- JavaDocの和訳
すべてのBeanプロパティを設定し、BeanFactoryAware、ApplicationContextAwareなどを満たした後で、格納されているBeanFactoryによって呼び出されます。
このメソッドを使用すると、Beanインスタンスは、すべてのBeanプロパティが設定されたときに、全体の構成の検証と最終的な初期化を実行できます。
- んーわからん
- 正しく
参考文献
【Java】lombokで自動生成されるsetter(getter)を上書きする
例えば、特定のプロパティのみ、自分で書いたsetterを使いたいときとか
やることは
- ただ単に
setXxx()
を作ればいいだけ
だった
以下、サンプルコード
サンプル
People.java
以下のようなPeople
クラスを作成する
package sample; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class People { private String name; private int age; // @Dataで自動生成されるsetAge()を上書きする public void setAge(int age) { this.age = age + 100; } }
@Data
: すべてのプロパティに対してsetXxx()
とgetXxx()
を生成@NoArgsConstructor
: 引数なしのコンストラクタを生成@AllArgsConstructor
: 全プロパティを引数に取るコンストラクタを生成
App.java
package sample; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); People people = new People("Taro", 11); // 自分で定義したsetAge()が呼ばれる people.setAge(20); System.out.println(people); // People(name=Taro, age=120) } }
おぉ、しっかりと100足されている!
【GitHub】PullやPushでパスワードを聞かれないように設定する
~/.ssh/id_rsa(秘密鍵)と~/.ssh/id_rsa.pub(公開鍵)を作る
> ssh-keygen -t rsa
~/.ssh/id_rsa
と~/.ssh/id_rsa.pub
が生成される
GitHubに登録する
Settings > SSH and GPG kers > SSH keys > New SSH key
に~/.ssh/id_rsa.pub
の内容を貼り付ける
- 接続の確認
> ssh -T git@github.com Hi tamago324! You've successfully authenticated, but GitHub does not provide shell access.
ローカルリポジトリの設定を変更する
GitHubのリポジトリのClone or download
のUse SSH
のテキストをコピーし、以下のコマンドを実行する
> git remote set-url origin xxxxxx
秘密鍵のパスを指定する( ~/.ssh/config
の作成)
秘密鍵を以下のように生成した場合
> ssh-keygen -t rsa -f ~/.ssh/id_rsa_github
以下のような~/.ssh/config
を作成する(全く同じ記述で作成する)
Host github github.com HostName github.com IdentityFile ~/.ssh/id_rsa_github User git
接続の確認
> ssh -T git@github.com Hi tamago324! You've successfully authenticated, but GitHub does not provide shell access.