使用Java Code配置Spring

在这里插入图片描述这篇文章介绍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();
    }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值