手拉手入门Spring6之Ioc注解开发

简介: 组件,控制器,业务、仓库控制器,业务、仓库都是组件的别名@Component@Controller@Service@Repository

组件,控制器,业务、仓库

控制器,业务、仓库都是组件的别名

@Component

@Controller

@Service

@Repository

111.png112.png113.png114.png115.png




     





 


Spring6之Ioc注解的使用

pomxml加入aop的依赖

<!-- 引入Spring context依赖-->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context</artifactId>

           <version>6.0.0-M2</version>

       </dependency>

13.png


SpringConfig.xml主配置文件

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd


14.png15.png


Bean类上使用注解

value是默认的

 

16.png17.png







组件扫描

<!--        组件扫描-->

       <context:component-scan base-package="com.spring.bean"></context:component-scan>

<!--组件扫描多个包-->

<!--        <context:component-scan base-package="com.spring.bean,com.spring.web"></context:component-scan>-->

<!--        <context:component-scan base-package="com.spring"></context:component-scan>-->

测试类

@Test

   public void  AnnotationDemo(){

       ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");

       Object studentBean = applicationContext.getBean("StudentBean");

       Object userBean = applicationContext.getBean("userBean");

       Object orderBean = applicationContext.getBean("OrderBean");

       Object teacherBean = applicationContext.getBean("TeacherBean");

       System.out.println(studentBean);

       System.out.println(userBean);

       System.out.println(orderBean);

       System.out.println(teacherBean);

   }

选择性实例化Bean

<!--    第一种 use-default-filters="false"如果是false表示这个包下所有带有声明的注解全部失效

<context:component-scan base-package="com.Bean" use-default-filters="false"/>

     

-->

   <context:component-scan base-package="com.Bean" use-default-filters="false">

                                               <!-- 想让那个注解生效就选择那个       只有生效Component -->

       <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>

   </context:component-scan>

<!--    第二种:

use-default-filters="ture" 所有带有注解的全部生效,不写use-default-filters,则默认为true

-->

   <context:component-scan base-package="com.Bean" use-default-filters="true">

       <!-- 排除谁不生效 (谁不生效)除了Controller不生效,其他都生效-->

       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

   </context:component-scan>

@Value(注入)注解

SpringConfig.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/contexthttp://www.springframework.org/schema/context/spring-context.xsd">

<!--        组件扫描-->

       <context:component-scan base-package="com.spring.bean"></context:component-scan>

</beans>

//当属性是简单数据类型的时候,可以使用@Value注解进行注入

@Value是用来代替<property name="name" value="张三"/>

11.png

public class Student {

   @Value("张三")

   private String name;

   @Value("20")

   private int age;

--get、set、toString--

}

12.png



@Autowired(注入)注解

@Autowired可以用来注入非简单类型。单独使用@Autowired注解:默认根据类型装配。[默认byType]

@Autowired可以出现在构造方法,方法,参数,属性,别的注解上。

public interface StudentDao {

   void insert();

}

@Repository("StudentDaoImpl")

public class StudentDaoImpl implements StudentDao{

   @Override

   public void insert() {

       System.out.println("StudentDaoImpl");

   }

}

@Service("StudentService")

public class StudentService {

   @Autowired

   StudentDao studentDao;

   public void test(){

       studentDao.insert();

   }

}

//测试类

@Test

public void AnnotationInto(){

   ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");

   StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);

   studentService.test();

}

8.png


@Qualifier(注入)注解

//当有多个实现类对象时,@Autowired和Qualifier联合使用,可以根据名称进行装配

//Qualifier可以指定要执行的实现类对象


7.png


public interface StudentDao {

   void insert();

}

@Repository("StudentDaoImpl")

public class StudentDaoImpl implements StudentDao{

   @Override

   public void insert() {

       System.out.println("StudentDaoImpl");

   }

}

@Repository("StudentDaoImpl2")

public class StudentDaoImpl2 implements StudentDao{

   @Override

   public void insert() {

       System.out.println("StudentDaoImpl2");

   }

}

//无法自动匹配。“studentdao”类型的bean不止一个。由此可见@Autowired不能在多个相同类型的Bean对象中使用。


6.png


@Service("StudentService")

public class StudentService {

   @Autowired

   @Qualifier("StudentDaoImpl2")

   StudentDao studentDao;

   public void test(){

       studentDao.insert();

   }

}

//测试

@Test

public void AnnotationInto(){

   ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");

   StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);

   studentService.test();

}


5.png

@Resource注入注解(推荐)

@Resource注解是JDK扩展包中的,属于JDK的一部分。该注解是标准注解,更加具有通用性。(JSR-250标准中指定的注解类型。JSR是Java规范提案)

@Autowired注解是Spring框架的。

@Resource注解默认根据名称装配byName,未指定name时,使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配。

@Autowired注解默认根据类型装配byType,@Autowired想根据名称装配,需要配合@Qualifier注解一起用。

@Resource注解可以用在属性上,set方法上。

@Autowired注解可以用在属性上,set方法上,构造方法上,构造方法参数上。

@Resource注解是JDK扩展包,不在JDK当中,需要引入依赖。

Spring6不在支持JavaEE,它支持的是JakartaEE9。

Spring6使用这个依赖

Spring6使用这个依赖

       <dependency>

           <groupId>jakarta.annotation</groupId>

           <artifactId>jakarta.annotation-api</artifactId>

           <version>2.1.0</version>

       </dependency>

Spring5使用这个依赖

       <dependency>

           <groupId>jakarta.annotation</groupId>

           <artifactId>jakarta.annotation-api</artifactId>

           <version>1.3.2</version>

       </dependency>


4.png



public interface StudentDao {

   void insert();

}

@Repository("StudentDaoImpl")

public class StudentDaoImpl implements StudentDao{

   @Override

   public void insert() {

       System.out.println("StudentDaoImpl");

   }

}

@Repository("StudentDaoImpl2")

public class StudentDaoImpl2 implements StudentDao{

   @Override

   public void insert() {

       System.out.println("StudentDaoImpl2");

   }

}

@Service("StudentService")

public class StudentService {

   //@Autowired

   //@Qualifier("StudentDaoImpl2")

   @Resource(name ="StudentDaoImpl2")

   StudentDao studentDao;

   public void test(){

       studentDao.insert();

   }

}

//测试

@Test

public void AnnotationInto(){

   ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");

   StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);

   studentService.test();

}


3.png


Java类代替SpringConfig.xml配置文件(全注解开发)


2.png

//创建一个类来代替SpringConfig.xml

@Configuration

//组件扫描

@ComponentScan({"com.spring.bean","com.spring.Dao"})

public class SpringConfig {

}

//测试

@Test

   public void AnnotationInto(){

       ApplicationContext applicationContext =new AnnotationConfigApplicationContext(SpringConfig.class);

       StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);

       studentService.test();

   }

1.png


目录
相关文章
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
9299 129
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
1978 128
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
873 12
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
1093 2
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
898 0
探索Spring Boot的@Conditional注解的上下文配置
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1691 0
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1727 0
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
721 0