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方法。所以和字段的作用域无关。
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
相关文章
使用CXF和Jersey框架来进行Java的WebService编程
这篇文章主要介绍了使用CXF和Jersey框架来进行Java的WebService编程,Web service是一个平台独立的低耦合的自包含的基于可编程的web的应用程序,需要的朋友可以参考下2015-12-12
Java中Easypoi实现excel多sheet表导入导出功能
这篇文章主要介绍了Java中Easypoi实现excel多sheet表导入导出功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-01-01
Springboot RestTemplate 简单使用解析
这篇文章主要介绍了Springboot RestTemplate 简单使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-08-08
MyBatis Generator配置生成接口和XML映射文件的实现
本文介绍了配置MBG以生成Mapper接口和XML映射文件,过合理使用MBG和自定义生成策略,可以有效解决生成的Example类可能带来的问题,使代码更加简洁和易于维护2025-02-02
Java中的ArrayList和contains函数和扩容机制(源码详解)
这篇文章主要介绍了Java中的ArrayList和contains函数和扩容机制,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-10-10


最新评论