• Spring学习笔记2:Spring HelloWorld


    1:IntelliJ新建Maven工程




    2:pom文件加入Spring依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.test</groupId>
      <artifactId>SpringStack</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>SpringStack</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
        <spring.version>4.3.11.RELEASE</spring.version>
    
    
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>6.11</version>
          <scope>test</scope>
        </dependency>
    
        <!-- 1.Spring核心依赖 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!-- 2.Spring dao依赖 -->
        <!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!-- 3.Spring web依赖 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
      </dependencies>
    </project>
    
    

    3:新建applicationContext.xml

    新建resources文件夹,并在resources文件夹下建立applicationContext.xml文件

    设置resources文件夹Mark as resources
    IntelliJ File->Project Structure

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
        <bean id="HelloNameObject" class="com.test.helloworld.HelloName">
            <property name="name" value="Spring"></property>
        </bean>
    
    </beans>
    

    4:新建HelloName类

    public class HelloName {
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void hello(){
            System.out.println("Hello:"+name);
        }
    
    }
    

    5:test目录加入测试类HelloNameTest

    public class HelloNameTest  {
        @Test
        public void testHelloName() {
            //1:创建Spring IOC容器对象
            //ApplicationContext代表IOC容器
            //ClassPathXmlApplicationContext是ApplicationContext接口的实现类,该实现类从类路径下来加载配置文件
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //2:从IOC容器中获取Bean实例
            //利用id定位IOC容器的Bean
            HelloName nameObject = (HelloName)ctx.getBean("HelloNameObject");
            
            //HelloName nameObject = ctx.getBean("HelloName");
            //利用类型返回IOC容器中的Bean,但要求IOC容器中,必须只能有一个该类型的Bean
            //3:调用hello()方法
            nameObject.hello();
        }
    }
    

    6:项目目录结构

  • 相关阅读:
    继承和派生
    面向过程和面向对象 , 面向对象基础
    numpy模块、matplotlib模块、pandas模块
    包 ,模块(time、datetime、random、hashlib、typing、requests、re)
    os模块 sys模块 json/pickle 模块 logging模块
    python模块基础
    匿名函数 python内置方法(max/min/filter/map/sorted/reduce)面向过程编程
    最简单的注册美区Apple ID方法
    迭代器 生成器 函数递归
    ODBC连接时报错不可识别的数据库格式
  • 原文地址:https://www.cnblogs.com/xidianzxm/p/7588645.html
Copyright © 2020-2023  润新知