IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring注解开发 -> 正文阅读

[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知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-26 11:56:40  更:2021-07-26 11:59:01 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/25 21:51:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码