1.管理什么(Service和Dao)
2.如何将管理的对象存放到IOC容器(配置applicationContext.xml)第二步
3.将管理的对象存放到IOC容器,如何获取IOC容器 第三步
4.获取到IOC容器后,如何从容器中获取bean(即管理的对象)第四步
5.使用Spring导入坐标(pom.xml)第一步
基本内容
package com.huhu.dao;public interface BookDao {void save();
}
package com.huhu.dao.impl;import com.huhu.dao.BookDao;public class BookDaoImpl implements BookDao {public void save() {System.out.println("Bookdao save ....");}
}
package com.huhu.service;public interface BookService {void save();
}
package com.huhu.service.impl;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;public class BookServiceImpl implements BookService {private BookDao bookDao=new BookDaoImpl();public void save() {System.out.println("BookService save ....");bookDao.save();}
}
第一步:导入坐标
org.springframework spring-context 5.2.10.RELEASE
第二步:配置bean
第三步:获取IOC容器和 第四步:获取bean
package com.huhu;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App2 {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) ctx.getBean("bookDao");bookDao.save();System.out.println();BookService bookService = (BookService) ctx.getBean("bookService");bookService.save();}
}
实现结果:
基本内容与上面差不多,唯一修改的就是BookServiceImpl类
package com.huhu.service.impl;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;public class BookServiceImpl implements BookService {private BookDao bookDao;public void save() {System.out.println("BookService save ....");bookDao.save();}public void setBookDao(BookDao bookDao) {this.bookDao = bookDao;}
}
bean配置 ref中bookDao与bookDaoImpl类注入bean定义id属性值bookDao一致
测试结果依旧不变
获取bean可以使用name属性值也可以id属性值(建议id)
package com.huhu;import com.huhu.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) ctx.getBean("bookDao");BookDao bookDao2 = (BookDao) ctx.getBean("bookDao");System.out.println(bookDao);System.out.println(bookDao2);}
}
修改单例的话在配置文件编辑scope域=prototype
第一种:默认无参构造方法(可不写) (private也可以被调用使用反射)一旦加了有参就必须写上无参构造方法
public class BookDaoImpl implements BookDao {private BookDaoImpl(int i){System.out.println("Bookdao constructor is running...");}public void save() {System.out.println("Bookdao save ....");}
}
没有无参构造方法后,报错Caused by: java.lang.NoSuchMethodException: com.huhu.dao.impl.BookDaoImpl.
第二种:静态工厂 (早期使用最多,了解即可)(基本内容如下)
package com.huhu.dao;public interface OrderDao {void save();
}
package com.huhu.dao.impl;import com.huhu.dao.OrderDao;public class OrderDaoImpl implements OrderDao {public void save() {System.out.println("OrderDao save ...");}
}
package com.huhu.factory;import com.huhu.dao.OrderDao;
import com.huhu.dao.impl.OrderDaoImpl;public class OrderDaoFactory {public static OrderDao getOrderDao(){return new OrderDaoImpl();}
}
package com.huhu;import com.huhu.dao.OrderDao;
import com.huhu.factory.OrderDaoFactory;public class AppForInstanceOrder {public static void main(String[] args) {//通过静态工厂创建对象OrderDao orderDao = OrderDaoFactory.getOrderDao();orderDao.save();}
}
使用静态工厂实例化bean
package com.huhu;import com.huhu.dao.OrderDao;
import com.huhu.factory.OrderDaoFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppForInstanceOrder {public static void main(String[] args) {//通过静态工厂创建对象
// OrderDao orderDao = OrderDaoFactory.getOrderDao();
// orderDao.save();ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");OrderDao orderDao = (OrderDao) ctx.getBean("orderDao");orderDao.save();}
}
第三种:实例工厂 (基本内容如下)
package com.huhu.dao;public interface UserDao {void save();
}
package com.huhu.dao.impl;import com.huhu.dao.UserDao;public class UserDaoImpl implements UserDao {public void save() {System.out.println("UserDao save ...");}
}
package com.huhu.factory;import com.huhu.dao.UserDao;
import com.huhu.dao.impl.UserDaoImpl;public class UserDaoFactory {public UserDao getUserDao(){return new UserDaoImpl();}
}
package com.huhu;import com.huhu.dao.UserDao;
import com.huhu.factory.UserDaoFactory;public class AppForInstanceUser {public static void main(String[] args) {UserDaoFactory userDaoFactory = new UserDaoFactory();UserDao userDao = userDaoFactory.getUserDao();userDao.save();}
}
使用实例工厂实例化bean
package com.huhu;import com.huhu.dao.UserDao;
import com.huhu.factory.UserDaoFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppForInstanceUser {public static void main(String[] args) {
// UserDaoFactory userDaoFactory = new UserDaoFactory();
// UserDao userDao = userDaoFactory.getUserDao();
// userDao.save();ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");UserDao userDao = (UserDao) ctx.getBean("userDao");userDao.save();}
}
优化创建UserDaoFactoryBean类代替UserDaoFactory类(实用)
package com.huhu.factory;import com.huhu.dao.UserDao;
import com.huhu.dao.impl.UserDaoImpl;
import org.springframework.beans.factory.FactoryBean;public class UserDaoFactoryBean implements FactoryBean {//代替原始实例工厂中创建对象的方法public UserDao getObject() throws Exception {return new UserDaoImpl();}//获取对象类型public Class> getObjectType() {return UserDao.class;}
}
暴力关闭容器(实际开发不使用)
package com.huhu.dao.impl;import com.huhu.dao.BookDao;public class BookDaoImpl implements BookDao {public void save() {System.out.println("Bookdao save ....");}public void init(){System.out.println("init ...");}public void destroy(){System.out.println("destroy ...");}
}
ClassPathXmlApplicationContext子类型替代ApplicationContext父类型,才可关闭容器
package com.huhu;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App2 {public static void main(String[] args) {ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) ctx.getBean("dao");bookDao.save();ctx.close();}
}
设置关闭钩子(哪个位置都可以)在虚拟机退出前先关闭容器在退出虚拟机
package com.huhu;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App2 {public static void main(String[] args) {ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) ctx.getBean("dao");bookDao.save();
// ctx.close();ctx.registerShutdownHook();}
}
优化篇实现接口(了解即可)
package com.huhu.service.impl;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {private BookDao bookDao;public void save() {System.out.println("BookService save ....");bookDao.save();}public void setBookDao(BookDao bookDao) {System.out.println("set...");this.bookDao = bookDao;}public void destroy() throws Exception {System.out.println("BookService destroy ...");}public void afterPropertiesSet() throws Exception {System.out.println("BookService init ...");}
}
package com.huhu;import com.huhu.dao.BookDao;
import com.huhu.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App2 {public static void main(String[] args) {ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) ctx.getBean("dao");bookDao.save();
// ctx.close();ctx.registerShutdownHook();}
}
结果如下:
总结:初始化容器
步骤:1创建对象 2执行构造方法 3执行属性注入(set)4执行bean初始化方法
使用bean:执行业务操作
关闭/销毁容器:执行bean销毁方法(容器关闭前触发bean的销毁)