如何在 spring 中使用自动装配概念?
how to work with autowire concept in spring?
我是 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);
}
}
main.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>
checking.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;
}
输出:
你好我的国家 is:null
你好,我的名字是空的
您可以在像这样的 bean 的 属性 名称之前使用 @Autowired
注释 -
@Autowired
@Qualifier("name")
private String name;
@Autowired
@Qualifier("country")
private String Country;
如果您像这样使用 autowire
,则不必使用 getter 和 setter 方法。
Razib 说的不错,但你要注入的class 是不是也必须加上注解(@Component、@service 等)?
您首先需要在 Employee class 中添加检查作为 属性。
所以你应该这样做,
public class Employee {
Checking checking;
public Employee(Checking checking) {
super();
this.checking = checking;
}
public Checking getChecking() {
return checking;
}
public void setChecking(Checking checking) {
this.checking = checking;
}
@Override
public String toString() {
return "Employee [checking=" + checking + "]";
}
}
public class Checking {
String name;
String id;
public Checking(String name, String id) {
super();
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Checking [name=" + name + ", id=" + id + "]";
}
}
System.out.println(emp);
然后打印员工对象,
这应该会给出预期的结果。
首先,java 名称以大写字母开头是一个很好的 java 做法,例如您的 Employee
class 应该以 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
class 个实例变量时创建多个 bean。像下面这样创建单个 bean,并将 name
和 country
设置为 属性 for Employee
class.
applicationcontext.xml
<bean id="b1" class="com.autowire.Employee">
<property name="name" value= "A"/>
<property name="country" value= "India"/>
</bean>
接下来,在您的 main
class 中避免不必要的导入并调用 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();
}
}
我是 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);
}
}
main.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>
checking.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;
}
输出: 你好我的国家 is:null 你好,我的名字是空的
您可以在像这样的 bean 的 属性 名称之前使用 @Autowired
注释 -
@Autowired
@Qualifier("name")
private String name;
@Autowired
@Qualifier("country")
private String Country;
如果您像这样使用 autowire
,则不必使用 getter 和 setter 方法。
Razib 说的不错,但你要注入的class 是不是也必须加上注解(@Component、@service 等)?
您首先需要在 Employee class 中添加检查作为 属性。 所以你应该这样做,
public class Employee {
Checking checking;
public Employee(Checking checking) {
super();
this.checking = checking;
}
public Checking getChecking() {
return checking;
}
public void setChecking(Checking checking) {
this.checking = checking;
}
@Override
public String toString() {
return "Employee [checking=" + checking + "]";
}
}
public class Checking {
String name;
String id;
public Checking(String name, String id) {
super();
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Checking [name=" + name + ", id=" + id + "]";
}
}
System.out.println(emp);
然后打印员工对象,
这应该会给出预期的结果。
首先,java 名称以大写字母开头是一个很好的 java 做法,例如您的 Employee
class 应该以 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
class 个实例变量时创建多个 bean。像下面这样创建单个 bean,并将 name
和 country
设置为 属性 for Employee
class.
applicationcontext.xml
<bean id="b1" class="com.autowire.Employee">
<property name="name" value= "A"/>
<property name="country" value= "India"/>
</bean>
接下来,在您的 main
class 中避免不必要的导入并调用 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();
}
}