Spring中IOC容器
创始人
2024-03-29 04:50:08
0

IOC入门案例思路分析

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.springframeworkspring-context5.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();}
}

实现结果: 

 

DI 入门案例

基本内容与上面差不多,唯一修改的就是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属性 

获取bean可以使用name属性值也可以id属性值(建议id)

bean作用范围配置默认单例

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

 

bean创建方式

第一种:默认无参构造方法(可不写) (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;}
}

bean生命周期 

暴力关闭容器(实际开发不使用)

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的销毁)

 

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...