目录
IOC的主要作用项目管理JavaBean,依靠依赖注入和控制反转进行管理
- <bean class="com.zjy.biz.impl.UserBizImpl2" id="userBiz">bean>
- <bean class="com.zjy.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz">property>
- <bean>
- <bean class="com.zjy.web.OrderAction" id="OrderAction">
-
- <property name="userBiz" ref="userBiz">property>
- <bean>
在案例1中对Spring的xml文件进行建模,建模之后所有的javabean对象都能够加载进Spring容器中的上下文
- public class Demo1 {
-
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
-
-
- OrderAction orderAction = (OrderAction) context.getBean("userAction");
- orderAction.list();
-
- }
依赖注入分为三类:set注入、构造注入和自动装配
- private UserBiz userBiz;
-
- public UserBiz getUserBiz() {
- return userBiz;
- }
-
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
-
- private String name;
- private int age;
- private List<String> hobby;
-
-
-
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public List<String> getHobby() {
- return hobby;
- }
-
- public void setHobby(List<String> hobby) {
- this.hobby = hobby;
- }
在xml文件中配置
- <bean class="com.zjy.web.UserAction" id="userAction">
-
- <property name="userBiz" ref="userBiz">property>
-
- <property name="name" value="zjy">property>
- <property name="age" value="22">property>
- <property name="hobby" >
- <list>
- <value>singvalue>
- <value>dancevalue>
- <value>basketballvalue>
-
- list>
-
- property>
- bean>
- public UserBiz getUserBiz() {
- return userBiz;
- }
-
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
-
- private String name;
- private int age;
- private List<String> hobby;
-
-
-
-
- public OrderAction() {
- // TODO Auto-generated constructor stub
- }
-
-
-
-
- public OrderAction(String name, int age, List<String> hobby) {
- super();
- this.name = name;
- this.age = age;
- this.hobby = hobby;
- }
在xml文件中配置
- <bean class="com.zjy.web.OrderAction" id="OrderAction">
-
- <property name="userBiz" ref="userBiz">property>
- <constructor-arg name="name" value="xiaozhu">constructor-arg>
- <constructor-arg name="age" value="24">constructor-arg>
- <constructor-arg name="hobby" >
-
- <list>
- <value>singvalue>
- <value>dancevalue>
- <value>basketballvalue>
-
- list>
- constructor-arg>
- bean>
byName:是对Spring管理的bean对象的id进行查找,如果找不到则注入失败 反之成功
byType:是对spring管理的bean对象接口实现类进行查找,如果没有或两个以上,则注入失败 反之成功
在未整合之前我们都是通过建模拿到Spring容器的上下文,但是建模十分耗时,并且项目庞大的话我们需要进行多次建模,就更加耗时
2.1 建模必不可少
2.2 建模要保证只执行一次
2.3建模后期再每一个Servlet都能够拿到Spring上下文对象ClasspathXmlApplication
3.1.监听器的初始化方法
3. 2.Spring的上下文要存放在tomcat上下文中
Spring的监听类
- package com.zjy.ioc.listener;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.zjy.web.UserAction;
-
- public class SpringLoadListener implements ServletContextListener{
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
-
- //拿到Spring的上下文
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- //将Spring的上下文保存到tomcat上下文中
- ServletContext servletContext = sce.getServletContext();
- servletContext.setAttribute("springContext",context );
-
- }
- }
Servlet方法
- @WebServlet("/springDemo")
- public class DemoServlet extends HttpServlet{
-
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
-
-
- ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
-
-
- }
运行结果
目前在我们的监听器类中框架文件的名字是定死的,我们需要使我们的框架文件名支持可更改
- <context-param>
-
- <param-name>springConfigLocationparam-name>
- <param-value>/applicationContext.xmlparam-value>
-
- context-param>
- public class SpringLoadListener implements ServletContextListener{
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
-
-
- //拿到Spring的上下文
- ServletContext servletContext = sce.getServletContext();
- String springConfigLocation = servletContext.getInitParameter("springConfigLocation");
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- //将Spring的上下文保存到tomcat上下文中
-
-
- servletContext.setAttribute("springContext",context );
-
- }