<beans ...>
<bean id="renderer"
class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer">
<property name="messageProvider" ref="provider"/>
</bean>
<bean id="provider"
class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/>
</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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="renderer"
class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer"
p:messageProvider-ref="provider"/>
<bean id="provider"
class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/>
</beans>
两种Bean注入方式
import org.springframework.beans.factory.annotation.Autowired;
@Service("renderer")
public class StandardOutMessageRenderer implements MessageRenderer {
...
@Override
@Autowired
public void setMessageProvider(MessageProvider provider) {
this.messageProvider = provider;
}
}
通过注解Setter注入
import com.apress.prospring5.ch2.decoupled.MessageProvider;
public class ConfigurableMessageProvider
implements MessageProvider {
private String message;
public ConfigurableMessageProvider(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}
<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="messageProvider"
class="com.apress.prospring5.ch3.xml.ConfigurableMessageProvider">
<constructor-arg value="I hope that someone gets my message in a bottle"/>
</bean>
</beans>
构造器注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="provider"
class="com.apress.prospring5.ch3.xml.ConfigurableMessageProvider"
c:message="I hope that someone gets my message in a bottle"/>
</beans>
另一种配置方式
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("provider")
public class ConfigurableMessageProvider implements MessageProvider {
private String message;
@Autowired
public ConfigurableMessageProvider(
(@Value("Configurable message") String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
}
<beans ...>
<context:component-scan
base-package="com.apress.prospring5.ch3.annotated"/>
<bean id="message" class="java.lang.String"
c:_0="I hope that someone gets my message in a bottle"/>
</beans>
构造器配置字符串
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("provider")
public class ConfigurableMessageProvider implements MessageProvider {
private String message;
@Autowired
public ConfigurableMessageProvider(String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
}
使用Autowire
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("provider")
public class ConfigurableMessageProvider implements MessageProvider {
private String message;
@Autowired
public ConfigurableMessageProvider(
(@Value("Configurable message") String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
}
<beans ...>
<context:component-scan
base-package="com.apress.prospring5.ch3.annotated"/>
<bean id="message" class="java.lang.String"
c:_0="I hope that someone gets my message in a bottle"/>
</beans>
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("provider")
public class ConfigurableMessageProvider implements MessageProvider {
private String message;
@Autowired
public ConfigurableMessageProvider(String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
}
构造器注入
import org.springframework.context.support.GenericXmlApplicationContext;
public class ConstructorConfusion {
private String someValue;
public ConstructorConfusion(String someValue) {
System.out.println("ConstructorConfusion(String) called");
this.someValue = someValue;
}
public ConstructorConfusion(int someValue) {
System.out.println("ConstructorConfusion(int) called");
this.someValue = "Number: " + Integer.toString(someValue);
}
public String toString() {
return someValue;
}
public static void main(String... args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:spring/app-context-xml.xml");
ctx.refresh();
ConstructorConfusion cc = (ConstructorConfusion)
ctx.getBean("constructorConfusion");
System.out.println(cc);
ctx.close
}
}
<beans ...>
<bean id="provider"
class="com.apress.prospring5.ch3.xml.ConfigurableMessageProvider"
c:message="I hope that someone gets my message in a bottle"/>
<bean id="constructorConfusion"
class="com.apress.prospring5.ch3.xml.ConstructorConfusion">
<constructor-arg>
<value>90</value>
</constructor-arg>
</bean>
</beans>
参数默认为字符串类型,也可以指定
@Service("constructorConfusion")
public class ConstructorConfusion {
private String someValue;
public ConstructorConfusion(String someValue) {
System.out.println("ConstructorConfusion(String) called");
this.someValue = someValue;
}
@Autowired
public ConstructorConfusion(@Value("90") int someValue) {
System.out.println("ConstructorConfusion(int) called");
this.someValue = "Number: " + Integer.toString(someValue);
}
public String toString() {
return someValue;
}
public static void main(String... args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:spring/app-context-annotation.xml");
ctx.refresh();
ConstructorConfusion cc = (ConstructorConfusion)
ctx.getBean("constructorConfusion");
System.out.println(cc);
ctx.close();
}
}
autowired是告诉Spring,构造对象时,用这个构造器。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("singer")
public class Singer {
@Autowired
private Inspiration inspirationBean;
public void sing() {
System.out.println("... " + inspirationBean.getLyric());
}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Inspiration {
private String lyric =
"I can keep the door cracked open, to let light through";
public Inspiration(
@Value("For all my running, I can understand") String lyric) {
this.lyric = lyric;
}
public String getLyric() {
return lyric;
}
public void setLyric(String lyric) {
this.lyric = lyric;
}
}
字段注入