Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我会出错?在spring框架中使用构造函数注入Account类属性时_Java_Spring - Fatal编程技术网

Java 为什么我会出错?在spring框架中使用构造函数注入Account类属性时

Java 为什么我会出错?在spring框架中使用构造函数注入Account类属性时,java,spring,Java,Spring,当我运行代码时,它正在打印帐号的account类intead的地址。 请有人帮帮我 //配置类。我没有使用xml 包com.springcore.annotation.bean import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class EmpConfig {

当我运行代码时,它正在打印帐号的account类intead的地址。 请有人帮帮我

//配置类。我没有使用xml

包com.springcore.annotation.bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmpConfig {
    
    @Bean
    public Employee getEmployee() {
        Employee e1=new  Employee(101, "Diju Singh", "LabTechnician", 15000, setAccount());
        return e1;
    }
    
    @Bean
    public Account  setAccount() {
        Account a1= new Account(1023450150);
        return a1;
    }

}


========================================================
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class EmpTest {

    public static void main(String[] args) {
        ApplicationContext context=new AnnotationConfigApplicationContext(EmpConfig.class);
        Employee emp1= context.getBean("getEmployee", Employee.class);
        System.out.println(emp1);
        emp1.doWork();
        
    }

}
=======================================================================================
public class Account {
    
    private long accountNumber;

    public long getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(long accountNumber) {
        this.accountNumber = accountNumber;
    }

    public Account(long accountNumber) {
        super();
        this.accountNumber = accountNumber;
    }
    
}


============================================================
//测试班 包com.springcore.annotation.bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmpConfig {
    
    @Bean
    public Employee getEmployee() {
        Employee e1=new  Employee(101, "Diju Singh", "LabTechnician", 15000, setAccount());
        return e1;
    }
    
    @Bean
    public Account  setAccount() {
        Account a1= new Account(1023450150);
        return a1;
    }

}


========================================================
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class EmpTest {

    public static void main(String[] args) {
        ApplicationContext context=new AnnotationConfigApplicationContext(EmpConfig.class);
        Employee emp1= context.getBean("getEmployee", Employee.class);
        System.out.println(emp1);
        emp1.doWork();
        
    }

}
=======================================================================================
public class Account {
    
    private long accountNumber;

    public long getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(long accountNumber) {
        this.accountNumber = accountNumber;
    }

    public Account(long accountNumber) {
        super();
        this.accountNumber = accountNumber;
    }
    
}


============================================================
//帐户bean类 包com.springcore.annotation.bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmpConfig {
    
    @Bean
    public Employee getEmployee() {
        Employee e1=new  Employee(101, "Diju Singh", "LabTechnician", 15000, setAccount());
        return e1;
    }
    
    @Bean
    public Account  setAccount() {
        Account a1= new Account(1023450150);
        return a1;
    }

}


========================================================
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class EmpTest {

    public static void main(String[] args) {
        ApplicationContext context=new AnnotationConfigApplicationContext(EmpConfig.class);
        Employee emp1= context.getBean("getEmployee", Employee.class);
        System.out.println(emp1);
        emp1.doWork();
        
    }

}
=======================================================================================
public class Account {
    
    private long accountNumber;

    public long getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(long accountNumber) {
        this.accountNumber = accountNumber;
    }

    public Account(long accountNumber) {
        super();
        this.accountNumber = accountNumber;
    }
    
}


============================================================
//雇员bean类

package com.springcore.annotation.bean;

public class Employee {
    private int empId;
    private String name;
    private String department;
    private double  salary;
    private Account  account;
    
    public Employee(int empId, String name, String department, double salary, Account account) {
        super();
        this.empId = empId;
        this.name = name;
        this.department = department;
        this.salary = salary;
        this.account = account;
    }
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    
    @Override
    public String toString() {
        return "Employee [empId=" + empId + ", name=" + name + ", department=" + department + ", salary=" + salary
                + ", account=" + account + "]";
    }
    
    public void doWork() {
        System.out.println("Employee is working ");
    }
    
}

    

在编写了toString()方法之后,它工作得很好。在写了toString()方法之后,我错过了它,它工作得很好。我错过了

您将需要
toString()
方法,用于
帐户
类,类似于
员工
类。在
toString()
方法的
Account
类中,如果您想打印,可以使用
accountNumber
。OHHHH是的,谢谢。您将需要
toString()
方法的
Account
类类似于
Employee
类。在
Account
类的
toString()
方法中,如果要打印,可以使用
accountNumber
。ohhh是的,谢谢。