2.Spring-HelloSpring
  4URcffAH6ktx 2023年11月01日 47 0

2.HelloSpring

思考问题?

  • Hello对象是谁创建的?

    Hello对象是由Spring设置的

  • Hello 对象的属性是怎么设置的?

    Hello 对象的属性是Spring容器设置的

这个过程就叫控制反转

控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的。

反转:程序本身不创建对象,而变成被动的接受对象。

依赖注入:就是利用set方法来进行注入的。

IOC是一种编程思想,由主动的编程变成被动的接收。

可以通过ClassPathXmlApplicationContext去浏览一下底层源码。

OK,到了现在,我们彻底不用再回程序中改动了,要是现任不同的操作,只需要再xml配置文件中进行修改,所谓的IOC,一句话搞定:对象由Spring来创建,管理,装配!

1、由项目一一探究

  • 创建了一下新的maven包:spring-02-hellospring

    • 在main的java创建一个Hellojavaclass文件

      package com.jan.pojo;
      
      public class Hello {
          //很简单的实体类
          private String str;
      
          public  String getStr(){
              return str;
          }
      
          public void setStr(String str) {
              this.str = str;
          }
      
          @Override
          public String toString() {
              return "Hello{" +
                      "str='" + str + '\'' +
                      '}';
          }
      }
      
  • resources

    • 创建beans.xml FIeld

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd">
      
      
      <!--使用spring来创建对象,在spring中这些都称为Bean
          类型 变量名 = new 类型();
          Hello hello = new Hello
      
          id=变量名
          class = new 的对象
          property 相当于给对象中的属性设置一个值!
          -->852
      
      <bean id="hello" class="com.jan.pojo.Hello">
          <property name="str" value="Sprin+g">
          </property>
      </bean>
      
      
      
      </beans>
      
  • test的java

    • 创建名为 MyTest 的 javaclass

      import com.jan.pojo.Hello;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class MyTest {
          public static void main(String[] args) {
      
              //获取spring的上下文对象!
              ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      
              //我们的对象现在都在Spring中管理了,我们要使用直接去里面取出来就可以了!
      
              //   context.getBean("hello")-- Hello hello = context.getBean("hello")
              Hello hello = (Hello) context.getBean("hello");
              System.out.println(hello.toString());
      
          }
      }
      

2、项目二:Spring开始的maven 进行控制反转

  • 所有的UserDao、UserDaoImpl、UserDaoMysqlImpl、UserDaoOracleImpl、UserDaoSqlserverImpl、UserService、UserServiceImpl均无变化

  • 配置beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <bean id="mysqlImpl" class="com.jan.dao.UserDaoMysqlImpl"/>
        <bean id="oracleImpl" class="com.jan.dao.UserDaoOracleImpl"/>
        <bean id="sqlserverImpl" class="com.jan.dao.UserDaoSqlserverImpl"/>
    
        <bean id="UserServiceImpl" class="com.jan.dao.service.UserServiceImpl">
    <!--
        ref : 引用spring容器中创建好的对象
        value : 具体的值,基本数据类型!
    -->
    
    
      <!--
        public class UserServiceImpl implements UserService{
      private UserDao userDao;
                               }
    -->
            <property name="userDao" ref="sqlserverImpl"></property>
            <!-- 只需修改 ref 里面的值就可以了
             <property name="userDao" ref="mysqlImpl"></property>
             -->
        </bean>
    
    </beans>
    
  • MyTest 的修改

    import com.jan.dao.UserDaoImpl;
    import com.jan.dao.UserDaoSqlserverImpl;
    import com.jan.dao.service.UserService;
    import com.jan.dao.service.UserServiceImpl;
    import com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        public static void main(String[] args) {
    
            /*  原来之前的调用
            UserService userService = new UserServiceImpl();
            用户实际调用的是业务层,dao层它们不需要接触!
            ((UserServiceImpl) userService).setUserDao(new UserDaoImpl());//userService.setUserDao
    
            userService.getUser();
            */
           
            //去获取ApplicationContext:拿到Spring的容器
          ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            //容器在手,天下我有,就直接get谁!
    
            UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
            userServiceImpl.getUser();
    
        }
    }
    
    
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   53   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   107   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
4URcffAH6ktx
作者其他文章 更多

2023-11-01

2023-11-01

2023-11-01

2023-11-01

2023-11-01