SpringBoot容器注入对象 Java SpringBoot容器注入对象详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot容器注入对象 Java SpringBoot容器注入对象详解

myli92   2021-09-08 我要评论
想了解Java SpringBoot容器注入对象详解的相关内容吗,myli92在本文为您仔细讲解SpringBoot容器注入对象的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,SpringBoot容器,注入对象,下面大家一起来学习吧。

1.注入的方式

方式一:使用Import注解

增加一个类HelloCompent

package com.lx.component;
public class HelloCompent {
    public void say() {
        System.out.println("HelloCompent.say hello");
    }
}
@SpringBootApplication
@Import(HelloCompent.class)
public class StartProgramNoWeb {
    public static void main(String[] args) {
        System.out.println("启动");
        SpringApplication.run(StartProgramNoWeb.class, args);
    }
}

使用@Import就可以将HelloCompent注入到容器中。(HelloCompent类不需要增加@Service ,

@Component等注解)

方式二:使用@Service 或者@Component等注解注入到容器中

在需要注入的类增加注解,修改HelloCompent类

package com.lx.component;
import org.springframework.stereotype.Component;
@Component
public class HelloCompent {
    public void say() {
        System.out.println("HelloCompent.say hello");
    }
}

方式三:使用@Configuration和@Bean组合实现

使用@Configuration和@Bean组合注入可以将对象注入到容器中,我主要生效的还是@Bean,如果将@Configuration换成@Component也是可以正常注入的。

增加一个CustomConfig类

package com.lx.config;
import com.lx.component.HelloCompent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConfig {
    @Bean("helloCompentConfig")
    public HelloCompent helloCompent() {
        return  new HelloCompent();
    }
}

这里我使用了方式2和3同时注入了,会导致重复注入而发生异常。所以我在bean是增加一个名称,所以打印容器里对象的名称也就是设置的名称。

springboot自动配置注入对象就是使用的方式3实现注入对象到容器中,平时最常用的就是方式2和方式3,如果同时使用方式2和方式3注入会出现注入重复的对象。

2.注入是增加条件判断注解

@ComponentScan:声明作用域
@ConditionalOnBean:当容器里有指定Bean的条件下
@ConditionalOnClass:当类路径下有指定的类的条件下
@ConditionalOnExpression:基于SpEL表达式为true的时候作为判断条件才去实例化
@ConditionalOnJava:基于JVM版本作为判断条件
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置
@ConditionalOnMissingBean:当容器里没有指定Bean的情况下
@ConditionalOnMissingClass:当容器里没有指定类的情况下
@ConditionalOnWebApplication:当前项目时Web项目的条件下
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下
@ConditionalOnProperty:指定的属性是否有指定的值
@ConditionalOnProperty(prefix = "customconfig",name = "enable",havingValue = "true") 等效于@ConditionalOnProperty(value = "customconfig.enable",havingValue = "true")
@ConditionalOnResource:类路径是否有指定的值
@ConditionalOnOnSingleCandidate:当指定Bean在容器中只有一个,或者有多个但是指定首选的Bean
这些注解都组合了@Conditional注解,只是使用了不同的条件组合最后为true时才会去实例化需要实例化的类,否则忽略

3.构造方法时带参数注入

有时候在实际工作中,我们需要在构造方法是增加一些处理逻辑,同事也需要从容器中获取对象,但是这时候我们在构造方式时想从容器中获取对象,实际上并不能获取到。因为这个spring的注解优先级有关系。当构造方法使用字段时,spring并没有将对象注入成功,所有构造方式取值也就是用。

package com.lx.component;
import com.lx.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class HelloTwoCompent {
    @Value("${proper.name}")
    private String name;
    @Autowired
    private HelloService helloService;
    public HelloTwoCompent() {
       System.out.println("hellotwo 无参");
        System.out.println("name=" + name + ";helloService=" + helloService);
        if (helloService != null) {
            helloService.print();
        }
    }
}

方式1:使用spring xml实现

新增加一个用于测试的类HelloTwoCompent

在xml bean节点上增加构造方法参数配置即可。然后在springboot启动类上增加@ImportResource(locations= {"classpath:application-bean.xml"})。这里我不喜欢用,暂时就不写测试代码了。

方式2:使用@Autowired

修改HelloTwoCompent 类在构造方法上增加@Autowired

    @Autowired
    public HelloTwoCompent( @Value("${proper.name}") String name, HelloService helloService) {
        System.out.println("hellotwo 两参");
        System.out.println("name=" + name + ";helloService=" + helloService);
        if (helloService != null) {
            helloService.print();
        }
    }

方式3使用@Configuration和@Bean组合

增加一个配置了 HelloConfig

package com.lx.config;
import com.lx.component.HelloTwoCompent;
import com.lx.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloConfig {
    @Value("${proper.name}")
    private String name;
    @Autowired
    private HelloService helloService;
    @Bean("helloTwoCompentBean")
    public HelloTwoCompent helloTwoCompent() {
        return  new HelloTwoCompent(name,helloService,"config-bean");
    }
}

修改一下HelloTwoCompent,增加一个三个参数的构造方法,并且构造方法上不增加任何的注解。

    public HelloTwoCompent(String name, HelloService helloService,String type) {
        System.out.println("hellotwo 三参;type="+type);
        System.out.println("name=" + name + ";helloService=" + helloService);
        if (helloService != null) {
            helloService.print();
        }
    }

4.对象注入时的一些总结

1.静态字段不支持@Autowired和@Resource实现自动装配,因为自动装配依赖于set和get方法,@Autowired和@Resource就是消除set和get方法。

2.自动装配的字段可以为private,因为自动装配依赖于set和get方法。所以和字段的作用域无关。

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们