注解开发入门(未完待续)

89 阅读1分钟

Spring注解开发

  1. 本章节主要是加深自己对于使用注解开发的理解,首先你需要在pom.xml导入需要的包
<dependencies>
    <!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
</dependencies>
  1. 我这里创建了接口Isimple和其实现类SimpleImpl类,工程目录结构如下:

image.png Isimple代码如下:

package org.beiqian.service;

public interface ISimple {
    void update();

}

SimpleImpl代码如下:

package org.beiqian.service.Impl;

import org.beiqian.service.ISimple;
import org.springframework.stereotype.Component;
//加入Component注解
@Component
public class SimpleImpl implements ISimple {
    @Override
    public void update() {
        System.out.println("hello");
    }
}
  1. 接下来是重要的步骤,在resource文件下创建相关的xml配置文件,这里我创建的是applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>

    <context:component-scan base-package="org.beiqian.service"> </context:component-scan>

</beans>

xml中的xmlns:context就是命名空间,方便使用标签<context:component-scan >

  1. 编写主方法用来测试
import org.beiqian.service.ISimple;
import org.beiqian.service.Impl.SimpleImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        ISimple bean = xmlApplicationContext.getBean(SimpleImpl.class);
        bean.update();
    }
}

执行结果为:

hello

纯注解开发

下面将上面的步骤进行优化,去除xml文件

  1. 首先删除刚刚的applicationContext.xml文件,然后创建Config相关的class,创建完的目录文件如下: image.png SpringConfig文件内容如下:
package org.beiqian.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("org.beiqian.service")
public class SpringConfig {
}

首先这里@Configuration注解就充当了刚刚的applicationContext.xml文件,而@ComponetScan就是applicationContext中的ConponentScan标签,@ComponentScan里面的内容可以用数组形式来写

  1. 编写主方法用来测试
import org.beiqian.config.SpringConfig;
import org.beiqian.service.ISimple;
import org.beiqian.service.Impl.SimpleImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
    //注意刚刚是XmlApplicationContext这里是Config ApplicationContext
        AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        ISimple bean = configApplicationContext.getBean(ISimple.class);
        bean.update();
    }
}

结果为:

hello

注意:@Component注解衍生的注解

  • @Controller:用于表现层bean定义
  • @Service:用于业务层bean定义
  • @Repository:用于数据层bean定义