gradle.properties的使用教程

一般我们都把全局属性都编写在一个工具类中,如果是有环境的切换的话,那么我们还会定义一个标志来进行相应的变换。对于项目而言,有时候需要配置某些敏感信息。比如密码,帐号等。而这些信息需要被很多类共同使用,所以必须有一个全局的配置。当需要把项目push到git上时,我们不希望别人看到我们项目的key,token等。我们可以将这些信息设置在gradle.properties中。
下面就讲解如何使用gradle.properties。
1,在gradle.properties文件中进行变量初始化。
如图:这里写图片描述
这里我们定义了5个属性。

KEY = 1234567890
TOKEN = 938372203
ACCOUNT = OK
PASSWORD = 123456
NAME = MyApp

2,在build.gradle(module app)中进行变量的重定义,即将配置内容转化成java能够使用的形式

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            buildConfigField "String","APPNAME","\"${NAME}\""
            resValue "string","app_key","${KEY}"
            resValue "string","app_token","${TOKEN}"
            resValue "string","account","${ACCOUNT}"
            resValue "string","password","${PASSWORD}"
        }
        debug{
            buildConfigField "String","APPNAME","\"${NAME}\""
            resValue "string","app_key","${KEY}"
            resValue "string","app_token","${TOKEN}"
            resValue "string","account","${ACCOUNT}"
            resValue "string","password","${PASSWORD}"
        }
    }

其中,buildConfigField定义的方式是(这里定义出来的属性主要是在java代码中使用)
buildConfigField 类型,变量名,值
resValue定义的方式是(这里定义出来的主要是在xml文件中使用)
resValue XML中的类型,变量名,值

3.在Java中使用。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        textView.setText(BuildConfig.APPNAME);
    }
}

定义了buildConfigField 类型,那么Android studio会自动的在BuildConfig文件下生成对应的全局属性参数
这里写图片描述
例如我们这里定义了之后生成的代码如下:

package com.gradletest;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.gradletest";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "wandoujia";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Fields from build type: debug
  public static final String APPNAME = "MyApp";
}

其中APPNAME就是我们自定义的全局属性参数。

4.在xml文件中使用。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gradletest.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:text="@string/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="@string/account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="@string/app_key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="@string/app_token"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

通过build.gradle中的配置,可以直接使用@string 访问、

上面就是gradle.properties的使用教程,通过gradle.properties的参数设置,项目开发中不同的服务器环境,我们也可以定义在这个文件下。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Data JPA 是一个用于简化数据库访问的框架,它基于 JPA (Java Persistence API) 规范,提供了一种更简单、更高效的方式来访问和操作数据库。 下面是使用 Spring Data JPA 的一般步骤: 1. 添加依赖:在你的项目中,添加 Spring Data JPA 的依赖。你可以在 Maven 或 Gradle 配置文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 配置数据库连接:在 `application.properties` 或 `application.yml` 文件中配置数据库连接信息,包括数据库 URL、用户名、密码等。 3. 创建实体类:创建与数据库表对应的实体类,并使用 JPA 注解来映射实体类与数据库表之间的关系。 ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // getters and setters } ``` 4. 创建 Repository 接口:创建一个继承自 `JpaRepository` 的接口,并通过方法命名规则或自定义查询方法来定义数据库操作。 ```java public interface UserRepository extends JpaRepository<User, Long> { List<User> findByAgeGreaterThan(Integer age); } ``` 5. 使用 Repository:在需要访问数据库的地方,注入 Repository 接口,并调用其中的方法进行数据库操作。 ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getUsersByAgeGreaterThan(Integer age) { return userRepository.findByAgeGreaterThan(age); } } ``` 这只是 Spring Data JPA 的基本用法,你还可以使用更高级的特性,如分页、排序、复杂查询等。希望这些信息对你有帮助!如果你有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值