• HelloSpring


    HelloSpring

     

    1、导包

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-webmvcartifactId>
    4. <version>5.3.19version>
    5. dependency>

    2、编写代码

    1. public class Hello {
    2. private String str;
    3. public String getStr() {
    4. return str;
    5. }
    6. public void setStr(String str) {
    7. this.str = str;
    8. }
    9. @Override
    10. public String toString() {
    11. return "Hello{" +
    12. "str='" + str + '\'' +
    13. '}';
    14. }
    15. }

     

    3、编写我们的spring文件,这里命名为beans.xml

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

     

    4、测试

    1. public class HelloTest {
    2. @Test
    3. public void test() {
    4. //解析beans.xml文件 , 生成管理相应的Bean对象
    5. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    6. //getBean : 参数即为spring配置文件中bean的id .
    7. Hello helloSpring = (Hello) context.getBean("helloSpring");
    8. System.out.println(helloSpring);
    9. }
    10. }

    思考:

    (1)Hello 对象是谁创建的 ?

            答:hello 对象是由Spring创建的

    (2)Hello 对象的属性是怎么设置的 ?

            答:hello 对象的属性是由Spring容器设置的

    这个过程就叫控制反转

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

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

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

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

     

    spring-01-ioc1

    1、新增一个spring配置文件beans.xml

    • ref:引用Spring容器中创建好的对象
    • value:具体的值,基本数据类型!
    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. https://www.springframework.org/schema/beans/spring-beans.xsd">
    6. <bean id="mysqlImpl" class="com.gt.dao.UserDaoMysqlImpl"/>
    7. <bean id="oracleImpl" class="com.gt.dao.UserOracleImpl"/>
    8. <bean id="SqlserverImpl" class="com.gt.dao.UserDaoSqlserverImpl"/>
    9. <bean id="UserServiceImpl" class="com.gt.service.UserServiceImpl">
    10. <property name="userDao" ref="SqlserverImpl"/>
    11. bean>
    12. beans>

    2、测试

    1. import com.gt.dao.UserDaoImpl;
    2. import com.gt.dao.UserDaoMysqlImpl;
    3. import com.gt.dao.UserDaoSqlserverImpl;
    4. import com.gt.service.UserService;
    5. import com.gt.service.UserServiceImpl;
    6. import org.junit.Test;
    7. import org.springframework.context.ApplicationContext;
    8. import org.springframework.context.support.ClassPathXmlApplicationContext;
    9. public class MyTest {
    10. // @Test
    11. // public void getUser(){
    12. // UserServiceImpl service = new UserServiceImpl();
    13. // service.getUser();
    14. // }
    15. public static void main(String[] args) {
    16. 用户实际调用的是业务层,dao层他们不需要接触!
    17. // UserServiceImpl userService = new UserServiceImpl();
    18. //
    19. // ((UserServiceImpl) userService).setUserDao(new UserDaoSqlserverImpl());
    20. //
    21. // userService.getUser();
    22. // 获取ApplicationContext:拿到Spring的容器
    23. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    24. // 容器在手,天下我有,需要什么,就直接get什么!
    25. UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
    26. userServiceImpl.getUser();
    27. }
    28. }

     格式:

    1. @Test
    2. public void addUser2() {
    3. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    4. UserServiceImpl service = (UserServiceImpl) context.getBean("userServiceImpl");
    5. service.addUser();
    6. }

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

  • 相关阅读:
    Java数据结构之堆(Heap)
    jQuery 在图片和文字中插入内容(多种情况考虑)
    LANG 环境变量
    【集训DAY5】选数字【数学】
    C++ Qt零基础入门进阶与企业级项目实战教程与学习方法分享
    RedisJava基础代码实现
    Oracle 常用命令大全
    为什么说重写是运行时多态?
    【字符串】求解方程 数学
    【图像分类】2018-MobileNetV2
  • 原文地址:https://blog.csdn.net/qq_46423017/article/details/126605787