java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot serviceImpl初始化注入对象

springboot serviceImpl初始化注入对象实现方式

作者:奔跑的闲鱼码农

这篇文章主要介绍了springboot serviceImpl初始化注入对象实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot初始化serviceImpl注入对象

遇到个需求是:公司是炼钢相关的项目,点位数据上来后放入kafka中,kafka要对数据进行处理,根据配置的点位值判断点位是否正常。

经历

看到这个需求,开始是这样实现的(然后每次修改了配置,就置空一下这个map)

public static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = null
//消费方法
private void consumerMsg(Object msg){
    if(monitorConfigMap == null){
        //初始化加载
    }
    //消费消息
}

有个同事看了我写的,觉得会发生线程安全问题。仔细一想是觉得有点不对劲,于是乎就修改成了下面这样

private static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = new HashMap<>();
@Autowired
private LogConfigMapper logConfigMapper;
private ConsumerPointDataMsg(){
    initMonitorConfig();
}
public void refreshMonitorMap() {
        //此处会调用数据库日志配置logConfigMapper
        initMonitorConfig();
 }

这样就有一个问题了,类构造器中初始化时要调用logConfigMapper,此刻这个mapper的实体是null的。以上就是问题产生的前因,下面继续讲述后果,直接上干货。

解决方案

1.构造器注入

@Component
public class PostConstructTest1 {
    @Autowired
    PostConstructTest2 postConstructTest2;
    public PostConstructTest1() {
        //postConstructTest2.test();
    }
    @PostConstruct
    public void init() {
        // todo sth
    }
}

Bean初始化时的执行顺序: 构造方法 -> @Autowired -> @PostConstruct

2.ApplicationContext

@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}
//使用时
private void initMonitorConfig(){
    LogConfigMapper mapper = ApplicationContextProvider.getBean(LogConfigMapper.class);
}

此种方案不太建议使用,因为有点开挂的感觉,直接到spring容器中去获取bean

3.重写afterPropertiesSet

@Component
@Slf4j
public class ConsumerPointDataMsg implements InitializingBean {
    private static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = new HashMap<>();
    @Autowired
    private LogConfigMapper logConfigMapper;
     @Override
    public void afterPropertiesSet() throws Exception {
        initMonitorConfig();
    }
}

4. 构造方法注入

@Configuration
public class XxlJobConfig {
    private final AddressServiceImpl addressService;
    public XxlJobConfig(AddressServiceImpl addressService) {
            this.addressService = addressService;
    }
    //直接调用
    addressService.serviceUrl("XXL-JOB-ADMIN")
}

springboot无法自动注入bean问题

Description:

Field demoService in com.spring.web.DemoApplication required a bean of type 'com.spring.service.DemoService' that could not be found.

Action:
Consider defining a bean of type 'com.spring.service.DemoService' in your configuration.

谷歌翻译为:

com.spring.web.DemoApplication中的field demoService需要无法找到类型为“com.spring.service.DemoService”的bean。

我的代码:

controller:DemoApplication

package com.spring.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spring.service.DemoService;
@RestController
//@EnableAutoConfiguration
@SpringBootApplication()
public class DemoApplication {
	@Autowired
	private DemoService demoService;
	@RequestMapping("/")
	public String helloWorld(){
		String msg =  demoService.demo();
		return msg;
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

Service:DemoService

package com.spring.service;
public interface DemoService {
	public String demo();
}

ServiceImpl:DemoServiceImpl

package com.spring.service.impl;
import org.springframework.stereotype.Service;
import com.spring.service.DemoService;
@Component
public class DemoServiceImpl implements DemoService {
	@Override
	public String demo() {
		System.out.println("Hello World!");
		return "Hello World!";
	}
}

报错:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)
 
2017-09-11 16:16:10.367  INFO 14512 --- [           main] com.spring.web.DemoApplication           : Starting DemoApplication on PC2014101864 with PID 14512 (E:\workspacedubbo\boot\boot-web\target\classes started by Administrator in E:\workspacedubbo\boot\boot-web)
2017-09-11 16:16:10.369  INFO 14512 --- [           main] com.spring.web.DemoApplication           : No active profile set, falling back to default profiles: default
2017-09-11 16:16:10.421  INFO 14512 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31206beb: startup date [Mon Sep 11 16:16:10 CST 2017]; root of context hierarchy
2017-09-11 16:16:11.710  INFO 14512 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-11 16:16:11.721  INFO 14512 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-09-11 16:16:11.722  INFO 14512 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-09-11 16:16:11.856  INFO 14512 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-09-11 16:16:11.856  INFO 14512 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1438 ms
2017-09-11 16:16:11.992  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-11 16:16:12.025  WARN 14512 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoApplication': Unsatisfied dependency expressed through field 'demoService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.spring.service.DemoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-09-11 16:16:12.026  INFO 14512 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-09-11 16:16:12.038  INFO 14512 --- [           main] utoConfigurationReportLoggingInitializer : 
 
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-09-11 16:16:12.107 ERROR 14512 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
 
***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Field demoService in com.spring.web.DemoApplication required a bean of type 'com.spring.service.DemoService' that could not be found.
 
 
Action:
 
Consider defining a bean of type 'com.spring.service.DemoService' in your configuration. 

解决办法

根据英文的提示是在配置中找不到一个指定自动注入类型的bean,

网上搜索得知:

经过多方排查得出结论:

正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上,

这个注解其实相当于下面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository,至此明白问题所在,之前我将接口与对应实现类放在了与控制器所在包的同一级目录下,这样的注解自然是无法被识别的。

      

之前我的controller和service是在同级目录下面现把controller放在service的上级目录,再启动就不报错了。

至此,得出两种解决办法  

1.将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法  

2.在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解,见下图

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文