gpt4 book ai didi

java - 如何在 Spring 使用 Autowiring 概念?

转载 作者:行者123 更新时间:2023-11-30 03:21:57 26 4
gpt4 key购买 nike

我是 Spring 框架的新手。我正在尝试研究 Autowired 概念,但我的输出不正确。我使用了下面的代码。我不知道我错在哪里。谁能帮我解决这个问题吗?

Employee.java:

package com.autowire;

public class employee {

private String name;
private String country;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

public void show()
{
System.out.println("hai my country is:"+country);
System.out.println("hai my name is"+name);

}

}

ma​​in.java:

package com.autowire;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;;


public class main {
public static void main(String args[]){
ApplicationContext context=new ClassPathXmlApplicationContext("config/applicationcontext.xml");

employee emp=(employee) context.getBean("b1");

emp.show();
}

}

applicationcontext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="b1" class="com.autowire.employee" autowire="byName">

</bean>

<bean id="name" class="com.autowire.checking">
<property name="id" value="12"></property>
<property name="name1" value="yes"></property>
</bean>

<bean id="id" class="com.autowire.checking">
<property name="id" value="12"></property>
<property name="name1" value="yes"></property>
</bean>

</beans>

检查.java

package com.autowire;

public class checking {
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name1;
private int id;



}

输出:哈喽,我的国家是:null嗨,我的名字为空

最佳答案

首先,以大写开头类名是一个很好的 java 实践,例如,您的 Employee 类应该以 E 开头,而不是 e >.

Employee.java

 package com.autowire;
public class Employee {

private String name;
private String country;

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

public String getCountry() {
return country;
}

public void setCountry(final String country) {
this.country = country;
}

public void show() {
System.out.println("hai my country is: " + country);
System.out.println("hai my name is: " + name);
}
}

其次,当您想要填充Employee类实例变量时,为什么要创建多个bean。创建如下所示的单个 bean,并将 namecountry 设置为 Employee 类的属性。

applicationcontext.xml

 <bean id="b1" class="com.autowire.Employee">
<property name="name" value= "A"/>
<property name="country" value= "India"/>
</bean>

接下来,在您的 main 类中避免不必要的导入并调用 b1 bean,如下所示:

Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationcontext.xml");
Employee emp = (Employee) context.getBean("b1");
emp.show();
}
}

关于java - 如何在 Spring 使用 Autowiring 概念?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31134494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com