博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring+springboot认识
阅读量:4033 次
发布时间:2019-05-24

本文共 5194 字,大约阅读时间需要 17 分钟。

一.SpringBoot介绍

1.SpringBoot基本概念

Spring Boot是其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

二.Spring的注解配置

1.spring IOC的使用

1.1xml形式

public class Myben(){
}

1.2 通过注解方式

/** * Spring的配置类 相当于是:applicationContext.xml * @Configuration :Spring的配置标签,标记改类是Spring的配置类 */@Configurationpublic class ApplicationConfig {
/** * @Bean : Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理 */ @Bean public MyBean myBean(){
return new MyBean(); }}

1.3ComponentScan&ComponentScans自动扫描

@Componentpublic class MyBean {
}
/** * * @ComponentScan会扫描 @Component @Controller @Service 注解的实体类  value默认不写, * 表示扫描本包下面的所有有该类注解的实体类 * */@Configuration@ComponentScan(value = {
"cn.itsource._03_bean_componentscan"}, //排除资源 excludeFilters = {
@ComponentScan.Filter( //通过注解排除 type = FilterType.ANNOTATION , //排除有@Controller注解的类 value = Controller.class ) })public class ApplicationConfig {
}
2.bean的详情
  • bean的id: 方法名
  • bean的name:可以通过 @Bean标签的name属性指定
  • 生命周期方法:initMethod , destroyMethod
  • 单利多利: @Scope(“prototype”)
  • 懒初始化: @Lazy
@Bean(name="" , initMethod ="" ,destroyMethod="" ) //@Scope("prototype") //@Lazy public MyBean myBean(){
return new MyBean(); }
3.依赖注入
/**     * @Bean : Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理     *  bean的id:  方法名 myBean     *  bean的name:可以通过 @Bean标签的name属性指定     *  生命周期方法:initMethod , destroyMethod     *  单利多利: @Scope("prototype")     */    @Bean(initMethod = "init" , destroyMethod = "destroy")    public MyBean myBean(OtherBean otherBean){
MyBean myBean = new MyBean(); myBean.setName("猪儿虫"); //myBean.setOtherBean(otherBean()); myBean.setOtherBean(otherBean); return myBean ; } @Bean //@Lazy public OtherBean otherBean(){
return new OtherBean(); }
4.条件Conditional

Conditional注解帖在bean的定义方法上来判断,如果不满足条件就不会定义bean

1.在Bean的定义方法帖@Conditional

@Bean    @Conditional(value = MyCondition.class)    public MyBean windowsMyBean(){
return new MyBean("windowMyBean"); } @Bean @Conditional(value = LinuxCondition.class) public MyBean linuxMyBean(){
return new MyBean("linuxMyBean"); }
public class MyCondition implements Condition {
/** * 匹配方法,返回值决定是否满足条件 */ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//获取系统环境 String systemName = context.getEnvironment().getProperty("os.name"); if("Windows 10".equals(systemName)){
return true; } return false; }}
5.import

1.直接导入Bean或者配置类

@Configuration@Import(ApplicationOtherConfig.class)	//导入其他的配置类public class ApplicationConfig

2.导入ImportSelector

public class MyImportSelector implements ImportSelector {
//选择导入,该方法返回我们需要导入的类的全限定名 public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{
"cn.itsource._07_import_selector.MyBean", "cn.itsource._07_import_selector.OtherBean"}; }}
@Configuration@Import(MyImportSelector.class)	//导入选择器public class ApplicationConfig
6.FactoryBean

1.定义FactoryBean

public class MyBeanFactoryBean implements FactoryBean
{
public MyBean getObject() throws Exception {
return new MyBean(); } public Class
getObjectType() {
return MyBean.class; } public boolean isSingleton() {
return true; }}

2.配置 MyBeanFactoryBean的bean定义

@Configurationpublic class ApplicationConfig {
@Bean public MyBeanFactoryBean myBean(){
return new MyBeanFactoryBean(); }}

3.测试

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes ={
ApplicationConfig.class})public class AnnoTest {
@Autowired private MyBean myBean; @Test public void test(){
System.out.println(myBean);

三.SpringBoot入门HelloWorld

1.pom文件

org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
...
org.springframework.boot
spring-boot-starter-web

2.启动类

@SpringBootApplicationpublic class ApplicationConfig {
public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class); }}

3.编写Controller

@Controllerpublic class Example {
@RequestMapping("/") @ResponseBody String home() {
return "Hello World!"; }}

spring中一些重要注解的理解

  • spring-boot-starter-parent :SpringBoot的父工程,帮我们管理了很多的基础jar包

  • spring-boot-starter-web :SpringBoot和SpringMvc整合的jar包,并且导入了日志,tomcat,等等相关的jar包

  • RestController : Controller+ResponseBody

  • @EnableAutoConfiguration : 开启自动配置功能

  • SpringApplication.run : 启动SpringBoot应用

  • jar :SpringBoot应用默认打jar包

  • SpringBootApplication:包括三个标签组成

    @SpringBootConfiguration - @Configuration : Spring的配置标签

    @EnableAutoConfiguration :开启自动配置

    @ComponentScan :组件自动扫描

spring底层中pom的一些理解
dependencyManagement> 该标签下的jar包,默认是不能被子项目直接使用的 , 他只有声明的功能 , 如果只项目想用这里标签里面的jar包 ,需要显示的写出来 ,而版本号使用父工程的。达到版本号统一管理的效果dependencies> 这个标签下面的jar包默认会被子项目直接继承直接使用

转载地址:http://vwfdi.baihongyu.com/

你可能感兴趣的文章
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
C++获取文件大小常用技巧分享
查看>>
关于AIS编码解码的两个小问题
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.147 - LeetCode1108
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>