springbootday1

1.javaconfig理论

什么是 javaconfig

以java代码的方式管理bean

为什么学习javaconfig

boot底层原理就是它

Java 的 bean 配置(JavaConfig)出现历史

spring1.x :xml配置
spring2.x :注解配置
spring3.x-4.x :javaconfig&springboot
spring5.x

javaconfig的操作

spring测试

new:ClassPathXmlApplicationContext
注入:RunWith ContextConfigration

IOC-xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
      ">
    <bean id="myDate" class="java.util.Date">
  scope="singleton" lazy-init="true" init-method="" destroy-method="" >
        <property name="name" value="zs"></property>
        <property name="otherBean" ref=""

</bean>
</beans>

测试注入 MyBean OtherBean

Ioc-注解

扫描注解
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd
">

    <context:component-scan base-package="cn.itsource._03iocanno_">

    </context:component-scan>
</beans>


配置注解
package cn.itsource._03iocanno_;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//controller service repository componet
@Component
public class MyBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Autowired
    private OtherBean otherBean;

    public OtherBean getOtherBean() {
        return otherBean;
    }

    public void setOtherBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }
}

2.Spring javaconfig-IOC

基本

导入xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.itsource</groupId>
<artifactId>javaconfig</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>compile</scope>
    </dependency>
</dependencies>


</project>

配置类

@Configuration:加了这个注解的类就相当于传统的一个applicationContext-xxx.xml
@Bean:在标注了@Configuration的类里面的方式上面打上@bean就相当于在applicationContext-xxx.xml配置的一个
Dao的名字默认就是方法名,如果想改方法名使用@Bean(“beanName”)

bean扫描@ComponentScan/ComponentScans)

单包,多包,排除或包含

package cn.itsource._05componentScan;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;

@Configuration //相当于原来的applicationContext,xml
//1 基础语法
//@ComponentScan("cn.itsource._05componentScan")
//2.配置多个包
//2.1加多个@componscan
//@ComponentScan("cn.itsource._05componentScan_.controller")
//@ComponentScan("cn.itsource._05componentScan_.service")
//2.2 @ComponentScans
/*@ComponentScans(value = {
        @ComponentScan("cn.itsource._05componentScan_.controller"),
        @ComponentScan("cn.itsource._05componentScan_.service")
})*/

//3 排除包里面某些类的bean,只包含某类注解的bean 其他的三个注解本身也是一个@Component
@ComponentScans( value = {
        //测试的是在一个包下面那些不要
  //      @ComponentScan(value = "cn.itsource._05componentScan",excludeFilters = {
                //排除加了Component注解的bean
       //         @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
      //  })


//只要改包下面的那些
@ComponentScan(
        value = "cn.itsource._05componentScan",
        includeFilters = {
                //排除加了Component注解的bean
                @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class})
        }
        ,useDefaultFilters = false //关闭默认全部扫描includeFilters才生效
)
})
public class IocConfig {

}

bean详情

1)@Scope单例测试

在这里插入图片描述
2)@Lazy懒加载
在这里插入图片描述
注入的四种方式

package cn.itsource._06bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MainConfig {
    @Autowired
    private OtherBean otherBean;

    //1 bean 名称  <bean id="" scope>
    //默认以方法名作为名称
    //@Bean("myBean") //配置名称
    //@Scope("singleton") //singleton单例(默认值) prototype多例
    //@Lazy //懒加载(默认),用到的时候才去创建对象,并且只对单例模式有效,因为多例是你要的时候才给你创建
    public MyBean yj() {
        // 方式1:创建对象直接设置值,没有在spring中,不较注入
//        OtherBean otherBean = new OtherBean();
//        MyBean myBean = new MyBean();
//        myBean.setOtherBean(otherBean);
// 方式2:直接调用方法
//        MyBean myBean = new MyBean();
       // 智能发现如果已经通过该方法注册了bean,直接注入就ok,不会再新创建一个了。
//        myBean.setOtherBean(otherBean());

        //方式3:对注入的bean进行设置值
        MyBean myBean = new MyBean();
        myBean.setOtherBean(otherBean);
        return myBean;
    }
    //方式4 通过构造函数进行注入 推荐使用

    @Bean
    public MyBean zzz(OtherBean otherBean){
        return  new MyBean(otherBean);

    }
    @Bean
    public  OtherBean otherBean(){
        return  new OtherBean();
    }
}

condition

@Conditional(value = LinuxCondition.class) //放到类上面下面所有方法都生效,但是如果方法上加了优先级更高****

import

类名
选择器
注册器

package cn.itsource._08import;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

// 通过import导入
// 1 导入特定类的对象
// 2 通过ImportSelector
// 3 ImportBeanDefinitionRegistra
@Configuration
@Import({YellowColor.class,GreenColor.class,WhiteColor.class,BlackColor.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
public class MainConfig {

    //直接通过bean名称获取的是Factorybean所创建的对象而不是PersonFactoryBean
    //如果想要获取PersonFactoryBean,通过&name
    @Bean
    public PersonFactoryBean person() {
        return new PersonFactoryBean();

    }
}

Bean生命周期

Bean生命周期:创建----初始化----销毁

3. springboot入门

理论

包的导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.itsource</groupId>
    <artifactId>sprinboot-hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

什么是Spring Boot

springBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:

. Spring Boot特点

Spring Boot 主要目标是:

  • 为所有 Spring 的开发者提供一个非常快速的、广泛接受的入门体验
  • 开箱即用(启动器starter-其实就是SpringBoot提供的一个jar包),但通过自己设置参数(.properties),即可快速摆脱这种方式。
  • 提供了一些大型项目中常见的非功能性特性,如内嵌服务器、安全、指标,健康检测、外部化配置等
  • 绝对没有代码生成,也无需 XML 配置。

入门

一 创建项目
parent
dependency
二 创建springboot项目并且启动
1)任意类加上@SpringBootApplication
2)Main函数启动springboot的应用

package cn.itsource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //标识一个为springboot的 项目
public class App {
    public  static  void main(String[] args){
        //启动应用
        SpringApplication.run(App.class,args);
    }
}

三 写一个Contorller来测试
HelloConroller

package cn.itsource.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @RequestMapping("/hello")
    @ResponseBody
    public  String hello(){
        return  "hello";
    }
}

打包运行、

添加插件

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

j进行打包
窗口运行:java -jar xxx.jar
后台运行: nohup java -jar XXX.jar & 只linux
注意事项
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值