자바 속성 프로파일 관리 도구 프레임 워크, 순서가 피할 쓰기 속성

재산

속성은 속성 프레임 워크 자바 구현입니다.

특징

  • 정상적으로 읽고 특성 파일을 업데이트

  • 속성 파일 속성을 작성 후 순서가 아니다

  • 유연한 정의 인코딩 정보

  • 운영 특성 파일의 사용 OO 방법

  • 이는 다단계 객체 참조를 지원

로그인 변경

변경 로그

빠른 시작

따라 환경

메이븐 3.x를

JDK 1.7+

Maven은 소개가 의존

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>property</artifactId>
    <version>0.0.4</version>
</dependency>

케이스를 얻기

읽기 속성

PropertyBs.getInstance("read.properties").get("hello");

read.properties파일 경로, hello기존 속성 값 이름.

읽기 속성은 기본 값을 지정합니다

final String value = PropertyBs.getInstance("read.properties")
                .getOrDefault("hello2", "default");

read.properties파일 경로, hello2속성 값 이름이 존재하지 않는 default속성이 존재하지 않는 경우 기본 값이 반환됩니다.

설정 등록

PropertyBs.getInstance("writeAndFlush.properties").setAndFlush("hello", "world-set");

writeAndFlush.properties파일 경로, hello속성 정보를 설정해야합니다.

방법 부트 스트랩 클래스 개요

아니오. 방법 설명
1 의 getInstance (propertyPath) 가이드 클래스 인스턴스의 속성에 지정된 파일 경로를 가져옵니다
캐릭터 (캐릭터) 파일 인코딩을 지정, 기본 UTF-8
수 (키) 키에 해당하는 속성 값을 얻기
4 getOrDefault (키, DEFAULTVALUE) 키에 해당하는 속성 값을 얻기에는 반환 DEFAULTVALUE 없다
5 세트 (키 값) 설정 값 (메모리)
6 제거 (키) 제거 값 (메모리)
(7) 플러시() 새로 고침 메모리 디스크는 현재 파일에 대한 변경
9 플러시 (경로) 새로 고침 메모리 디스크는 지정된 파일로 변경
(10) 세트 (지도) 메모리 설정지도 정보
(11) 세트 (콩) 메모리에 빈 객체 정보 설정
(12) asMap () 반환지도에 반환으로, 메모리에 속성 정보
(13) asBean (콩) 메모리 객체 속성 정보 빈을 돌려줍니다

사물

간략한 소개

우리는 OO 생각과 일치으로 개체를 조작 할 수 속성을 운영하고 싶습니다.

설정

User user = new User();
user.setName("hello");
user.setHobby("hobby");

final long time = 1574147668411L;
user.setBirthday(new Date(time));

PropertyBs propertyBs = PropertyBs.getInstance("setBean.properties")
        .set(user);

Assert.assertEquals("hobby", propertyBs.get("myHobby"));
Assert.assertEquals("1574147668411", propertyBs.get("birthday"));

읽기 값

PropertyBs propertyBs = PropertyBs.getInstance("setBean.properties"
        .set("myHobby", "play")
        .set("birthday", "1574147668411");
User user = new User();
propertyBs.asBean(user);
Assert.assertEquals("play", user.getHobby());
Assert.assertEquals(1574147668411L, user.getBirthday().getTime());

개체 정의

  • User.java
public class User {

    private String name;

    @PropertyField("myHobby")
    private String hobby;

    @PropertyField(converter = DateValueConverter.class)
    private Date birthday;

}

@PropertyField 코멘트

아니오. 재산 기본 값 설명
1 현재 필드 이름 속성 이름 속성에 해당
변환기 기본 변환 달성 DefaultValueConverter 현재 변환 처리는 필드 속성

사용자 정의 변환 클래스

  • DateValueConverter.java

이것은 우리가 날짜 유형, 자신의 구현에 초점을 맞춘 과정의 유형입니다.

다음을 달성하기 :

public class DateValueConverter implements IValueConverter {

    @Override
    public Object fieldValue(String value, IFieldValueContext context) {
        return new Date(Long.parseLong(value));
    }

    @Override
    public String propertyValue(Object value, IPropertyValueContext context) {
        Date date = (Date)value;
        return date.getTime()+"";
    }

}

세트

설명

때때로 속성 설정 또는 여기 주어지는 배열 달성하기가 비교적 간단 할 수있다.

필드 값이 속성 값으로 직접 콤마로 분리된다.

테스트 케이스

UserArrayCollection userArrayCollection = buildUser();
PropertyBs propertyBs = PropertyBs.getInstance("setBeanArrayCollection.properties")
        .set(userArrayCollection);
Assert.assertEquals("array,collection", propertyBs.get("alias"));
Assert.assertEquals("array,collection", propertyBs.get("hobbies"));

개체 정의

  • UserArrayCollection.java
public class UserArrayCollection {

    private List<String> alias;

    private String[] hobbies;
}

만 너무 복잡하고 싶지 않아, 문자열 형식을 지원합니다.

후자는 지원의 여러 유형을 추가하는 것을 고려합니다.

멀티 레벨 객체

설명

때로는 이러한 대상체 들어 B와 같은 객체에서 다른 객체를 참조.

키 ABC 더 관습 사용과 일치, 이런 식으로 속성으로 여기에 사용.

테스트 케이스

설정

Book book = new Book();
book.name("《海底两万里》").price("12.34");
UserEntry user = new UserEntry();
user.name("海伦").book(book).age("10");
PropertyBs propertyBs = PropertyBs.getInstance("setBeanEntry.properties")
        .set(user);
Assert.assertEquals("海伦", propertyBs.get("name"));
Assert.assertEquals("10", propertyBs.get("age"));
Assert.assertEquals("《海底两万里》", propertyBs.get("book.name"));
Assert.assertEquals("12.34", propertyBs.get("book.price"));

읽기

Map<String, String> map = new HashMap<>();
map.put("name", "海伦");
map.put("age", "10");
map.put("book.name", "《海底两万里》");
map.put("book.price", "12.34");
UserEntry userEntry = new UserEntry();
PropertyBs.getInstance("setBeanEntry.properties")
        .set(map)
        .asBean(userEntry);
Assert.assertEquals("UserEntry{name='海伦', age=10, book=Book{name='《海底两万里》', price=12.34}}",
        userEntry.toString());

개체 정의

  • UserEntry.java
public class UserEntry {

    private String name;

    private String age;

    @PropertyEntry
    private Book book;

}
  • Book.java
public class Book {

    private String name;

    private String price;

}

@PropertyEntry 설명

@PropertyEntry 이 필드를 주석 다중 수준 개체를 식별하는 데 사용됩니다 여부를 나타냅니다.

이 주석은 하나만 숙박 시설입니다 value(), 현재 필드는 별명을 지정하는 데 사용할 수 있으며, @PropertyField별명은 비슷합니다.

후속 특성을

  • 더 많은 내장 타입 변환을 달성하기 위해

추천

출처www.cnblogs.com/houbbBlogs/p/11901764.html