2.HelloSpring

Posted zhongjianYuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.HelloSpring相关的知识,希望对你有一定的参考价值。

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 : 具体的值,基本数据类型!
    -->
    
            <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();
    
        
    
    
    

以上是关于2.HelloSpring的主要内容,如果未能解决你的问题,请参考以下文章