Spring Boot 依赖注入
2016-12-19 15:09
363 查看
Spring Boot 依赖注入
SpringBoot的实现方式基本都是通过注解实现的,跟Spring注解注入差不多,相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。
1).Autowired是自动注入,自动从spring的上下文找到合适的bean来注入
2).Resource用来指定名称注入
3).Qualifier和Autowired配合使用,指定bean的名称
4).Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
5).Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。
实例:
1.创建对象Test1 和Test2,如下
package com.supre.springboot;
import org.springframework.stereotype.Component;
@Component
public class Test {
public String test(String str){
System.out.println("000000000000"+str);
return "test1"+str;
}
}
3.创建springboot启动类App
4.运行启动类App,访问http://localhost:8080/test?str=aaa
注意:后来经研究发现,SpringBoot项目的Bean装配默认规则是根据Application类(SpringBoot项目入口类)所在的包位置从上往下扫描!这个类的位置很关键:如果Application类所在的包为:io.github.project.app,则只会扫描io.github. project.app包及其所有子包,如果service或dao所在包不在io.github. project.app及其子包下,则不会被扫描!
解决方案:
1.将SpringBoot项目的启动类,放在所以包之上。如Application类放在:“io.github. project”包下,其他类放在“io.github.project.bean”、“io.github.project.controller”、“io.github.project.dao”等。
2.使用@ComponentScan注解,在springboot的启动类上注解配置,需要扫描的包,例如:
@ComponentScan(basePackages={" io.github.project.app"," io.github.project.bean "})
注意:经测试,依赖注入,不能向static属性注入Spring上下文中的对象。
参考:http://blog.csdn.net/u014695188/article/details/52263903
SpringBoot的实现方式基本都是通过注解实现的,跟Spring注解注入差不多,相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。
1).Autowired是自动注入,自动从spring的上下文找到合适的bean来注入
2).Resource用来指定名称注入
3).Qualifier和Autowired配合使用,指定bean的名称
4).Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
5).Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。
实例:
1.创建对象Test1 和Test2,如下
package com.supre.springboot;
import org.springframework.stereotype.Component;
@Component
public class Test {
public String test(String str){
System.out.println("000000000000"+str);
return "test1"+str;
}
}
package com.supre.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Test2 { @Autowired private Test test; public String test(String str){ return "test2"+test.test(str); } }2.创建controller类TestController
package com.supre.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @Autowired private Test test; @Autowired private Test2 test2; @RequestMapping("/test") @ResponseBody public String test(String str){ System.out.println(test2.test(str)); return test.test(str); } }
3.创建springboot启动类App
package com.supre.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello world! * */ @SpringBootApplication @RestController //@ComponentScan(basePackages={"com.supre.springboot"}) public class App{ @RequestMapping("/") public String getHolle(){ return "HolleWord"; } public static void main( String[] args ){ System.out.println( "Hello World!" ); SpringApplication.run(App.class, args); } }
4.运行启动类App,访问http://localhost:8080/test?str=aaa
注意:后来经研究发现,SpringBoot项目的Bean装配默认规则是根据Application类(SpringBoot项目入口类)所在的包位置从上往下扫描!这个类的位置很关键:如果Application类所在的包为:io.github.project.app,则只会扫描io.github. project.app包及其所有子包,如果service或dao所在包不在io.github. project.app及其子包下,则不会被扫描!
解决方案:
1.将SpringBoot项目的启动类,放在所以包之上。如Application类放在:“io.github. project”包下,其他类放在“io.github.project.bean”、“io.github.project.controller”、“io.github.project.dao”等。
2.使用@ComponentScan注解,在springboot的启动类上注解配置,需要扫描的包,例如:
@ComponentScan(basePackages={" io.github.project.app"," io.github.project.bean "})
注意:经测试,依赖注入,不能向static属性注入Spring上下文中的对象。
参考:http://blog.csdn.net/u014695188/article/details/52263903
相关文章推荐
- SpringBoot学习 (二)— 依赖注入
- SpringBoot系列: 理解 Spring 的依赖注入(一)
- 01-SpringBoot——Spring基础-依赖注入
- 详解SpringBoot中实现依赖注入功能
- spring boot项目依赖spring mvc项目注入失败问题
- Spring boot框架整合quartz,job类中依赖注入报空指针问题
- 解决 Springboot中Interceptor拦截器中依赖注入失败
- Spring Boot参考文档(9)依赖注入
- 解决Springboot中Interceptor拦截器中依赖注入失败
- spring boot 多模块管理时,依赖注入bean失败
- 解决Springboot中Interceptor拦截器中依赖注入失败
- spring boot 学习(三) — 依赖注入 @Bean
- spring boot 多模块管理时,依赖注入bean失败
- 解决 Springboot中Interceptor拦截器中依赖注入失败
- SpringBoot中实现依赖注入功能
- Spring Boot快速入门(三):依赖注入
- spring的依赖注入三种方式
- 用小说的形式讲解Spring(1) —— 为什么需要依赖注入
- Spring的依赖注入(DI)和控制反转(IOC)
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案