1.引入外部属性文件
①可以利用外部属性文件中的类注入容器并且依赖注入(IOC和DI)
jdbc.user=root
jdbc.password=atguigu
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " druidDataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " url" value = " ${jdbc.url}" />
< property name = " driverClassName" value = " ${jdbc.driver}" />
< property name = " username" value = " ${jdbc.user}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
2.Bean的作用域
①框架
②多实例在调用方法时会获得不同的实例
3.Bean的生命周期
①bean声明周期
②bean生命周期调用的方法
public User() {
System.out.println("生命周期:1、创建对象");
}
public void setId(Integer id) {
System.out.println("生命周期:2、依赖注入");
this.id = id;
}
public void initMethod(){
System.out.println("生命周期:3、初始化");
}
4.使用
public void destroyMethod(){
System.out.println("生命周期:5、销毁");
}
需要在 property标签中配置初始化方法 和销毁时调用方法
< bean class = " com.atguigu.bean.User" scope = " prototype" init-method = " initMethod"
destroy-method = " destroyMethod" >
< property name = " id" value = " 1001" > </ property>
< property name = " username" value = " admin" > </ property>
< property name = " password" value = " 123456" > </ property>
< property name = " age" value = " 23" > </ property>
</ bean>
<bean id="myBeanProcessor" class="com.atguigu.spring.process.MyBeanProcessor"/
public class MyBeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("☆☆☆" + beanName + " = " + bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("★★★" + beanName + " = " + bean);
return bean;
}
}
4.factorybean
①作用
正常工厂bean:获得工厂,获得bean factorybean:工厂返回的即为bean,即不用获得工厂可直接获得bean
②工厂bean的使用
public class UserFactoryBean implements FactoryBean< User> {
@Override
public User getObject() throws Exception {
return new User();
}
@Override
public Class< ?> getObjectType() {
return User.class;
}
}
< bean id = " user" class = " com.atguigu.bean.UserFactoryBean" > </ bean>
@Test
public void testUserFactoryBean(){
//获取IOC容器
ApplicationContext ac = new ClassPathXmlApplicationContext("springfactorybean.xml");
User user = (User) ac.getBean("user");
System.out.println(user);
}
5.自动装配功能
①作用
解决对类类型依赖注入时,还需要配置ref引入 可以直接通过自动匹配IOC容器中的类型
②使用
< bean id = " userController" class = " com.atguigu.spring.controller.UserController" >
< property name = " userService" ref = " userService" > </ property>
</ bean>
< bean id = " userService" class = " com.atguigu.spring.service.impl.UserServiceImpl" >
< property name = " userDao" ref = " userDao" > </ property>
</ bean>
< bean id = " service" class = " com.atguigu.spring.service.impl.UserServiceImpl" >
< property name = " userDao" ref = " userDao" > </ property>
</ bean>
< bean id = " userController"
class = " com.atguigu.autowire.xml.controller.UserController" autowire = " byType" >
</ bean>
< bean id = " userService"
class = " com.atguigu.autowire.xml.service.impl.UserServiceImpl" autowire = " byType" >
</ bean>
< bean id = " userDao" class = " com.atguigu.autowire.xml.dao.impl.UserDaoImpl" > </ bean>
③byType和byName 的区别
bytype:根据类型去匹配所需要的依赖 有且只能有一个唯一类型匹配 没找到:不装配,默认为null 找到多个:抛出异常 byname:根据属性的属性名为id去匹配IOC容器中的bean 没找到:不装配,默认为null 找到多个bean不是装配问题,配置错误,id重复