首页 > 其他分享 >HelloSpring

HelloSpring

时间:2023-05-07 18:56:16浏览次数:32  
标签:xml 对象 Spring HelloSpring Hello new hello

别提了,学了一会儿黑马的SSM框架,很懵
听了女朋友的建议,改换遇见狂神说的Spring的讲解课程了

跟着狂神说,编写了最初的程序HelloSpring程序

第一步,Maven导入Spring和junit的依赖

第二步,创建pojo文件夹,导入一个实体类作为练习

记得需要鼠标右键,选择ptg Java Bean(可以自动创建下面那一堆)

第三步,使用Spring创建对象,在Spring这些都称为Bean

等价于 类型 变量名称 = new 类型();
Hello hello = new Hello();

  id = 变量名
 class = new 的对象;
property相当于给对象中的属性设置一个值

第四步,书写测试类

1.获取Spring的上下文对象!ClassPathXmlApplicationContext是固定用法,调用.xml文件,我只会用,具体是什么意思我还不太理解

2.我们的对象现在都在Spring中的管理了,我们要使用,直接从里面取出来就可以了

测试类运行截图

在这里放一些代码吧

mytset.java

import org.example.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class mytset {
    public static void main(String[] args) {
        /*获取Spring的上下文对象!*/
        ApplicationContext atc = new ClassPathXmlApplicationContext("Beans.xml");
        //我们的对象现在都在Spring中的管理了,我们要使用,直接从里面取出来就可以了
        Hello hello = (Hello) atc.getBean("hello");
        System.out.println(hello.toString());


    }
}

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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用Spring创建对象,在Spring这些都称为Bean-->
    <!--
        <bean></bean> 等价于 类型 变量名称 = new 类型();
        Hello hello = new Hello();

        id = 变量名
        class = new 的对象;
        property相当于给对象中的属性设置一个值
    -->

        <bean id="hello" class="org.example.pojo.Hello">
        <property name="str" value="spring"></property>

        </bean>

</beans>

标签:xml,对象,Spring,HelloSpring,Hello,new,hello
From: https://www.cnblogs.com/yzx-sir/p/17379791.html

相关文章

  • SpringMVC-lesson04-hellospringmvc-注解开发-2023-03-22
    真实开发-注解开发1、<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XM......
  • SpringMVC-lesson02-hellospringmvc-2023-03-21
    参考网页:https://www.kuangstudy.com/bbs/1618521039124783105第1步:注册web.xml,注册DispatcherServlet<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http:......
  • 2.HelloSpring
    2.HelloSpring思考问题?Hello对象是谁创建的?Hello对象是由Spring设置的Hello对象的属性是怎么设置的?Hello对象的属性是Spring容器设置的这个过程就叫控制反转......
  • 【学习笔记】第一个Spring程序-HelloSpring
    第一个Spring程序-HelloSpring使用Spring来写第一个程序,首先要将spring依赖导入,我们这里导入的是spring-webmvc<dependency>  <groupId>org.springframework</grou......
  • Spring(四):HelloSpring
    上一篇学习了控制反转(IoC)的本质和具体实现方法,这次我们就学习写一个小的项目来体验这个过程。一、项目构建1.Maven依赖(导包)<dependencies><!--https://mvn......