Spring注解注入

给大一新生的蓝桥杯EDA备赛规划:从电路分析到立创EDA,如何用一年时间高效备赛? 本文为大一新生提供了一份系统的蓝桥杯EDA备赛规划,从电路分析基础到立创EDA实战操作,分阶段指导如何高效备赛。内容涵盖理论学习、软件操作、PCB设计专项提升及冲刺优化策略,帮助学生在一年内全面提升电子设计能力,为竞赛做好充分准备。 阅读详情

一:注释的方法注入

   1.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-2.5.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">                 
     <context:annotation-config/>       
</beans> 

二:@Autowired或 @Resource注释方式进行装配

    @Autowired是默认按类型装配。

    @Resource是默认按名称装配,找不到与名称匹配的bean时,再按类型装配。

 

     (1).@Resource: JDK提供,移植性强,推荐使用, 可以看到支撑类是: import javax.annotation.Resource; 只要是JDK1.5以上版本,JDK内置了此注释支撑类.

     (2).用法:

     用在属性上

     示例:

   //dao层接口:

package com.spring.chapter5.dao;  
  
public interface FruitDao {  
    public void create();  
} 

dao层实现类  

    package com.spring.chapter5.dao.impl;  
      
    import com.spring.chapter5.dao.FruitDao;  
      
    public class FruitDaoImpl implements FruitDao {  
        public void create() {  
            System.out.println("Dao层方法create被调用");  
        }  
    }  

service层接口  

package com.spring.chapter5.service;  
  
public interface FruitService {  
    public void create();  
} 

service层实现类

package com.spring.chapter5.service.impl;  
  
import javax.annotation.Resource;  
import com.spring.chapter5.dao.FruitDao;  
import com.spring.chapter5.service.FruitService;  
  
public class FruitServiceImpl implements FruitService {  
    @Resource  
    private FruitDao fruitDao;  
  
    public void create() {  
        fruitDao.create();  
    }  
  
    public void setFruitDao(FruitDao fruitDao) {  
        this.fruitDao = fruitDao;  
    }  
} 

配置文件(chapter5.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-2.5.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    <context:annotation-config />  
    <bean id="fruitDao" class="com.spring.chapter5.dao.impl.FruitDaoImpl" />  
    <bean id="fruitService" class="com.spring.chapter5.service.impl.FruitServiceImpl" />  
</beans>

     注意:我们只在xml中配置了两个bean对象,它们之间并没有关系,但是在Java中我们采用注释语法,这个时候@Resource注释语法会根据属性名称在spring容器中去寻找和属性名称同名id的bean对象,然后将其注入到属性中。

   //测试类  

    package com.spring.chapter5.service.impl;  
      
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
    import com.spring.chapter5.service.FruitService;  
      
    public class Test {  
        public static void main(String[] args) {  
            ApplicationContext act = new ClassPathXmlApplicationContext("chapter5.xml");  
            FruitService fruitService = (FruitService) act.getBean("fruitService");  
            fruitService.create();  
        }  
    }  

  (3).更改配置文件

      将<bean id="fruitDao" class="com.spring.chapter5.dao.impl.FruitDaoImpl" />改为<bean id="myFruitDao" class="com.spring.chapter5.dao.impl.FruitDaoImpl" />

      这个时候可以看到myFruitDao和属性名称并没有对应,但执行Test文件后,可以看到执行成功。这因为如果名称上没有对应,就会按类型自动对性,所以会执行成功。

 

  (4).我们还可以给@Resource加上name,请看下面的语法

   //service层实现类

    public class FruitServiceImpl implements FruitService {  
        @Resource(name="myFruitDao")  
        private FruitDao fruitDao;  
      
        public void create() {  
            fruitDao.create();  
        }  
      
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }  
    }  

   再次执行Test,同样可以执行成功.

 

   (5).用上属性的set方法上

    //service层实现类    

    public class FruitServiceImpl implements FruitService {   
        private FruitDao fruitDao;  
      
        public void create() {  
            fruitDao.create();  
        }  
      
        @Resource(name="myFruitDao")  
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }  
    }  

   可以看到,用在属性上,或者用上属性的set方法上,效果是一模一样的,没有任何区别.

 

三:@Autowired

   //service层实现类  

    public class FruitServiceImpl implements FruitService {   
        private FruitDao fruitDao;  
      
        public void create() {  
            fruitDao.create();  
        }  
      
        @Autowired  
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }  
    }  

  运行Test文件,可以看到效果一样

   同样,我们也可以把@Autowired改成按名称装配

   //service层实现类  

    public class FruitServiceImpl implements FruitService {  
        @Autowired()  
        @Qualifier("myFruitDao")  
        private FruitDao fruitDao;  
              
        public void create() {  
            fruitDao.create();  
        }  
      
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }  
    }  


   这里需要注意的是:

   @Autowired()
   @Qualifier("myaFruitDao")
   private FruitDao fruitDao;
   这两句只能用在属性上,不能用上方法上.

 

   @Autowired的required属性

   @Autowired(required=true)
   @Qualifier("myFruitDao")
   required=true : 必须为属性注入值,
   required=false: 不是必须注入值,如果找不到对应的值注入,会注入null

 

四:通过在classpath自动扫描方式把组件纳入到spring容器中管理.

    Spring2.5引入了组件自动扫描机制,他可以在类路径下寻找标注了@Component, @Service, @Controller, @Repository注解的类, 并把这些类纳入进Spring容器中管理,它的作用,和在XML文件中使用Bean节点配置组件是一样的,要使用自动扫描机制。  

    <?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-2.5.xsd  
               http://www.springframework.org/schema/context  
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
        <context:component-scan base-package="com.spring.chapter5" />  
    </beans>  

<context:component-scan base-package="com.spring.chapter5" />这一项配置了需要扫描的包,包含子包.

    @Service: 用于标注业务层的类
    @Controller: 用于标注控制层,如Action
    @Repository: 用于标注Dao层
    @Component: 用于标注组件层, 当组件不好归类的时候,可以用此标注

    通过扫描机制, 基本上可以达到配置文件基本为空的效果, 这是Spring2.5新推出的功能, 老系统不能运用, 但是新系统中, 此功能运用的非常多。

    示例:

   dao层实现类  

package com.spring.chapter5.dao.impl;  
  
import org.springframework.stereotype.Repository;  
import com.spring.chapter5.dao.FruitDao;  
  
@Repository  
public class FruitDaoImpl implements FruitDao{    
    public void create() {  
        System.out.println("Dao层方法create被调用");  
    }         
} 

service层实现类  

    package com.spring.chapter5.service.impl;  
      
    import org.springframework.stereotype.Service;  
    import com.spring.chapter5.dao.FruitDao;  
    import com.spring.chapter5.service.FruitService;  
      
    @Service  
    public class FruitServiceImpl implements FruitService {  
        private FruitDao fruitDao;  
          
        public void create() {  
            fruitDao.create();  
        }  
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }     
    }  

    这个时候,我将Dao层,服务层的两个实现类交给Spring管理了,相当于以前配置了两个Bean,这个时候我们可以验证一下,我们是否可以从容器中获取对象。

   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-2.5.xsd  
               http://www.springframework.org/schema/context  
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
        <context:component-scan base-package="com.spring.chapter5" />  
    </beans>  

测试类  

    package com.spring.chapter5.service.impl;  
      
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
    import com.spring.chapter5.dao.FruitDao;  
    import com.spring.chapter5.service.FruitService;  
      
    public class Test {  
        public static void main(String[] args) {  
            ApplicationContext acx = new ClassPathXmlApplicationContext(  
                    "chapter5.xml");  
            FruitDao fruitDao = (FruitDao) acx.getBean("fruitDaoImpl");  
            FruitService fruitService = (FruitService) acx  
                    .getBean("fruitServiceImpl");  
            System.out.println(fruitDao);  
            System.out.println(fruitService);  
        }  
    }  

     这里需要注意的是,当对象交给Spring管理后,Spring给对象起的ID名称是类的简单名称, 即类全名,但是首字母小写。

 

   自定义名称:

   dao层实现类  

    package com.spring.chapter5.dao.impl;  
      
    import org.springframework.stereotype.Repository;  
    import com.spring.chapter5.dao.FruitDao;  
      
    @Repository("fruitDao")  
    public class FruitDaoImpl implements FruitDao {  
        public void create() {  
            System.out.println("Dao层方法create被调用");  
        }  
    }  

service层实现类  

    package com.spring.chapter5.service.impl;  
      
    import org.springframework.stereotype.Service;  
    import com.spring.chapter5.dao.FruitDao;  
    import com.spring.chapter5.service.FruitService;  
      
    @Service("fruitService")  
    public class FruitServiceImpl implements FruitService {  
          
        private FruitDao fruitDao;  
      
        public void create() {  
            fruitDao.create();  
        }  
      
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }  
    }  

测试类  

    package com.spring.chapter5.service.impl;  
      
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
    import com.spring.chapter5.dao.FruitDao;  
    import com.spring.chapter5.service.FruitService;  
      
    public class Test {  
        public static void main(String[] args) {  
            ApplicationContext acx = new ClassPathXmlApplicationContext(  
                    "chapter2.xml");  
            FruitDao fruitDao = (FruitDao)acx.getBean("fruitDao");  
            FruitService fruitService = (FruitService) acx.getBean("fruitService");  
            System.out.println(fruitDao);  
            System.out.println(fruitService);  
        }  
    }  

五:改变对象的范围(单例或非单例)

    @Repository("fruitDao")
    @Scope("prototype")
    加上@Scope("prototype")此注释即可,这个时候每次获取对象都会是一个新对象

 

六:对象初始化方法和销毁方法的配置   

    @Repository("fruitDao")  
    @Scope("prototype")  
    public class FruitDaoImpl implements FruitDao {  
        public void create() {  
            System.out.println("Dao层方法create被调用");  
        }  
          
        @PostConstruct  
        public void init(){  
            System.out.println("调用init");  
        }  
          
        @PreDestroy  
        public void destory(){  
            System.out.println("调用destory");  
        }  
    }  

   如果对象是非单例模式,这个时候可以看到init方法会调用两次,而destroy方法不会调用.

  将Dao层注入到服务层:  


    @Service("fruitService")  
    public class FruitServiceImpl implements FruitService {  
        @Resource(name="fruitDao")  
        private FruitDao fruitDao;  
      
        public void create() {  
            fruitDao.create();  
        }  
      
        public void setFruitDao(FruitDao fruitDao) {  
            this.fruitDao = fruitDao;  
        }  
    }  


03 Token、Embedding、上下文窗口:开发者必须懂的三个基本概念 Token、Embedding 和上下文窗口是大模型应用开发的三个基础概念。本文从成本、速度、检索和上下文设计四个角度讲清它们的工程意义,帮助开发者理解为什么同一段内容会超限、变贵,为什么 RAG 能做语义检索。 阅读详情

相关推荐

等保: Postgresql配置ssl链接

等保: postgresql配置ssl链接完整方案和避坑指南

weixin_43557605的博客 4383

第五章 注释的注入

一:注释的方法注入    1.XML配置文件的改动:     &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc...

x125521853的专栏 229

abaqus 根据坐标施加载荷- Analytical Field 载荷映射

在一些类型的分析中,需要将按位置分布的载荷施加到结构上,比如流体分析导出了结构的气动载荷、或者温度载荷,需要将压强分布、温度分布载荷施加到固体上。这种分析可以使用abaqus中的Analytical Field实现。 1 例子 1.1 结构 一个100*100mm的面,面的载荷分布函数为p=(x2+y2)0.5/((20.5)*100) MPa,即(0, 0)的压强为0,(100, 100)的压强为1。 1.2 载荷 载荷函数:p=(x^2+y^2)^0.5/((2^0.5)*100) in exce.

shengyutou的博客 2万+

Spring使用注解进行注入

Spring使用注解进行注入的几种方式

x2656271356的博客 1616

CASS3D实战:高效三维房屋与地形采集绘制技巧

本文详细介绍了CASS3D在三维房屋与地形采集绘制中的高效技巧,包括智能绘房功能、不规则结构处理、特殊构件快速绘制及地形数据采集策略。通过实战案例和进阶操作指南,帮助用户快速掌握CASS3D的核心功能,提升三维采集与绘制效率。

weixin_29216049的博客 872

2025ICPC南昌邀请赛题解

2025ICPC南昌邀请赛由XDU和WHU联合出题的ICPC南昌邀请赛,榜单显示8题金、6题银、5题铜。

weixin_50089904的博客 1960

Spring依赖注入(注解方式)

Spring依赖注入(注解方式) 在Spring中,尽管使用XML配置文件就可以实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给程序的维护与升级带来一定的困难。 Java从JDK5.0以后,提供了Annotation(注解)功能,Spring也提供了对注解技术的全面支持。 一,Spring中注入常用的注解如下: 1,@Component 可以使用此注解描述Spring中的Bean,它是一个泛化的概念,表示一个组件(Bean),可以作用在任何层次。使用时只需要将该注

淮宁湾的博客 1万+

【2026年米哈游春招 - 3月14日 -第一题- 行列相等】(题目+思路+Java&C++&Python解析+在线测试)

摘要 本文解决了一个矩阵行列和匹配问题:统计矩阵中行和等于列和的所有行列对数量。算法首先计算每行和每列的元素和,然后利用哈希表统计各行列和的出现次数。通过遍历列和并查询对应的行和出现次数,高效计算出匹配对数。该方法时间复杂度为O(nm),空间复杂度为O(n+m),适用于大规模矩阵数据。代码实现了Python、Java和C++版本,通过哈希表优化避免了暴力枚举,显著提高了计算效率。

m0_73659489的博客 83

spring注解注入详解

1、Spring配置 <?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...

Big Data 3494

利用自己的域名和cloudflare实现邮件转发(无限邮箱注册cursor使用账号) 保姆级图文

大家喜欢的话,给个👍,点个关注!给大家分享更多计算机专业学生的求学之路!发现你走远了@mzh原创作品,转载必须标注原文链接欢迎关注『发现你走远了』 专栏,持续更新中欢迎关注『发现你走远了』 专栏,持续更新中『未完待续』

MZH 415

AI人工智能 Agent:在仓储物流中的应用

AI人工智能 Agent:在仓储物流中的应用 1. 背景介绍 1.1 问题的由来 仓储物流行业是全球经济运行的基石之一,涉及商品的存储、运输、分拣和配送等多个环节。随着电子商务的普及和消费者对物流速度和精确性的要求不断提高,仓储物流

AGI×大数据,开启智能时代的认知跃迁;解码AGI,赋能数据驱动的智能革命。 864

Spring中通过注解来配置bean以及自动注入

今天看到一篇好文章,写的很是详细。再加上自己的理解和补充,成了这一篇文章。文后会献上原文链接。 使用Spring经常性的需要: 通过注解配置bean   基于注解配置bean   基于注解来配置bean的属性    -------------------------------今天就来看看是怎么实现的------------------------------------------------...

默默的笔记 1万+

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Spring的注解注入

Spring的注解注入 一、注解 1.代码种的特殊标记,使用注解可以完成功能 2.注解写法:@注解名称(属性名称=属性值) 3.注解可以使用在类、方法、属性上面 二、Spring注解开发准备 1.导入jar包 (1)导入基本的jar包 (2)导入aop的jar包 2.创建类,创建方法 public class User { public void add(){ System.out.p...

秒怂_z 1625

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Spring之注解注入bean

1.引入jar包。配置log4j.properties文件 2.创建Person类 package com.hy.spring.pojo; import org.springframework.stereotype.Component; //使用component的注解方式注入 //@Component(&quot;person&quot;),等同于使用依赖注入方式,配置的&amp;lt;bean name=&quot;pers...

elinice 1万+

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Spring 属性注入和注解自动注入

Spring属性 xml文档注入和注解自动注入

m0_52325708的博客 2715

Spring注入注解

Spring和注入相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。

inexaustible的博客 1590

Spring-注解注入

Spring-注解注入

m0_71212413的博客 1288

spring:setter注入构造器注入注解注入

一。内容回顾 简述对spring框架的了解 优点 高内聚低耦合 可拔插式设计模式 XML配置 便于维护 Sping 可以和各大优秀的框架很好结合(Struts2 springmvc mybatis hibernate) Spring 一站式开发(springmvc spring JdbcTemplate) 底层原理:反射,代理机制 搭建spring框架步骤 1.导入jar包 2.创建spring...

java_dingbg的博客 3959

spring基础——注解注入

Spring 针对 Bean 管理中创建对象提供注解 (1)@Component (2)@Service (3)@Controller (4)@Repository 配置文件 1、注解注入要先引入context名称空间 xmlns:context=“http://www.springframework.org/schema/context” http://www.springframework.org/schema/context http://www.springframework.org/schema/

cc_xxo的博客 2783

Spring使用注解实现注入

Spring框架 第五讲:使用注解实现注入 一、 Spring注解概述 jdk1.5开始支持注解,spring2.5开始全面支持注解,利用注解的方式注入属性,需要准备: (1)spring引入context约束配置 (2)开启属性注解 Spring的一个核心功能是Ioc,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式。 Spring注解方式减少了配置文件内容,更加便于管理,并且使用注解可以大大提高了开发效率。 二、 优缺点 传统的S

m0_45858905的博客 1281

基于注解的Spring注入

基于注解的Spring注入 使用注解时,需要添加额外的依赖spring-aop-5.2.6.RELEASE.jar,下面是项目结构: 我们已经将jar导入到lib库中,就可以直接使用了。 首先需要在bean.xml文件中进行配置,因为使用注解进行属性的注入,需要在xml中引入context和aop。 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/bean

weixin_43412322的博客 280

Spring原理揭秘--基于注解的注入

在之前的方法中我们知道spring是基于xml进行注入的bean,但是在目前的spring中我们一般不再是书写xml文件来一一列出需要管理的bean而是通过注解的方式来完成依赖的注入。下面我们就来聊聊依赖的注入是哪种方式。

qq_65779957的博客 1412

Spring注入:配置与注解

之前在某电商公司的时候也接触过一点Spring,不过了解不深,只是大概知道这个东西的存在。现在在一家公司实习的时候,才系统的看了一下Spring,这个框架网上的解释都很多,但主要都会介绍一下关于spring注入这一点,其实spring还有拦截的功能,而spring本身又是可以通过注解来使用,也可以通过配置文件使用,比较复杂,这里粗略介绍spring的注入层面,后面也许会写一篇关于spring MV

ZZ的博客 1万+

Spring | Spring通过注解注入Bean的四种方式

Spring 注解的方式注入Bean 官方定义:IoC又叫依赖注入(DI)。它描述了对象的定义和依赖的一个过程。依赖的对象通过构造参数、工厂方法参数或者属性注入,当对象实例化后依赖的对象才被创建,当创建bean后容器注入这些依赖对象。这个过程基本上是反向的。 方法详情 1.在Spring中,一个类我们为它加上注解就可以被识别为一个Bean。 例如你在一个类上加上一个@Component注解...

cafeshadow的博客 1万+

Spring构造注入&注解注入&AOP

一、依赖注入 1.导包 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocatio...

Yuncoco的博客 1621

Spring使用注解方式注入对象

上次Spring入门的时候我们使用的是最原始的方法来注入对象,这次我们用个有意思的方式——注解方式来注入对象,使用注解方式可以使得配置文件变得更加简洁,前面的准备工作可以看我的上一篇博客https://mp.youkuaiyun.com/postedit/90181339 首先我们修改xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <be...

java__liyong的博客 1352
上一篇: 了解记忆这些,才能说熟练使用Eclipse
下一篇: 计数排序
Coder_Jiang
博客等级 码龄14年 28粉丝 8原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值