文章摘要
一、三种配置Bean的方式
1、基于XML配置Bean
1.1、基于构造方法实例化对象
<!-- 使用==》 无参构造-->
<bean id="apple1" class="com.test.spring.ioc.entity.Apple"></bean>
<!-- 使用===》 有参构造-->
<bean id="apple2" class="com.test.spring.ioc.entity.Apple">
<constructor-arg name="color" value="红色"></constructor-arg>
<constructor-arg name="origin" value="中国"></constructor-arg>
<constructor-arg name="title" value="金帅"></constructor-arg>
</bean>
1.2、基于静态工厂实例化对象
public class AppleStaticFactory {
//静态工厂
public static Apple cerateSweetApple(){
Apple apple = new Apple();
apple.setTitle("金帅");
apple.setOrigin("中国");
apple.setColor("金色");
return apple;
}
}
<!-- 利用静态工厂获取对象-->
<bean id="apple4" class="com.test.spring.ioc.factory.AppleStaticFactory"
factory-method="cerateSweetApple"></bean>
1.3、基于工厂实例化对象
public class AppleFactoryInstance {
public Apple cerateSweetApple(){
Apple apple = new Apple();
apple.setTitle("金帅");
apple.setOrigin("中国");
apple.setColor("金色");
return apple;
}
}
<!-- 利用工厂实例方法获取对象-->
<!-- 先对工厂实例化-->
<bean id="factoryInstance" class="com.test.spring.ioc.factory.AppleFactoryInstance"></bean>
<bean id="apple5" factory-bean="factoryInstance" factory-method="cerateSweetApple"></bean>
总结: 工厂实例化对象 =>如果修改内部工厂代码,不会影响其他任何类的正常运行。
2、基于注解配置Bean
组件扫描 开启:
//不写到某个类,只到包
<context:component-scan base-package="com.test.spring.ioc.entity"></context:component-scan>
放在
类
上面的注解,又叫⇒ 组件类注解。也说明,在xml中需要 开启 组件扫描。
IoC运行时自动为
某个 属性 装配数据
⇒ 自动装配注解
为对象设置一些辅助信息⇒ 元数据注解
3、基于java Config 配置Bean
使用java类 来替代原始xml文件
@Configuration //当前类是一个配置类,用于替代applicationContext.xml
public class Config {
@Bean //Java Config利用方法创建对象,将方法的放回对象放入容器,beanId = 方法名
public UserDao userDao(){
UserDao userDao = new UserDao();
return userDao;
}
}
===== ====== ====== ====== ======
ApplicationContext ac = new ClassPathXmlApplicationContext(Config.class);
二、从IoC容器中获取Bean
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取Bean的方法一(推荐):
Apple sweetApple = ac.getBean("sweetApple",Apple.class);
//获取Bean的方法二:
Apple sweetApple = (Apple) ac.getBean("sweetApple");
----------------------------------------------------------
System.out.println(sweetApple.getTitle());
三、路径匹配表达式
四、基本对象和引用对象的依赖注入
解释:依赖注入 ==> 运行时容器内对象利用反射赋给其他对象的操作(两个对象关联起来)。
4.1、基于setter方法注入对象(常用)
注入基本类型:使用 property + value
<bean id="sweetApple" class="com.test.spring.ioc.entity.Apple">
<property name="title" value="金帅"></property>
<property name="color" value="金色"></property>
<property name="origin" value="中国"></property>
</bean>
注入引用类型:使用 property + ref
<bean id="XXXX" class="com.test.spring.ioc.entity.Child">
<property name="name" value="李雷"></property>
<property name="apple" ref="sweetApple"></property>
</bean>
4.2、基于构造方法注入对象
很类似于,基于构造方法实例化对象,只是遇到,引用类型注入时,value 换成 ref。
<bean id="apple2" class="com.test.spring.ioc.entity.Apple">
<constructor-arg name="name" value="李雷"></constructor-arg>
<constructor-arg name="apple" ref="sweetApple"></constructor-arg>
</bean>
五、注入集合对象
5.1、注入List
//允许重复
<bean id="XXXX" class="XXXX">
<property name="someList">
<list>
<value>具体值</value>
<ref bean="beanId"></ref>
</list>
</property>
</bean>
5.2、注入Set
//自动去重复
<bean id="XXXX" class="XXXX">
<property name="someSet">
<set>
<value>具体值</value>
<ref bean="beanId"></ref>
</set>
</property>
</bean>
5.3、注入Map
<bean id="XXXX" class="XXXX">
<property name="someMap">
<map>
<entry key="k1" value="v1"></entry>
<entry key="k2" value-ref="beanId"></entry>
</map>
</property>
</bean>
5.4、注入Properties
//key 和 value 只能是字符串
<bean id="XXXX" class="XXXX">
<property name="someProperties">
<props>
<prop key="k1">v1</prop>
<prop key="k2">v2</prop>
</props>
</property>
</bean>
六、如何查看容器内对象
获取容器内所有beanId的数组:
String[] beanNames = ac.getBeanDefinitionNames();
for (String beanName : beanNames){
System.out.println(beanName);
//如果还要查看类型
System.out.println("类型:"+ac.getBean(beanName).getClass().getName());
//如果还要查看内容
System.out.println("内容:"+ac.getBean(beanName).toString());
}
七、bean 的 scope属性
scope(范围) 的选择,决定对象何时被创建与作用范围。以及线程安全问题。
用法:
<bean id="c1" class="com.test.spring.ioc.entity.Computer" scope="上面的某一项参数">
</bean>
总结:一般开发中都是使用singleton(单例)的。
八、bean的生命周期
<bean id="company" class="com.test.spring.ioc.entity.Company" init-method="这里写个方法名字">
</bean>
//对应的类中可以创建个方法,和上述方法名字一样,这样在创建对象时,可以先执行该方法。
//释放资源也是同样的方式
IoC容器的销毁
((ClassPathXmlApplicationContext) ac).registerShutdownHook();