Java 为什么我会出错?在spring框架中使用构造函数注入Account类属性时
当我运行代码时,它正在打印帐号的account类intead的地址。 请有人帮帮我 //配置类。我没有使用xml 包com.springcore.annotation.beanJava 为什么我会出错?在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 {
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是的,谢谢。