IDEA搭建第一个Spring案例,基于Maven实现

Spring框架的主要核心就是DI(依赖注入)和AOP(面向切面编程),用Bean来管理对象,从而实现了松耦合,提高了灵活性,现在对于Spring的学习主要有xml方式和注解方式,可能 注解方式用到的比较多,也比较的方便。下面我们使用xml和注解的方式来写第一个Spring程序!

一、xml的方式

  • 项目引用Maven坐标
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- 面向切面模块 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- 管理Bean -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- 处理上下文的 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>

        <!-- 第三方日志 -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
        </dependency>
        <!-- SpEL表达式 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
    </dependencies>
  • Hello
public class Hello {
    public void hellos(){
        System.out.println("hello Spring");
    }
}
  • applicationContex.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"
       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">
       
	<!--使用Spring中的容器来创建对象-->
    <bean id="hello" class="com.itt.entity.Hello">

    </bean>
</beans>
  • AppTest
public static void main(String[] args) {
      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
      Hello hello = applicationContext.getBean("hello", Hello.class);
      hello.hellos();
}

注 意:

管理配置文件的两个类:

ClassPathXmlApplicationContext:用于加载classPath(类路径、src)下的指定的xml。

FileSystemXmlApplicationContext :用于加载指定盘符下的xml。

  • 运行结果:

在这里插入图片描述

二、注解方式

  • 项目引用Maven坐标
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- 面向切面模块 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- 管理Bean -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- 处理上下文的 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>

        <!-- 第三方日志 -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
        </dependency>
        <!-- SpEL表达式 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
    </dependencies>
  • Person
package com.itt.entity;

public class Person {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
  • MyConfig
//告诉Spring这是一个配置类
@Configuration
public class MyConfig {

    //给容器中注册一个bean并赋值,默认按类名访问(注意:小写类名)
    @Bean
    public Person person(){
        return new Person("zs",18);
    }
}
  • APPTest
public static void main(String[] args) {
   	//AnnotationConfigApplicationContext:注解扫描配置对应的配置类
     ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
     //从容器中取出对应的Bean对象
     Person person = applicationContext.getBean("Person", Person.class);
     System.out.println(person);
}
  • 运行结果

在这里插入图片描述

发布了47 篇原创文章 · 获赞 34 · 访问量 8864

猜你喜欢

转载自blog.csdn.net/weixin_42893085/article/details/105301643