这篇文章介绍java code的配置方法。
步骤如下:
编写一个SportConfig.class
@Configuration
@ComponentScan("com.luv2code.springdemo")
public class SportConfig {
}
@Component
public class TennisCoach implements Coach {
@Autowired
@Qualifier("randomService")
private FortuneService fortuneService;
//define init method
@PostConstruct
public void doMyStartupStuff() {
System.out.println(">> TennisCoach: doMyStartupStuff");
}
//define destory method
@PreDestroy
public void doMyCleanupStuff() {
System.out.println(">> TennisCoach: doMyCleanupStuff");
}
@Override
public String getDailyWorkout() {
return "practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
新建一个实现类
public class JavaConfigDemoApp {
public static void main(String[] args) {
//read spring config file
//注意指定class不要带“”
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SportConfig.class);
//get the bean from spring container
Coach theCoach = context.getBean("tennisCoach",Coach.class);
//call method to get the daily fortune
System.out.println(theCoach.getDailyFortune());
//close the context
context.close();
}
}
Result:
>> TennisCoach: doMyStartupStuff
The journey is the reward
>> TennisCoach: doMyCleanupStuff
使用Java Code定义Bean
新建SportConfig 类定义Bean
@Configuration
public class SportConfig {
//define bean for sadFortuneService
@Bean
public FortuneService sadFortuneService() {
return new SadFortuneService();
}
//define bean for swimCoach and inject dependency
@Bean
public Coach swimCoach() {
return new SwimCoach(sadFortuneService()); // inject depedencies
}
}
SadFortuneService 类
public class SadFortuneService implements FortuneService{
public String getFortune() {
return "It is a sad day!";
}
}
SwimCoach 类
public class SwimCoach implements Coach {
private FortuneService fortuneService;
public SwimCoach(FortuneService theFortuneService) {
fortuneService = theFortuneService;
}
@Override
public String getDailyWorkout() {
return "Swim 1000ms a day";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
实现类SwimJavaConfigDemoApp :
public class SwimJavaConfigDemoApp {
public static void main(String[] args) {
//read spring config file
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SportConfig.class);
//get the bean from spring container
Coach theCoach = context.getBean("swimCoach",Coach.class);
//call a method on the bean
System.out.println(theCoach.getDailyWorkout());
//call method to get the daily fortune
System.out.println(theCoach.getDailyFortune());
//close the context
context.close();
}
}
Swim 1000ms a day
It is a sad day!
最后写一下从property 文件中赋值
sport.properties配置文件
foo.email = 8200@
foo.team = Amazon
配置类
@Configuration
//@ComponentScan("com.luv2code.springdemo")
@PropertySource("classpath:sport.properties")
public class SportConfig {
//define bean for swimCoach and inject dependency
@Bean
public Coach swimCoach() {
return new SwimCoach(sadFortuneService());
}
}
SwimCoach 类
public class SwimCoach implements Coach {
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
}
实现类
public class SwimJavaConfigDemoApp {
public static void main(String[] args) {
//read spring config file
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SportConfig.class);
//get the bean from spring container
SwimCoach theCoach = context.getBean("swimCoach",SwimCoach.class);
System.out.println("Email:"+theCoach.getEmail());
System.out.println("Team:"+theCoach.getTeam());
//close the context
context.close();
}
}
email:8200@
Team:Amazon
Q:如果在spring4.2或一下版本,可能出现:
${foo.email}
${foo.team}
Solution:
public class SportConfig {
// add support to resolve ${...} properties
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}