目录
1 bean配置
1.1bean基础配置
1.2 bean别名配置
1.3 bean作用范围配置
public class App {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean(根据bean配置id获取)
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
BookDao bookDao2 = (BookDao) ctx.getBean("bookDao");
//观察默认为单例模式,两次造的是同一个对象
System.out.println(bookDao);//com.dao.impl.BookDaoImpl@29ca901e
System.out.println(bookDao2);//com.dao.impl.BookDaoImpl@29ca901e
}
}
2 bean实例化
2.1 构造方法
Spring创建Bean调用的是无参构造,私有的无参构造也可以调用
2.2静态工厂
管理用静态工厂造出来的Bean
<!--静态工厂配置-->
<bean id="bookDao" class="com.factory.BookDaoFactory" factory-method="getBookDao"/>
public interface BookDao {
public void save();
}
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl save()");
}
}
public class BookDaoFactory {
public static BookDao getBookDao(){
System.out.println("BookDaoFactory getBookDao()");
return new BookDaoImpl();
}
}
//使用静态工厂
public class App2 {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext2.xml");
//使用工厂类实例化
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
//BookDaoFactory getBookDao()
//BookDaoImpl save()
}
}
2.3实例工厂
管理用实例工厂造出来的Bean
<!--实例工厂配置-->
<!--先把工厂类实例造出来-->
<bean id="bookDaoFactory2" class="com.factory.BookDaoFactory2"/>
<!--再用工厂类实例把实体类zaochul-->
<bean id="bookDao" factory-bean="bookDaoFactory2" factory-method="getBookDao"/>
//实例工厂
public class BookDaoFactory2 {
public BookDao getBookDao() {
System.out.println("BookDaoFactory2 getBookDao()");
return new BookDaoImpl();
}
}
//使用实例工厂
public class App3 {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext3.xml");
//使用实例化工厂获取对象
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
//BookDaoFactory2 getBookDao()
//BookDaoImpl save()
}
}
2.4优化版的实例工厂 常用!!!!!
public class BookFactoryBean implements FactoryBean {
public Object getObject() throws Exception {
System.out.println("BookFactoryBean getObject()");
return new BookDaoImpl();
}
public Class<?> getObjectType() {
return BookDao.class;
}
public boolean isSingleton() {
return true;//true单例 false非单例
}
}
//使用FactoryBean
public class App4 {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext4.xml");
//使用实例化工厂获取对象
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
//BookFactoryBean getObject()
//BookDaoImpl save()
}
}
<!--FactoryBean配置,不用指定方法名了-->
<bean id="bookDao" class="com.factory.BookFactoryBean"/>
3.Bean的生命周期控制
3.1Bean的生命周期
3.2Bean销毁时机
3.2 生命周期控制
方式1:手动写方法,需要配置
<!--Bean生命周期控制-->
<bean id="bookDao" class="com.dao.impl.BookDaoImpl" init-method="init" destroy-method="destory"/>
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl save()");
}
public void init() {
System.out.println("init...");
}
public void destory() {
System.out.println("destory...");
}
}
public class App5 {
public static void main(String[] args) {
//获取IoC容器 ApplicationContext里面没有close()方法
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext5.xml");
//注册容器关闭钩子
ctx.registerShutdownHook();//可以在任何位置
//使用实例化工厂获取对象
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
// ctx.close();//暴力的关闭容器的方法
//控制台输出
//init...
//BookDaoImpl save()
//destory... 只有关闭容器才能看到destory...
}
}
方式2:接口,不用配置
public class BookServiceImpl implements BookService,InitializingBean,DisposableBean {
//不使用new的方式创建的dao对象,提供对应的set方法
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
System.out.println("BookServiceImpl save()");
bookDao.save();
}
public void destroy() throws Exception {
System.out.println("BookServiceImpl destroy()");
}
public void afterPropertiesSet() throws Exception {
System.out.println("BookServiceImpl afterPropertiesSet()");
}
}
public class App6 {
public static void main(String[] args) {
//获取IoC容器 ApplicationContext里面没有close()方法
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//注册容器关闭钩子
ctx.registerShutdownHook();//可以在任何位置
//使用实例化工厂获取对象
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.save();
//控制台输出
//BookServiceImpl afterPropertiesSet()
//BookServiceImpl save()
//BookDaoImpl save()
//BookServiceImpl destroy()
}
}
<bean id="bookDao" class="com.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
</bean>
4 容器
public class App {
public static void main(String[] args) {
//1.获取IoC容器
//方式1:加载类路径下的配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// //方式2:从文件系统下加载配置文件
// ApplicationContext ctx2 = new FileSystemXmlApplicationContext("E:\\java\\01_WorkSpace\\mytest2\\Spring\\contain-demo\\src\\main\\resources\\applicationContext.xml");
//获取Bean对象
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
BookDao bookDao2 = ctx.getBean("bookDao", BookDao.class);
BookDao bookDao3 = ctx.getBean(BookDao.class);//这个类型的Bean对象只能有一个
bookDao.print();
}
}