目录
初始化实现FactoryBean接口的类,实现对bean加载到容器之前的批处理操作
使用proxyBeanMethods=true可以保障调用此方法得到的对象是从容器中获取的而不是重新创建的编辑
六、@import注解 加载bean(此形式可以有效的降低源代码与Spring技术的耦合度,在spring技术底层及诸多框架的整合中大量使用)
八、导入实现了ImportSelector接口的类,实现对导入源的编程式处理
十、导入实现了BeanDefinitionRegistryPostProcessor接口的类,通过BeanDefinition的注册器注册实名bean,实现对容器中bean的最终裁定
一、使用xml定义bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--xml方式声明自己开发的bean-->
<bean id="cat" class="Cat"/>
<bean class="Dog"/>
<!--xml方式声明第三方开发的bean-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"/>
<bean class="com.alibaba.druid.pool.DruidDataSource"/>
<bean class="com.alibaba.druid.pool.DruidDataSource"/>
</beans>
将如何使用bean?
public class App1 {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationCOntext1.xml");
// Object cat = ctx.getBean("cat");
// System.out.println(cat);
// Dog dog = ctx.getBean(Dog.class);
// System.out.println(dog);
String[] names = ctx.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}
二、使用XML+注解定义bean
1、这个是定义加载哪些bean
2、如何定义bean?
使用@Component及其衍生注解@Controller , @Service、@Repository定义bean
@Service("bookService")
public class BookServiceImpl1 implements BookSerivce {
@Override
public void check() {
System.out.println("book service 1..");
}
}
3、如何定义第三方bean?
使用@Bean定义第三方bean,并将所在类定义为配置类或Bean
//@Component
@Configuration
public class DbConfig {
@Bean
public DruidDataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
return ds;
}
}
三、配置类+注解定义bean
1、配置类
@ComponentScan({"com.itheima.bean","com.itheima.config"})
public class SpringConfig3 {
@Bean
public DogFactoryBean dog(){
return new DogFactoryBean();
}
}