Spring Setter Injection and Constructor Injection

Setter Injection

AppContext.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.xsd"> <bean id="messageRenderer"
class="com.apress.prospring4.ch3.xml.StandardOutMessageRenderer"
p:messageProvider-ref="messageProvider"/> <bean id="messageProvider"
class="com.apress.prospring4.ch3.xml.HelloWorldMessageProvider"/>
</beans>

Source Code

package com.apress.prospring4.ch3;

public interface MessageRenderer {
void render();
void setMessageProvider(MessageProvider provider);
MessageProvider getMessageProvider();
}
package com.apress.prospring4.ch3;

public interface MessageProvider {
String getMessage();
}
package com.apress.prospring4.ch3.xml;

import com.apress.prospring4.ch3.MessageProvider;

public class HelloWorldMessageProvider implements MessageProvider {
@Override
public String getMessage() {
return "Hello World!";
}
}
package com.apress.prospring4.ch3.xml;

import com.apress.prospring4.ch3.MessageProvider;
import com.apress.prospring4.ch3.MessageRenderer; public class StandardOutMessageRenderer implements MessageRenderer {
private MessageProvider messageProvider; @Override
public void render() {
if (messageProvider == null) {
throw new RuntimeException(
"You must set the property messageProvider of class:"
+ StandardOutMessageRenderer.class.getName());
} System.out.println(messageProvider.getMessage());
} @Override
public void setMessageProvider(MessageProvider provider) {
this.messageProvider = provider;
} @Override
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
package com.apress.prospring4.ch3;

import org.springframework.context.support.GenericXmlApplicationContext;

public class DeclareSpringComponents {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context-xml.xml");
ctx.refresh(); MessageRenderer messageRenderer = ctx.getBean("messageRenderer",
MessageRenderer.class); messageRenderer.render();
}
}

Constructor Injection

AppContext.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: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="messageProvider"
class="com.apress.prospring4.ch3.xml.ConfigurableMessageProvider"
c:message="This is a configurable message"/> <bean id="constructorConfusion"
class="com.apress.prospring4.ch3.xml.ConstructorConfusion">
<constructor-arg type="int">
<value>90</value>
</constructor-arg>
</bean>
</beans>

Source code

package com.apress.prospring4.ch3;

public interface MessageProvider {
String getMessage();
}
package com.apress.prospring4.ch3.xml;

import com.apress.prospring4.ch3.MessageProvider;

public class ConfigurableMessageProvider implements MessageProvider {
private String message; public ConfigurableMessageProvider(String message) {
this.message = message;
} @Override
public String getMessage() {
return message;
}
}
package com.apress.prospring4.ch3.xml;
import com.apress.prospring4.ch3.MessageProvider; 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 static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context-xml.xml");
ctx.refresh(); ConstructorConfusion cc = (ConstructorConfusion) ctx.getBean("constructorConfusion");
System.out.println(cc); MessageProvider mp = (MessageProvider) ctx.getBean("messageProvider");
System.out.println(mp.getMessage()); } public String toString() {
return someValue;
}
}
上一篇:Centos 7环境下安装配置MySQL 5.7


下一篇:mac安装brew 软件包管理工具Homebrew