手拉手入门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


目录
相关文章
|
1月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
135 26
|
2月前
|
缓存 Java 数据库
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
211 89
|
1月前
|
监控 Java Spring
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
67 16
|
7天前
|
存储 监控 数据可视化
SaaS云计算技术的智慧工地源码,基于Java+Spring Cloud框架开发
智慧工地源码基于微服务+Java+Spring Cloud +UniApp +MySql架构,利用传感器、监控摄像头、AI、大数据等技术,实现施工现场的实时监测、数据分析与智能决策。平台涵盖人员、车辆、视频监控、施工质量、设备、环境和能耗管理七大维度,提供可视化管理、智能化报警、移动智能办公及分布计算存储等功能,全面提升工地的安全性、效率和质量。
|
2月前
|
监控 Java 应用服务中间件
SpringBoot是如何简化Spring开发的,以及SpringBoot的特性以及源码分析
Spring Boot 通过简化配置、自动配置和嵌入式服务器等特性,大大简化了 Spring 应用的开发过程。它通过提供一系列 `starter` 依赖和开箱即用的默认配置,使开发者能够更专注于业务逻辑而非繁琐的配置。Spring Boot 的自动配置机制和强大的 Actuator 功能进一步提升了开发效率和应用的可维护性。通过对其源码的分析,可以更深入地理解其内部工作机制,从而更好地利用其特性进行开发。
59 6
|
2月前
|
人工智能 Java API
阿里云工程师跟通义灵码结伴编程, 用Spring AI Alibaba来开发 AI 答疑助手
本次分享的主题是阿里云工程师跟通义灵码结伴编程, 用Spring AI Alibaba来开发 AI 答疑助手,由阿里云两位工程师分享。
112 0
阿里云工程师跟通义灵码结伴编程, 用Spring AI Alibaba来开发 AI 答疑助手
|
2月前
|
监控 JavaScript 数据可视化
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
113 7
|
7月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
3月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
|
9月前
|
XML Java 数据格式
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
62 1