クラスを初期化するときに属性に初期値を添付する方法

ここでは、@ valueアノテーションを使用して、クラスのプロパティを初期化して割り当てます。Springプロジェクトでは、クラスをiocコンテナに登録する必要があり、クラスのインスタンスを取得するとき(つまり、オブジェクトの作成)、手動で使用することはできませんnewはコンテナから取得する必要があります。つまり、@ Autowiredまたは@Resourceアノテーション ` @valueは属性またはsetメソッドで初期化できますが、原則が設定されているため、setの方が優先されます注入ここに画像の説明を挿入
ここに画像の説明を挿入

@Valueと@PropertySourceを組み合わせて使用​​して、構成ファイルの値を取得できます。エンタープライズプロジェクトでは、簡単に確認、変更、検証できるように、構成ファイルに重要な値を入力します。
ステップ1:で新しい構成ファイルを作成しますsrc / main / resources User.properties、初期化してユーザーに値を割り当てるために使用
ステップ2:@PropertySource( "classpath:User.properties")構成ファイルの場所を見つける
ステップ3:@value( "xxx")構成ファイルの値を取得します

このブログへの具体的な参照:@Valueと@PropertySourceを組み合わせて使用

ここに画像の説明を挿入

bbのコードはあまりありません

package com.csdn.demo_ds.属性初始化;

import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLOutput;

/**
 * @author Administrator
 * @ClassName test
 * @Description
 * @date 2020-11-03 11:31
 */
@RestController
public class test {
    
    

    @Autowired
    A a;
    @Autowired
    User user;
    @GetMapping("first")
    public String test(){
    
    
        return a.toString();
    }
    @GetMapping("num")
    public String num(){
    
    
        return user.toString();
    }


}
@Component
class  A{
    
    
    @Value("nihao")
    private String name;
    private int age;

    public String getName() {
    
    
        return name;
    }
    @Value("xiaoming")
    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public A(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    public A() {
    
    
    }

    @Override
    public String toString() {
    
    
        return "A{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

user类代码

package com.csdn.demo_ds.属性初始化;

/**
 * @author Administrator
 * @ClassName user
 * @Description
 * @date 2020-11-03 13:57
 */


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author csy
 * 使用@PropertySource引入外部配置文件
 * @Component或@Configuration将该类交由IOC容器保管(这里有一个很奇怪的地方,明明使用了@Bean在配置类中已经注入了这个Bean,但是如果
 * 这里只使用了@PropertySource依然无法对User赋值,所以这里需要再加上一个@Component,很奇怪)
 */
@PropertySource("classpath:User.properties")
@Component
public class User {
    
    
    @Value("${user.id}")
    private Integer id;
    @Value("${user.userName}")
    private String userName;
    @Value("${user.password}")
    private String password;
    @Value("${user.tel}")
    private String tel;
    @Value("用户数据")
    private String defaultMessage;

    public void init() {
    
    
        System.out.println("User创建后调用初始化方法..........");
    }

    public void destory() {
    
    
        System.out.println("User销毁后调用销毁方法....通过@Bean的destoryMethod指定销毁方法......");
    }

    public User() {
    
    
        System.out.println("User创建完成...通过@Bean的initMethod调用初始化方法............");
    }

    public User(Integer id, String userName, String password, String tel, String defaultMessage) {
    
    
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.tel = tel;
        this.defaultMessage = defaultMessage;
    }

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getUserName() {
    
    
        return userName;
    }

    public void setUserName(String userName) {
    
    
        this.userName = userName;
    }

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }

    public String getTel() {
    
    
        return tel;
    }

    public void setTel(String tel) {
    
    
        this.tel = tel;
    }

    public String getDefaultMessage() {
    
    
        return defaultMessage;
    }

    public void setDefaultMessage(String defaultMessage) {
    
    
        this.defaultMessage = defaultMessage;
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", tel='" + tel + '\'' +
                ", defaultMessage='" + defaultMessage + '\'' +
                '}';
    }
}

`

おすすめ

転載: blog.csdn.net/f_a_ker/article/details/109468243