@Configuration
public class MainConfig {
//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
}
}
注册组件
@Configuration
public class MainConfig2 {
/**
* @Conditional({Condition}) : 按照一定的条件进行判断,满足条件给容器中注册bean
*
* 如果系统是windows,给容器中注册Person类型("bill")
* 如果是linux系统,给容器中注册Person类型("linus")
*/
@Conditional(WindowsCondition.class)
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
}
标在类上
//类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效;
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig2 {
//...
}
测试方法
使用,可配置VM参数:-Dos.name=linux 测试
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void test03(){
//获取所有Person类型的Bean名字
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
//获取运行时环境
ConfigurableEnvironment environment = applicationContext.getEnvironment();
//动态获取环境变量的值;Windows 10
String property = environment.getProperty("os.name");
System.out.println(property);
for (String name : namesForType) {
System.out.println(name);
}
//获取所有Person类型Bean的对象,名字是否是lunus或bill
Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
System.out.println(persons);
}
}
@Configuration
//MyImportBeanDefinitionRegistrar是手动注册到Bean容器中,可以指定规则
@Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
//@Import导入组件,id默认是组件的全类名
public class MainConfig2 {
//...
}
测试
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
//输出:除了Spring自己的Bean还有手动定制规则新导入的rainBow
@Test
public void testImport(){
printBeans(applicationContext);
Blue bean = applicationContext.getBean(Blue.class);
System.out.println(bean);
}
//输出容器中所有的Bean名
private void printBeans(AnnotationConfigApplicationContext applicationContext){
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}
//创建一个Spring定义的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {
//返回一个Color对象,这个对象会添加到容器中
@Override
public Color getObject() throws Exception {
// TODO Auto-generated method stub
System.out.println("ColorFactoryBean...getObject...");
return new Color();
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Color.class;
}
//是单例?
//true:这个bean是单实例,在容器中保存一份
//false:多实例,每次获取都会创建一个新的bean;
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}
配置
@Configuration
public class MainConfig2 {
/**
* 给容器中注册组件;
* 4)、使用Spring提供的 FactoryBean(工厂Bean);
* 1)、默认获取到的是工厂bean调用getObject创建的对象
* 2)、要获取工厂Bean本身,我们需要给id前面加一个&
* &colorFactoryBean
*/
@Bean
public ColorFactoryBean colorFactoryBean(){
return new ColorFactoryBean();
}
}
测试
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
//直接使用输出的不是工厂本身,而是工厂内getObject创建的Bean
@Test
public void testImport(){
//工厂Bean获取的是调用getObject创建的Color对象
Object bean2 = applicationContext.getBean("colorFactoryBean");
Object bean3 = applicationContext.getBean("colorFactoryBean");
//输出:com.xxx.bean.Color
System.out.println("bean的类型:"+bean2.getClass());
System.out.println(bean2 == bean3);
//输出:com.xxx.bean.ColorFactoryBean
Object bean4 = applicationContext.getBean("&colorFactoryBean");
System.out.println(bean4.getClass());
}
//输出容器中所有的Bean名
private void printBeans(AnnotationConfigApplicationContext applicationContext){
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}
属性赋值
@Value赋值
基本数值
public class Person {
//使用@Value赋值;
//1、基本数值
@Value("张三")
private String name;
}
SpEL表达式 #{}
public class Person {
//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
@Value("张三")
private String name;
//计算后值为18
@Value("#{20-2}")
private Integer age;
}
@PropertySource配置文件取值
读取properties配置文件的值,例如读取数据库连接信息
读取文件
取值
读取配置文件
使用@PropertySource读取外部配置文件
//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Person person(){
return new Person();
}
}
配置文件person.properties
person.nickName=法外狂徒
配置文件取值${}
取出配置文件properties中的值
public class Person {
//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
//3、可以写${};取出配置文件中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
}
运行时环境变量取值
运行时properties配置文件加载进环境变量,可以直接取出
public class IOCTest_PropertyValue {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
@Test
public void test01(){
//直接从applicationContext容器中获取运行时环境变量ConfigurableEnvironment
ConfigurableEnvironment environment = applicationContext.getEnvironment();
//取值
String property = environment.getProperty("person.nickName");
System.out.println(property);
applicationContext.close();
}
}
自动装配
/**
* @Bean标注的方法创建对象的时候,方法参数的值从容器中获取
* @param car car也是从容器中获取的值,前提是car已经注入容器
* @return
*/
@Bean
public Color color(Car car){
Color color = new Color();
color.setCar(car);
return color;
}
//默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作
@Component
public class Boss {
private Car car;
//构造器要用的组件,都是从容器中获取
//@Autowired
public Boss(Car car){
this.car = car;
System.out.println("Boss...有参构造器");
}
}
参数上
标在参数前
public void setCar(@Autowired Car car) {
this.car = car;
}
@Configuration
public class MainConfig {
//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
}
}
注册组件
@Configuration
public class MainConfig2 {
/**
* @Conditional({Condition}) : 按照一定的条件进行判断,满足条件给容器中注册bean
*
* 如果系统是windows,给容器中注册Person类型("bill")
* 如果是linux系统,给容器中注册Person类型("linus")
*/
@Conditional(WindowsCondition.class)
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
}
标在类上
//类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效;
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig2 {
//...
}
测试方法
使用,可配置VM参数:-Dos.name=linux 测试
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void test03(){
//获取所有Person类型的Bean名字
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
//获取运行时环境
ConfigurableEnvironment environment = applicationContext.getEnvironment();
//动态获取环境变量的值;Windows 10
String property = environment.getProperty("os.name");
System.out.println(property);
for (String name : namesForType) {
System.out.println(name);
}
//获取所有Person类型Bean的对象,名字是否是lunus或bill
Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
System.out.println(persons);
}
}
@Configuration
//MyImportBeanDefinitionRegistrar是手动注册到Bean容器中,可以指定规则
@Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
//@Import导入组件,id默认是组件的全类名
public class MainConfig2 {
//...
}
测试
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
//输出:除了Spring自己的Bean还有手动定制规则新导入的rainBow
@Test
public void testImport(){
printBeans(applicationContext);
Blue bean = applicationContext.getBean(Blue.class);
System.out.println(bean);
}
//输出容器中所有的Bean名
private void printBeans(AnnotationConfigApplicationContext applicationContext){
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}
//创建一个Spring定义的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {
//返回一个Color对象,这个对象会添加到容器中
@Override
public Color getObject() throws Exception {
// TODO Auto-generated method stub
System.out.println("ColorFactoryBean...getObject...");
return new Color();
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Color.class;
}
//是单例?
//true:这个bean是单实例,在容器中保存一份
//false:多实例,每次获取都会创建一个新的bean;
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}
配置
@Configuration
public class MainConfig2 {
/**
* 给容器中注册组件;
* 4)、使用Spring提供的 FactoryBean(工厂Bean);
* 1)、默认获取到的是工厂bean调用getObject创建的对象
* 2)、要获取工厂Bean本身,我们需要给id前面加一个&
* &colorFactoryBean
*/
@Bean
public ColorFactoryBean colorFactoryBean(){
return new ColorFactoryBean();
}
}
测试
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
//直接使用输出的不是工厂本身,而是工厂内getObject创建的Bean
@Test
public void testImport(){
//工厂Bean获取的是调用getObject创建的Color对象
Object bean2 = applicationContext.getBean("colorFactoryBean");
Object bean3 = applicationContext.getBean("colorFactoryBean");
//输出:com.xxx.bean.Color
System.out.println("bean的类型:"+bean2.getClass());
System.out.println(bean2 == bean3);
//输出:com.xxx.bean.ColorFactoryBean
Object bean4 = applicationContext.getBean("&colorFactoryBean");
System.out.println(bean4.getClass());
}
//输出容器中所有的Bean名
private void printBeans(AnnotationConfigApplicationContext applicationContext){
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}
属性赋值
@Value赋值
基本数值
public class Person {
//使用@Value赋值;
//1、基本数值
@Value("张三")
private String name;
}
SpEL表达式 #{}
public class Person {
//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
@Value("张三")
private String name;
//计算后值为18
@Value("#{20-2}")
private Integer age;
}
@PropertySource配置文件取值
读取properties配置文件的值,例如读取数据库连接信息
读取文件
取值
读取配置文件
使用@PropertySource读取外部配置文件
//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Person person(){
return new Person();
}
}
配置文件person.properties
person.nickName=法外狂徒
配置文件取值${}
取出配置文件properties中的值
public class Person {
//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
//3、可以写${};取出配置文件中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
}
运行时环境变量取值
运行时properties配置文件加载进环境变量,可以直接取出
public class IOCTest_PropertyValue {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
@Test
public void test01(){
//直接从applicationContext容器中获取运行时环境变量ConfigurableEnvironment
ConfigurableEnvironment environment = applicationContext.getEnvironment();
//取值
String property = environment.getProperty("person.nickName");
System.out.println(property);
applicationContext.close();
}
}
自动装配
/**
* @Bean标注的方法创建对象的时候,方法参数的值从容器中获取
* @param car car也是从容器中获取的值,前提是car已经注入容器
* @return
*/
@Bean
public Color color(Car car){
Color color = new Color();
color.setCar(car);
return color;
}
//默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作
@Component
public class Boss {
private Car car;
//构造器要用的组件,都是从容器中获取
//@Autowired
public Boss(Car car){
this.car = car;
System.out.println("Boss...有参构造器");
}
}
参数上
标在参数前
public void setCar(@Autowired Car car) {
this.car = car;
}