广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java之Spring注解开发案例详解
  • 555
分享到

Java之Spring注解开发案例详解

2024-04-02 19:04:59 555人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

在spring4之后,要使用注解开发,必须要保证aop的包导入了 使用注解需要导入context约束,增加注解的支持! <?xml ver

  • 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注解开发内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java之Spring注解开发案例详解

本文链接: https://www.lsjlt.com/news/131226.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java之Spring注解开发案例详解
    在Spring4之后,要使用注解开发,必须要保证aop的包导入了 使用注解需要导入context约束,增加注解的支持! <?xml ver...
    99+
    2022-11-12
  • Java Spring AOP之PointCut案例详解
    目录一、PointCut接口二、ClassFilter接口三、MethodMatcher接口总结一、PointCut接口 package org.springframewor...
    99+
    2022-11-12
  • Java注解之Elasticsearch的案例详解
    学会了技术就要使用,否则很容易忘记,因为自然界压根就不存在什么代码、变量之类的玩意,这都是一些和生活常识格格不入的东西。只能多用多练,形成肌肉记忆才行。 在一次实际的产品开发中,由于...
    99+
    2022-11-13
    Java注解 Elasticsearch Java Elasticsearch
  • Java Spring之@Async原理案例详解
    目录前言一、如何使用@Async二、源码解读总结前言 用过Spring的人多多少少也都用过@Async注解,至于作用嘛,看注解名,大概能猜出来,就是在方法执行的时候进行异步执行。 一...
    99+
    2022-11-12
  • Spring MVC注解式开发案例分析
    这篇文章主要讲解了“Spring MVC注解式开发案例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring MVC注解式开发案例分析”吧!项目案例用 Reque...
    99+
    2023-07-05
  • Java Spring的使用注解开发详解
    目录使用注解开发1.bean2.属性如何注入3.衍生的注解4.自动装配5.作用域6.小结代码show1.新建一个模块:2.新建pojo包及类3.新建dao包及类4.新建service...
    99+
    2022-11-12
  • Java并发之Condition案例详解
    目录一、Condition接口介绍和示例二、Condition接口常用方法三、Condition接口原理简单解析3.1、等待3.2、通知四、总结五、利用Condition实现生产者消...
    99+
    2022-11-12
  • Spring入门到精通之注解开发详解
    目录Spring原始注解DI 依赖注入的注解实现方式Spring新注解@Configuration @ComponentScan @Import@PropertySource @va...
    99+
    2022-11-13
  • 详解Spring注解驱动开发之属性赋值
    一、@Value注解 在Person的属性上使用@Value注解指定注入值 public class Person { @Value("#{20-2}") ...
    99+
    2022-11-12
  • Java Spring @Lazy延迟注入源码案例详解
    前言 有时候我们会在属性注入的时候添加@Lazy注解实现延迟注入,今天咱们通过阅读源码来分析下原因 一、一个简单的小例子 代码如下: @Service public class ...
    99+
    2022-11-12
  • Java之HashMap案例详解
    概述 这篇文章,我们打算探索一下Java集合(Collections)框架中Map接口中HashMap的实现。Map虽然是Collctions框架的一部分,但是Map并没有实现Col...
    99+
    2022-11-12
  • Java之Algorithm_analysis案例详解
    public class BubbleSort { public void sort(int[] array){ for(int i=1;i<...
    99+
    2022-11-12
  • Spring@ComponentScan注解使用案例详细讲解
    目录一、简单介绍二、注解说明1. @ComponentScans注解源码2. @ComponentScan注解源码3. ScopedProxyMode枚举类源码4. FilterTy...
    99+
    2023-03-10
    Spring @ComponentScan注解 Spring @ComponentScan Spring @ComponentScan扫描组件
  • Java Spring-IOC容器与Bean管理之基于注解的方式案例详解
    Spring-IOC容器-Bean管理-基于注解方式 什么是注解? (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…) (2)使用注解,注解作用在类...
    99+
    2022-11-12
  • Spring超详细讲解注解开发
    目录1.使用注解开发1.1.Bean的实现1.2.属性注入1.3.衍生注解1.4.自动装配注解1.5.作用域1.6.小结2.基于Java类进行配置1.使用注解开发 说明 在sprin...
    99+
    2022-11-13
    Spring 注解开发 Spring 注解使用
  • Spring注解之Service详解
    目录 @[TOC](目录) Service注解Service用法及示例传统方式是怎么做的呢?@Service注解是怎么体现业务逻辑复用的? 总结 Service注...
    99+
    2023-08-31
    spring spring boot java
  • Java Spring拦截器案例详解
    springmvc提供了拦截器,类似于过滤器,他将在我们的请求具体出来之前先做检查,有权决定接下来是否继续,对我们的请求进行加工。 拦截器,可以设计多个。 通过实现handlerun...
    99+
    2022-11-12
  • Java Thread之Sleep()案例详解
    一、API简介 Thread.sleep()是Thread类的一个静态方法,使当前线程休眠,进入阻塞状态(暂停执行),如果线程在睡眠状态被中断,将会抛出IterruptedExcep...
    99+
    2022-11-12
  • Java之OutputStreamWriter流案例详解
    一、OutputStreamWriter流     API说明:OutputStreamWriter是从字符流到字节流的桥接:使用指定的字符集将写入其中...
    99+
    2022-11-12
  • 详解spring如何使用注解开发
    在Spring4之后,要使用注解开发,必须要保证aop的包导入了。 使用注解需要导入context约束,增加注解的支持。 <?xml version="1.0" ...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作