java Spring中的内豆是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40042493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are inner beans in Spring?
提问by dhaval joshi
I am new in spring.
我是春天的新人。
- How to inject inner bean in spring ?
- what is main purpose of inner bean in spring
- 如何在 spring 中注入内豆?
- 春天内豆的主要用途是什么
Please just guide when i should go for inner bean in spring
请指导我什么时候应该去春天吃内豆
回答by Nayan Sharma
In Spring framework, whenever a bean is used for only one particular property, it's advise to declare it as an inner bean. And the inner bean is supported both in setter injection ‘property‘ and constructor injection ‘constructor-arg‘. Like Inner classes are the classes which are defined inside the scope of another class. Similarly inner beans are the beans which are defined in the scope of another bean.
在 Spring 框架中,每当 bean 仅用于一个特定属性时,建议将其声明为内部 bean。并且在 setter 注入“property”和构造函数注入“constructor-arg”中都支持内部 bean。内部类是在另一个类的范围内定义的类。类似地,内部 bean 是在另一个 bean 的范围内定义的 bean。
Injecting Inner Beans
注入内豆
<bean id="outer_bean" class="OuterBean">
<property name="innerbean">
<bean class="InnerBean"/>
</property>
</bean>
As you can see, instead of using ref attribute of property tag, beans are defined inside property tag. In this case an instance of InnerBean class will be created and wired in to innerbean property of OuterBean class.
如您所见,bean 不是使用 property 标记的 ref 属性,而是在 property 标记内定义。在这种情况下,将创建 InnerBean 类的实例并将其连接到 OuterBean 类的 innerbean 属性。
We can use inner beans in constructor-arg tag as well like below
我们可以在constructor-arg标签中使用内部bean,如下所示
<bean id="outer_bean" class="OuterBean">
<constructor-arg>
<bean class="InnerBean"/>
</ constructor-arg>
</bean>
In this case an instance of InnerBean class will be created and will be passed as an constructor argument of OuterBean class.
在这种情况下,将创建 InnerBean 类的实例,并将作为 OuterBean 类的构造函数参数传递。
Consider this example
考虑这个例子
Student class
学生班
public class Student {
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Room class
房间等级
public class Room
{
private int roomNumber;
private Student allotedTo;
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public Student getAllotedTo() {
return allotedTo;
}
public void setAllotedTo(Student allotedTo) {
this.allotedTo = allotedTo;
}
@Override
public String toString() {
return "Room [roomNumber=" + roomNumber + ", allotedTo=" + allotedTo.getName()
+ "]";
}
}
beans entry in beans.xml
beans.xml 中的 beans 条目
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="room" class="Room">
<property name="roomNumber" value="101" />
<property name="allotedTo">
<bean class="Student">
<property name="name" value="joe bloggs" />
</bean>
</property>
</bean>
</beans>
TestInnerBeanDependency class
TestInnerBeanDependency 类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInnerBeanDependency {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
Room room = (Room)context.getBean("room");
System.out.println(room);
}
}
Run the TestInnerBeanDependency
运行 TestInnerBeanDependency
Room [roomNumber=101,allotedTo=joe bloggs]
An inner bean definition does not require a defined id or name; if specified, the container does not use such a value as an identifier. The container also ignores the scope flag on creation: Inner beans are always anonymous and they are always created with the outer bean. It is not possible to inject inner beans into collaborating beans other than into the enclosing bean or to access them independently.
内部 bean 定义不需要定义的 id 或名称;如果指定,容器不会使用这样的值作为标识符。容器在创建时也会忽略范围标志:内部 bean 始终是匿名的,并且它们始终与外部 bean 一起创建。不可能将内部 bean 注入到协作 bean 中,除非注入封闭 bean 或独立访问它们。
I hope it would make you understand the Inner beans. Thanks.
我希望它能让你了解内豆。谢谢。