package com.springconfig.example.bean;
public class HelloWorld ...{
private String word;
public void sayHello() ...{
System.out.println(this.word);
}
public void setWord(String word) ...{
this.word = word;
}
}
package com.springconfig.example.chapter2;
@Configuration
public class HelloWorldConfiguration ...{
@Bean
public HelloWorld helloWorld()...{
HelloWorld helloWorldBean = new HelloWorld();
helloWorldBean.setWord("Hello world!");
return helloWorldBean;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloWorld" class="com.springconfig.example.bean.HelloWorld">
<property name="word">
<value>Hello world!</value>
</property>
</bean>
</beans>
package com.springconfig.example.chapter2;
public class HelloWorldWithJavaConfig ...{
public static void main(String[] args)...{
ApplicationContext config = new AnnotationApplicationContext(HelloWorldConfiguration.class.getName());
HelloWorld helloWorld = (HelloWorld)config.getBean("helloWorld");
helloWorld.sayHello();
}
}