Spring框架学习(1)——HelloWorld

1.创建一个名为spring-01-hello的Maven项目,结构如下
Spring框架学习(1)——HelloWorld_第1张图片

2.引入依赖jar包,代码如下

<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.0modelVersion>

  <groupId>com.ycgroupId>
  <artifactId>spring-01-helloartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>spring-01-helloname>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>testscope>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.2.RELEASEversion>
      <scope>runtimescope>
    dependency>
  dependencies>
project>

3.创建实体类Hello.java和Person.java

package com.yc.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("hello") //创建一个Hello这个类的对象,交给spring容器管理,对象名为hello
@Scope("prototype")//原型对象,默认为单例对象
public class Hello {
    private String name="无名氏";
    @Autowired  //自动注入对象
    private Person person;
    public Hello() {
            System.out.println("我是Hello的无参构造方法....");
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    public void sayHello(){
        System.out.println(String.format("%s 对 %s说 你好帅!!", name,person.getName()));
    }
    @Override
    public String toString() {
        return "Hello [name=" + name + "]";
    }
}
package com.yc.spring;

import org.springframework.stereotype.Component;

@Component("person")
public class Person {
        private String name="帅哥";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Person [name=" + name + "]";
        }

}

4.创建Spring配置文件spring.xml,代码如下


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    
    <context:component-scan base-package="com.yc.spring" />
beans>

5.创建测试类HelloTest.java,代码如下

package com.yc.spring;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
    @Test
    public void testIOC() {
        Hello h=new Hello();
        System.out.println(h);
    }

    @Test
    public void testIOC02() {
        ApplicationContext cxt=new ClassPathXmlApplicationContext("spring.xml");
        System.out.println("======================");
        Hello h01=(Hello) cxt.getBean("hello");
        System.out.println(h01);
        h01.sayHello();
        System.out.println("======================");
        Hello h=(Hello) cxt.getBean("hello");
        System.out.println(h);
        h.sayHello();

        if (h01==h) {
            System.out.println("同一对象");
        }else {
            System.out.println("不同对象");
        }
    }

}

你可能感兴趣的:(MVC模式,maven,spring)