How to attach initial values to attributes when initializing a class

Here I use the @value annotation to initialize and assign the properties of the class. It should be noted that in the spring project, the class must be registered in the ioc container, and when obtaining an instance of the class (that is, when creating an object), you cannot use manual new must be obtained from the container, that is, @Autowired or @Resource annotations` @value can be initialized in attributes or set methods, but set has a higher priority, because the principle is set injectionInsert picture description here
Insert picture description here

@Value and @PropertySource can be used in conjunction to obtain the value of the configuration file. In the enterprise project, put important values ​​in the configuration file for easy review, modification and verification.
Step 1: Create a new configuration file under src/main/resources User.properties, used to initialize and assign values ​​to User
Step 2: @PropertySource("classpath:User.properties") find the location of the configuration file
Step 3: @value("xxx") get the value in the configuration file

Specific reference to this blog: @Value and @PropertySource used in conjunction

Insert picture description here

Not much code on 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 + '\'' +
                '}';
    }
}

`

Guess you like

Origin blog.csdn.net/f_a_ker/article/details/109468243