<beans>
<bean id="provider"
class="com.apress.prospring5.ch4.ConfigurableMessageProvider"
p:message="Love on the weekend" />
</beans>
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import com.apress.prospring5.ch2.decoupled.MessageRenderer;
import com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(value="classpath:spring/app-context-xml-01.xml")
public class AppConfigFive {
@Autowired
MessageProvider provider;
@Bean(name = "messageRenderer")
public MessageRenderer messageRenderer() {
MessageRenderer renderer = new StandardOutMessageRenderer();
renderer.setMessageProvider(provider);
return renderer;
}
}
java配置文件里引入XML配置文件
<beans>
<context:annotation-config/>
<bean class="com.apress.prospring5.ch4.mixed.AppConfigSix"/>
<bean id="messageRenderer"
class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer"
p:messageProvider-ref="provider"/>
</beans>
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import com.apress.prospring5.ch4.annotated.ConfigurableMessageProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfigSix {
@Bean
public MessageProvider provider() {
return new ConfigurableMessageProvider("Love on the weekend");
}
}
xml配置文件里引入java配置文件
public class Food {
private String name;
public Food() {
}
public Food(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.List;
public interface FoodProviderService {
List<Food> provideLunchSet();
}
import java.util.ArrayList;
import java.util.List;
import com.apress.prospring5.ch4.Food;
import com.apress.prospring5.ch4.FoodProviderService;
public class FoodProviderServiceImpl implements FoodProviderService {
@Override
public List<Food> provideLunchSet() {
List<Food> lunchSet = new ArrayList<>();
lunchSet.add(new Food("Coke"));
lunchSet.add(new Food("Hamburger"));
lunchSet.add(new Food("French Fries"));
return lunchSet;
}
}
import java.util.ArrayList;
import java.util.List;
import com.apress.prospring5.ch4.Food;
import com.apress.prospring5.ch4.FoodProviderService;
public class FoodProviderServiceImpl implements FoodProviderService {
@Override
public List<Food> provideLunchSet() {
List<Food> lunchSet = new ArrayList<>();
lunchSet.add(new Food("Milk"));
lunchSet.add(new Food("Biscuits"));
return lunchSet;
}
}
<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"
profile="highschool">
<bean id="foodProviderService"
class="com.apress.prospring5.ch4.highschool.FoodProviderServiceImpl"/>
</beans>
<!-- kindergarten-config.xml -->
<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"
profile="kindergarten">
<bean id="foodProviderService"
class="com.apress.prospring5.ch4.kindergarten.FoodProviderServiceImpl"/>
</beans>
import java.util.List;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ProfileXmlConfigExample {
public static void main(String... args) {
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext();
ctx.load("classpath:spring/*-config.xml");
ctx.refresh();
FoodProviderService foodProviderService =
ctx.getBean("foodProviderService", FoodProviderService.class);
List<Food> lunchSet = foodProviderService.provideLunchSet();
for (Food food: lunchSet) {
System.out.println("Food: " + food.getName());
}
ctx.close();
}
}
-Dspring.profiles.active="kindergarten"
profile的使用
import com.apress.prospring5.ch4.FoodProviderService;
import com.apress.prospring5.ch4.kindergarten.FoodProviderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("kindergarten")
public class KindergartenConfig {
@Bean
public FoodProviderService foodProviderService(){
return new FoodProviderServiceImpl();
}
}
import com.apress.prospring5.ch4.FoodProviderService;
import com.apress.prospring5.ch4.highschool.FoodProviderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("highschool")
public class HighschoolConfig {
@Bean
public FoodProviderService foodProviderService(){
return new FoodProviderServiceImpl();
}
}
import com.apress.prospring5.ch4.FoodProviderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={KindergartenConfig.class,
HighschoolConfig.class})
@ActiveProfiles("kindergarten")
public class ProfilesJavaConfigTest {
@Autowired FoodProviderService foodProviderService;
@Test
public void testProvider(){
assertTrue(foodProviderService.provideLunchSet() != null);
assertFalse(foodProviderService.provideLunchSet().isEmpty());
assertEquals(2, foodProviderService.provideLunchSet().size());
}
}
在java中配置profile
public interface MessageProvider {
String getMessage();
}
import javax.inject.Inject;
import javax.inject.Named;
@Named("messageProvider")
public class ConfigurableMessageProvider
implements MessageProvider {
private String message = "Default message";
public ConfigurableMessageProvider() {
}
@Inject
@Named("message")
public ConfigurableMessageProvider(String message) {
this.message = message;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
public interface MessageRenderer {
void render();
void setMessageProvider(MessageProvider provider);
MessageProvider getMessageProvider();
}
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
@Named("messageRenderer")
@Singleton
public class StandardOutMessageRenderer
implements MessageRenderer {
@Inject
@Named("messageProvider")
private MessageProvider messageProvider = null;
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());
}
public void setMessageProvider(MessageProvider provider) {
this.messageProvider = provider;
}
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
<beans>
<context:component-scan
base-package="com.apress.prospring5.ch4"/>
<bean id="message" class="java.lang.String">
<constructor-arg value="Gravity is working against me"/>
</bean>
</beans>
使用JSR 330标准 的 注解DI
public class Singer {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "\tName: " + name + "\n\t" + "Age: " + age;
}
}
import com.apress.prospring5.ch3.xml.Singer
beans {
singer(Singer, name: 'John Mayer', age: 39)
}
import com.apress.prospring5.ch3.xml.Singer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericGroovyApplicationContext;
public class GroovyBeansFromJava {
public static void main(String... args) {
ApplicationContext context =
new GenericGroovyApplicationContext("classpath:beans.groovy");
Singer singer = context.getBean("singer", Singer.class);
System.out.println(singer);
}
}
apply plugin: 'groovy'
dependencies {
compile misc.groovy
compile project(':chapter03:bean-inheritance')
}
ext {
springVersion = '5.0.0.M4'
groovyVersion = '2.4.5'
...
misc = [
...
groovy: "org.codehaus.groovy:groovy-all:$groovyVersion"
]
...
}
Spring中使用Groovy注入
import com.apress.prospring5.ch3.xml.Singer
import org.springframework.context.support.GenericApplicationContext
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
def ctx = new GenericApplicationContext()
def reader = new GroovyBeanDefinitionReader(ctx)
reader.beans {
singer(Singer, name: 'John Mayer', age: 39)
}
ctx.refresh()
println ctx.getBean("singer")
Groovy中获取ApplicationContext