本文主要是介绍springIoc容器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
springIoc容器
Ioc容器的两个作用:
(1)管理bean,包括发布和获取bean。
(2)描述Bean之间的依赖关系。
Ioc是管理Bean的容器,所有的Ioc都继承自BeanFactory,他是一个顶级容器。
BeanFactory方法有getBean,containBean,isSingleton,isPrototype,getAliases。
getBean()返回Bean会调用isPrototype()方法,如果返回true,就会创建新Bean返回。
ApplicationContext接口继承自BeanFactory接口,并且扩展了消息国际化接口,环境可配置接口,应用事件发布接口,资源模式解析接口。
ioc的配置文件:@Configuration,spring容器用它生成的Ioc配置去装配Bean。
装配Bean:@Bean(name=)return pojo;将这个返回的pojo装配到环境中去。
@configuration
public class AppConf{
@Bean(name="user")
public User initUser(){
return new User();}
}
然后就可以使用AnnotationConfigApplicationContext来构建Ioc容器了。
psvm{
ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConf.class);
User u=(User)ctx.getBean("user");
}
- 使用@Component和@ComponsentScan装配Bean
使用@Component装配的bean默认使用类名的首字母小写作为Bean的名字。
@ComponentScan在它的包和子包中扫描,将Conponent装配。
使用这个组合就可以方便配置类的构造了:
@ComponentScan
@Configuration
public class AppConf{
}
然后再使用注解上下文获得Ioc容器:
psvm{
ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConf.class);
User u=(User)ctx.getBean("user");
}
@ComponentScan可以设置扫描范围
@ComponentScan(basePackage={"com.springboot.chapter3.*"},excudeFilters={@Filter(classes={UserService.class})
//排除包
3.将引入包的类装配到Ioc中
eg.将DBCP装配
先在pom中引入DBCP,然后使用@Bean.
@Bean(name="dataSource")
public DataSource getDataSource(){
Properties p=new Properties();
p.setProperty("driver","com.mysql.jdbc.Driver");
p.setProperty("url","jdbc:mysql://localhost:3306/chapter3");
p.setProperty("username","root");
p.setProperty("password","12345");
return BasicDataSourceFactory.createDataSource(p);
}
3.3依赖注入(DI)
@Autowired通过在Ioc容器中通过Type查找相应的Bean。
当这个类型有多个子类都符合时候,就会通过对象名字比如(Animal dog;),就会查找name=dog的Bean。
@Autowired(required=false) :允许找不到Bean设置为null
使用@Primary和@Qualifier消除歧义性
- 使用@Primary标注在Bean上面,可以优先使用Bean
@Primary
@Component
public class Dog{}
- 使用@Qualifier和@Autowired,使用名字和类型一起获得Bean
@Autowired
@Qualifier("dog")
private Animal dog=null;
Bean的生命周期
(1)资源定位:@ComponseScan扫描到Bean
(2)Bean定义:定义载入BeanDefinnition实例
(3)发布Bean定义:Ioc装载Bean的定义
(4)实例化:创建Bean的实例对象
(5)依赖注入
所有的单例作用域的Bean对象都会被创建并放入IoC容器中。对于原型作用域的Bean对象,它们可能在每次被请求时都会被创建。
如果开启延迟注入:@ComponetnScan(lazyInit=true),就会在getBean时候时候再创建Bean
之后就是程序员可以操作的回调函数了
(6)接口BeanNameAware的setBeanName方法,会回传BeanName作为参数(在Compontent中实现)
@Override
public void getBeanName(String BeanName){}
(7)接口BeanFactoryAware的setBeanFactory方法,可以用来获得上下文。
@Oerride
public void setBeanFactory(BeanFactory beanFactory){}
(8)接口ApplicationContextAware的setApplicationContext()方法,只用applicaitonContext的Ioc才能使用
@Override
public void setApplicationContext(ApplicationContext applicationContext){}
(9)针对全部Bean的预处理方法BeanPostProcessor接口(后置初始化器),的postProcessBeforeInitialization方法。
(10)使用@PostConstruct标注的方法,自定义的这个compontent初始化方法
(11)初始化结束的InitializingBeand的afterPropertiesSet方法
(12)针对全部Bean的预处理方法BeanPostProcessor接口(后置初始化器)的postProcessAfterInitialization方法。
(13)使用
(14)@PreDestroy标注方法,自定义销毁方法
(15)接口DisposableBean的destroy方法
后置初始化器:对所有Bean都有效
@Component
public class BeanPostProcessorExample implements BeanPostProcessor{
@Override
postProcessBeforeInitialization()
@Override
postProcesssAfterInitialization()
}
Ioc容器只管理singleton的生命周期,而prototype只负责创建
使用属性文件
在properties.application中配置完成后,可以使用
@Value(“${database.driverName}”)来进行使用属性配置。
使用@ConfigurationProperties(“database”)在自动装配application中的(database.成员变量名字)
使用@PropertySource(value={“classpath:database.properties”},ignoreResourceNotFound=true)将database.properties文件中的属性装配到上下文中。
条件装配Bean:检查上下文有没有在载入properties的配置(@Value载入的)
使用@Condition(ConditionImplments.class)配合接口Condition使用。
@Bean
@Condition(DatabaseCondition.class)
public DataSource getDatasource()
Bean的作用域:@Scope
prototype会在每一次getBean过程中创建一个Bean。
singleton单例
session:每个HTTP会话(SpringWeb工程才能使用)
这篇关于springIoc容器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!