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等注解)

方式二: @ComponentScan或@ComponentScans

在启动类注解上增加注解

@SpringBootApplication
@ComponentScans(value = {
        @ComponentScan(value = "com.lx.bootstrapcommon")
        , @ComponentScan(value = "com.lx.mybootstrapcommons",includeFilters = {
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.lx.mybootstrapcommons.config")})})
public class BootstrapCommonProgram {
    public static void main(String[] args) {
        SpringApplication.run(BootstrapCommonProgram.class, args);
    }
}

 外部包也能使用这种方式直接注入

 方式三:使用@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方法。所以和字段的作用域无关。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值