Java之Spring注解开发案例详解

 更新时间:2021年07月27日 11:21:12   作者:雪潇潇  
这篇文章主要介绍了Java之Spring注解开发案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
  • 在Spring4之后,要使用注解开发,必须要保证aop的包导入了

在这里插入图片描述

  • 使用注解需要导入context约束,增加注解的支持!
<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.example.springannotation" />
    <context:annotation-config></context:annotation-config>
    <!-- <bean id="cat" class="com.example.springannotation.dao.Cat"/>
    <bean id="people" class="com.example.springannotation.dao.People"/>-->
</beans>

注解的支持:

//@Component 等价于<bean id="pepople" class="com.example.springannotation.dao.People" />
@Component
public class People {
    @Autowired(required = false)
    @Value("1235") //相当<property name="id " value="1235"/>
    private int id;
    @Autowired(required = false)
    private String name ="ming";
    
    @Value("qing") //相当<property name="name " value="qing"/>
    public void setName(@Nullable String name) {
        this.name = name;
    }
}

衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

  • dao 【@Repository】
  • service【@Service】
  • controller【@Controler】

这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean。

@Scope("singleton") //singleton:标识单例模式,prototype:标识原型模式 、request:标识请求模式、session:标识会话模式

xml 与注解:

  • xml更加万能,适用于任何场合!维护简单方便。注解不是自己类使用不了,维护相对复杂!

xml与注解最佳实践:

  • xml 用来管理bean;
  • 注解只负责完成属性的注入;
  • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.example.springannotation" />
    <context:annotation-config></context:annotation-config>

JAVA的方式配置Spring

  • @Configuration 这个也会spring容器托管,注册到容器中,因为他本来就是一个Component,
  • @Configuration代表这是一个配置类,就和我们之前看的beans.xml
// 配置类 代替 beans.xml
import com.example.springannotation.dao.Cat;
import com.example.springannotation.dao.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ComponentScan("com.example.springannotation")
@Import(WwConfig.class)  //引入第二个配置
public class AppConfig {
    //注朋一个bean 相当于当于我们之前写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性
    //这个方法的返回价,就和当了bean标签中的class属性
    @Bean
    public People getPeople(){
        return new People();
    }

    @Bean
    public Cat getCat(){
        return new Cat();
    }
}

import org.springframework.context.annotation.Configuration;
@Configuration
public class WwConfig {
}
//测试类
import com.example.springannotation.config.AppConfig;
import com.example.springannotation.dao.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootTest
class SpringannotationApplicationTests {
    @Test
    void contextLoads() {
        如果完全使用了配置类方式做,
        // 我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        People people = (People) context.getBean("getPeople");
        System.out.println(people.toString());
    }
}

到此这篇关于Java之Spring注解开发案例详解的文章就介绍到这了,更多相关Java之Spring注解开发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Jenkins的安装配置详解

    Jenkins的安装配置详解

    这篇文章主要介绍了Jenkins的安装配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 一文掌握IDEA中的Maven集成与创建

    一文掌握IDEA中的Maven集成与创建

    maven是用来帮助我们快速搭建项目结构与开发环境的好工具,这篇文章主要介绍了一文掌握IDEA中的Maven集成与创建,需要的朋友可以参考下
    2023-02-02
  • Spring Initializr中生成的mvnw有什么用

    Spring Initializr中生成的mvnw有什么用

    这篇文章主要介绍了Spring Initializr中生成的mvnw有什么用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • springboot自定义日志注解的实现

    springboot自定义日志注解的实现

    本文主要介绍了springboot自定义日志注解的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Spring MVC处理参数中的枚举类型通用实现方法

    Spring MVC处理参数中的枚举类型通用实现方法

    这篇文章主要给大家介绍了关于Spring MVC处理参数中的枚举类型通用实现方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2018-11-11
  • 关于Mybatis-plus设置字段为空的正确写法

    关于Mybatis-plus设置字段为空的正确写法

    这篇文章主要介绍了关于Mybatis-plus设置字段为空的正确写法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java基于Dijkstra算法实现校园导游程序

    Java基于Dijkstra算法实现校园导游程序

    这篇文章主要为大家详细介绍了Java基于Dijkstra算法实现校园导游程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • spring中@Autowired自动注入依赖项的使用

    spring中@Autowired自动注入依赖项的使用

    当使用@Autowired注解时,它可以自动注入依赖项,例如其他类的实例,本文就来详细的介绍一下,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • springbooot使用google验证码的功能实现

    springbooot使用google验证码的功能实现

    这篇文章主要介绍了springbooot使用google验证码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • java利用多线程和Socket实现猜拳游戏

    java利用多线程和Socket实现猜拳游戏

    这篇文章主要为大家详细介绍了java利用多线程和Socket实现猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08

最新评论