@Autowired:构造器、参数、方法、属性;
1)、[标注在方法位置]:@Bean+方法参数;参数从容器中获取;默认不写@Autowired效果是一样的,都能自动装配
2)、[标在构造器上]:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以从容器中获取
3)、[放在参数位置];
第一步、创建bean
//默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作
@Component
public class Boss {private Car car;//构造器要用的组件,都是从容器中获取//如果只有一个有参构造,默认不写@Autowired效果一样//@Autowiredpublic Boss(Car car){this.car=car;System.out.println("Boss...有参构造器");}public Car getCar() {return car;}//@Autowired//标注在方法上,Spring容器创建当前对象,就会调用方法,完成赋值;//方法使用的参数,自定义类型的值从ioc容器中获取public void setCar(Car car) {this.car = car;}@Overridepublic String toString() {return "Boss{" +"car=" + car +'}';}
}
第二步、配置类
/*** @Bean标注的方法创建对象的时候,方法参数的值从容器中获取* @param car* @return*/@Beanpublic Color color(Car car){Color color = new Color();color.setCar(car);return color;}
第三步、测试
public class IOCTest_Autowired {@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);BookService bookService = applicationContext.getBean(BookService.class);System.out.println(bookService);// BookDao bookDao = applicationContext.getBean("bookDao");
// System.out.println(bookDao);Boss boss = applicationContext.getBean(Boss.class);System.out.println(boss);Car car = applicationContext.getBean(Car.class);System.out.println(car);Color color = applicationContext.getBean(Color.class);System.out.println(color);applicationContext.close();}
}
第四步、运行结果
4)、自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);
自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;
把Spring底层一些组件注入到自定义的Bean中;
xxxAware:功能使用xxxProcessor;
ApplicationContextAware==》ApplicationContextAwareProcessor;
第一步、创建Bean,连接xxxAware接口
@Component //将Red放入到容器中
public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {private ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {System.out.println("传入的ioc"+applicationContext);this.applicationContext = applicationContext;}@Overridepublic void setBeanName(String name) {System.out.println("当前bean的名字"+name);}@Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {String resolveStringValue = resolver.resolveStringValue("你好${os.name} 我是#{20*18}");System.out.println("解析的字符串:"+resolveStringValue);}
}
第二步、测试
public class IOCTest_Autowired {@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);BookService bookService = applicationContext.getBean(BookService.class);System.out.println(bookService);// BookDao bookDao = applicationContext.getBean("bookDao");
// System.out.println(bookDao);Boss boss = applicationContext.getBean(Boss.class);System.out.println(boss);Car car = applicationContext.getBean(Car.class);System.out.println(car);Color color = applicationContext.getBean(Color.class);System.out.println(color);//xxxAware放入bean的ioc容器名System.out.println(applicationContext);applicationContext.close();}
}
第三步、运行结果
Profile:
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
开发环境、测试环境、生产环境;
数据源:(/A)(/B) (/C)
@Profile
第一步、创建Profile配置类
@PropertySource("classpath:/dbconfig.properties")//扫描配置文件
@Configuration
public class MyConfigOfProfile implements EmbeddedValueResolverAware {@Value("${db.user}")private String user;//数据解析器private StringValueResolver valueResolver;//自定义数据库驱动类-->数据解析器private String driverClass;@Bean("testDataSource")public DataSource dataSourceTest(@Value("${db.password}")String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/sys");dataSource.setDriverClass(driverClass);return dataSource;}@Bean("devDataSource")public DataSource dataSourceDev(@Value("${db.password}") String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/springboot");dataSource.setDriverClass(driverClass);return dataSource;}@Bean("prodDataSource")public DataSource dataSourceProd(@Value("${db.password}") String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/jdbc");dataSource.setDriverClass(driverClass);return dataSource;}@Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {this.valueResolver = resolver;driverClass = valueResolver.resolveStringValue("${db.driverClass}");}
}
第二步、导入pom依赖
c3p0 c3p0 0.9.1.2 mysql mysql-connector-java 5.1.44
**第三步、测试 **
public class IOCTest_Profile {@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext =new AnnotationConfigApplicationContext(MyConfigOfProfile.class);//当前容器中有几个数据源String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);for (String name : beanNamesForType) {System.out.println(name);}applicationContext.close();}
}
第四步、运行结果
@Profile:指定组件在哪个环境的情况下才能被注册到容器中;
以前我们都不指定,任何环境下都能注册这个组件
1)、加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境—》@Profile(“default”)
①、命令行动态参数:
第一步、配置虚拟机
在虚拟机参数位置加载 -Dspring.profiles.active=test
第二步、在bean上添加@Profile注解,为bean自定义数据库名称
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MyConfigOfProfile implements EmbeddedValueResolverAware {@Value("${db.user}")private String user;private StringValueResolver valueResolver;private String driverClass;@Profile("test") //被调用数据库名称为test@Bean("testDataSource")public DataSource dataSourceTest(@Value("${db.password}")String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/sys");dataSource.setDriverClass(driverClass);return dataSource;}@Profile("dev") //被调用数据库名称为dev@Bean("devDataSource")public DataSource dataSourceDev(@Value("${db.password}") String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/springboot");dataSource.setDriverClass(driverClass);return dataSource;}@Profile("prod") //被调用数据库名称为prod@Bean("prodDataSource")public DataSource dataSourceProd(@Value("${db.password}") String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/jdbc");dataSource.setDriverClass(driverClass);return dataSource;}@Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {this.valueResolver = resolver;driverClass = valueResolver.resolveStringValue("${db.driverClass}");}
}
第三步、测试
public class IOCTest_Profile {//1.使用命令行动态参数:在虚拟机参数位置加载 -Dspring.profiles.active=test//2.@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext =new AnnotationConfigApplicationContext(MyConfigOfProfile.class);String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);for (String name : beanNamesForType) {System.out.println(name);}applicationContext.close();}
}
第四步、运行结果
第一步、配置类
/*** Profile:* Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;** 开发环境、测试环境、生产环境;* 数据源:(/A)(/B)(/C)* @Profile:指定组件在哪个环境的情况下才能被注册到容器中;* 以前我们都不指定,任何环境下都能注册这个组件** 1)、加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境* 2)、写在配置类上,只有指定环境的时候,整个配置类里面的所有配置才能开始生效* 3)、没有标注环境标识的bean,在任何环境下都是加载的。*/
//@Profile("test")
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MyConfigOfProfile implements EmbeddedValueResolverAware {@Value("${db.user}")private String user;private StringValueResolver valueResolver;private String driverClass;@Beanpublic Yellow yellow(){return new Yellow();}@Profile("test")@Bean("testDataSource")public DataSource dataSourceTest(@Value("${db.password}")String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sys");dataSource.setDriverClass(driverClass);return dataSource;}@Profile("dev")@Bean("devDataSource")public DataSource dataSourceDev(@Value("${db.password}") String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/springboot");dataSource.setDriverClass(driverClass);return dataSource;}@Profile("prod")@Bean("prodDataSource")public DataSource dataSourceProd(@Value("${db.password}") String pwd) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setUser(user);dataSource.setPassword(pwd);dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/jdbc");dataSource.setDriverClass(driverClass);return dataSource;}@Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {this.valueResolver = resolver;driverClass = valueResolver.resolveStringValue("${db.driverClass}");}
}
第二步、测试
public class IOCTest_Profile {//1.使用命令行动态参数:在虚拟机参数位置加载 -Dspring.profiles.active=test//2.代码的方式激活某种环境;@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext =new AnnotationConfigApplicationContext();//1. 创建一个applicationContext//2. 设置需要激活的环境applicationContext.getEnvironment().setActiveProfiles("test");//3. 注册主配置类applicationContext.register(MyConfigOfProfile.class);//4.启动刷新容器applicationContext.refresh();String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);for (String name : beanNamesForType) {System.out.println(name);}//没有标注环境标识的bean,在任何环境下都会加载Yellow yellow = applicationContext.getBean(Yellow.class);System.out.println("没有标注环境标识的bean==>"+ yellow);applicationContext.close();}
}
第三步、运行结果
学习新知识最正确的方式—》看官方文档
上一篇:Python中类的继承细讲
下一篇:SSL、TLS应用笔记