gpt4 book ai didi

java - Spring框架中的bean Autowiring

转载 作者:行者123 更新时间:2023-12-02 10:22:47 25 4
gpt4 key购买 nike

我正在尝试从 Pro Spring 5 书中学习 Spring。

这是一个我不理解 Autowiring 的例子:

<?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.xsd">

<bean id="fooOne" class="com.apress.prospring5.ch3.xml.Foo"/>
<bean id="barOne" class="com.apress.prospring5.ch3.xml.Bar"/>

<bean id="targetByName" autowire="byName" class="com.apress.prospring5.ch3.xml.Target"
lazy-init="true"/>

<bean id="targetByType" autowire="byType" class="com.apress.prospring5.ch3.xml.Target"
lazy-init="true"/>

<bean id="targetConstructor" autowire="constructor"
class="com.apress.prospring5.ch3.xml.Target" lazy-init="true"/>
</beans>

Tarjet 级

package com.apress.prospring5.ch3.xml;

import org.springframework.context.support.GenericXmlApplicationContext;

public class Target {
private Foo fooOne;
private Foo fooTwo;
private Bar bar;

public Target() {
}

public Target(Foo foo) {
System.out.println("Target(Foo) called");
}

public Target(Foo foo, Bar bar) {
System.out.println("Target(Foo, Bar) called");
}

public void setFooOne(Foo fooOne) {
this.fooOne = fooOne;
System.out.println("Property fooOne set");
}

public void setFooTwo(Foo foo) {
this.fooTwo = foo;
System.out.println("Property fooTwo set");
}

public void setBar(Bar bar) {
this.bar = bar;
System.out.println("Property bar set");
}

public static void main(String... args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:spring/app-context-03.xml");
ctx.refresh();

Target t = null;

System.out.println("Using byName:\n");
t = (Target) ctx.getBean("targetByName");

System.out.println("\nUsing byType:\n");
t = (Target) ctx.getBean("targetByType");

System.out.println("\nUsing constructor:\n");
t = (Target) ctx.getBean("targetConstructor");

ctx.close();

}
}

Foo 类

package com.apress.prospring5.ch3.xml;

public class Foo {

}

酒吧类

package com.apress.prospring5.ch3.xml;

public class Bar {

}

我不明白的地方:

<bean id="targetByName" autowire="byName" class="com.apress.prospring5.ch3.xml.Target"
lazy-init="true"/>

知道我们没有在 bean 定义中使用任何属性或构造函数注入(inject),如何注入(inject)目标属性 (fooOne,fooTwo,bar)?

通常我们应该有这样的东西:

 <property name = "fooOne">
<bean id = "fooOne" class = "com.apress.prospring5.ch3.xml.Foo"/>
</property>

最佳答案

<bean id="targetByName" autowire="byName" class="com.apress.prospring5.ch3.xml.Target"
lazy-init="true"/>

因为它将自动连线模式声明为“byName”,它具有以下行为(取自 docs ):

Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master and uses it to set the property.

这意味着它是setter注入(inject)。

回到你的例子,由于Target有以下setter,spring将执行以下注入(inject):

public class Target {

// Find a bean which name is "fooOne" , and call this setter to inject
public void setFooOne(Foo fooOne) {}

// Find a bean which name is "fooTwo" , and call this setter to inject (As no beans called fooTwo in your example , it will be null)
public void setFooTwo(Foo foo) {}

//Find a bean which name is "bar" , and call this setter to inject (As no beans called bar in your example , it will be null)
public void setBar(Bar bar) {}
}

当然,如果bean的类型与setter参数的类型不匹配,就会发生异常。

关于java - Spring框架中的bean Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54206126/

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