首页 > 其他分享 >Springboot知识点总结

Springboot知识点总结

时间:2024-10-23 23:16:53浏览次数:3  
标签:总结 知识点 java Springboot 配置文件 容器 Bean Student public

一、传统使用配置文件方式创建Java对象:

1、创建一个普通的Maven项目,并加入依赖:
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.1</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
      <version>1.18.24</version>
    </dependency>
2、创建数据类Student:
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Student {
    private Integer age;
    private String name;
    private String sex;
}
3、使用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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--声明bean对象-->
    <bean id = "myStudent" class = "com.lifang.vo.Student">
        <property name = "name" value = "李四"/>
        <property name = "age" value="22"/>
        <property name="sex" value = "女"/>
    </bean>

</beans>
4、单元测试,从容器中拿到myStudent对象:
   @Test
    public void test01(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Student s = (Student)ac.getBean("myStudent");
        System.out.println(s);
    }

二、JavaConfig配置类:

使用java类当作配置文件来配置我们的Spring IOC 容器,是配置spring容器的纯java方式

涉及到的注解:

@Configuration:

        使用@Configuration注解修饰的类可以当作配置文件来使用,用来配置spring容器,是配置spring容器的纯java方式,在这个javaConfig配置类中,我们可以通过定义方法结合@Bean注解来声明对象,就是在方法上面使用@Bean注解,把方法返回值对象注入到Spring容器中,如果@Bean注解不指定对象名称,默认这个方法名就是java对象在容器中的名字。

@Bean: 相当于<Bean>

        用来修饰javaConfig配置类中的方法,放在方法上,它会把方法的返回值对象注入到容器中,默认方法名就是java对象在容器中的名字。若用@Bean,不指定对象的名称,默认的是这个方法名就是java对象在容器中的名字

        指定:@Bean(name = "lisiStudent") @Bean( "lisiStudent")

1、自定义一个类,这个类来代替配置文件的作用,类上加@Configuration注解:
@Configuration
public class SpringConfig {
    //若用@Bean,不指定对象的名称,
    // 默认的是这个方法名就是java对象在容器中的名字:createStudent
    @Bean
    public Student createStudent(){
        Student s = new Student();
        s.setSex("男");
        s.setAge(26);
        s.setName("张三");
        return s;
    }
    @Bean("lisi")
    public Student createStudent1(){
        Student s = new Student();
        s.setSex("男");
        s.setAge(26);
        s.setName("lisi");
        return s;
    }
}
2、测试:
    @Test
    public void test01(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Student s = (Student)ac.getBean("myStudent");
        System.out.println(s);
    }

    @Test
    public void test02(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student s = (Student)ac.getBean("lisi");
        System.out.println(s);
    }

三、@ImportResource:

        导入其它的配置文件,把其它的配置文件导入进来,与当前的javaconfig配置类合在一块作为容器的配置使用

        Spring Boot 中也可以使用 XML 配置,你可以在独立的配置文件中声明你的bean对象,之后再通过@ImportResource把我们的配置文件导入进来就可以了

                         

1、在beans.xml中有“myStudent”对象:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--声明bean对象-->
    <bean id = "myStudent" class = "com.lifang.vo.Student">
        <property name = "name" value = "李四"/>
        <property name = "age" value="22"/>
        <property name="sex" value = "女"/>
    </bean>
</beans>

class path类路径就在你编译后的target-classes目录之下

                        

2、导入: 
@Configuration
@ImportResource(value = "classpath:beans.xml")
public class SpringConfig {
    //若用@Bean,不指定对象的名称,
    // 默认的是这个方法名就是java对象在容器中的名字:createStudent
    @Bean // @Bean("lisi")
    public Student createStudent(){
        Student s = new Student();
        s.setSex("男");
        s.setAge(26);
        s.setName("张三");
        return s;
    }
}
3、测试:
 @org.junit.Test
    public void test02(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student s = (Student)ac.getBean("myStudent");
        System.out.println(s);
    }

四、@PropertySource

        读取properties属性配置文件,使用属性配置文件可以实现外部化配置,在程序代码之外提供数据,比如数据库的连接信息
        步骤:
                1.在resources目录下,创建properties文件,使用k=v的格式提供数据

                2.使用@PrppertySource注解指定properties文件的位置
                3.使用@Value ( value="${key)”

1、在resources目录下创建一个普通文件,扩展名以.properties结尾:

这个文件中如果你要用中文的话你的设置一下你的编码格式: 

                                                

2、使用@Value读取属性配置文件:
@AllArgsConstructor
@NoArgsConstructor
@Data

@Component("tiger")//一定记得要加@Component
public class Tiger {
    @Value("${tiger.name}")
    private String name;
    @Value("${tiger.age}")
    private Integer age;
}
3、使用@PropertySource指定属性配置文件所在位置:

       @PropertySource注解 的作用只是让spring框架读到这个配置文件而已,知道里面的key和value的值,但是我们这个java对象是用注解的方式创建的,那你需要告诉我们的容器,到哪里去找Tiger这个对象:组件扫描器 @ComponentScan

@Configuration
@ImportResource(value = "classpath:beans.xml")

@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.lifang.vo")
public class SpringConfig {
    //若用@Bean,不指定对象的名称,
    // 默认的是这个方法名就是java对象在容器中的名字:createStudent
    @Bean // @Bean("lisi")
    public Student createStudent(){
        Student s = new Student();
        s.setSex("男");
        s.setAge(26);
        s.setName("张三");
        return s;
    }
}
4、测试 :
    @Test
    public void test03(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
        Tiger t = (Tiger) ac.getBean("tiger");
        System.out.println(t);
    }

Springboot已经把你spring相关的配置好了,也就是说程序中已经有了中央调度器,你直接写业务逻辑controller处理就可以了,大大提高了开发效率

五、@SpringBootApplication

@SpringBootApplication是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:

        Springboot都包含主启动类。不管是web应用还是非web应用,它都是通过主方法来启动Springboot项目的,在方法之上有注解@SpringBootApplication

标签:总结,知识点,java,Springboot,配置文件,容器,Bean,Student,public
From: https://blog.csdn.net/weixin_53676834/article/details/143097656

相关文章

  • 2024年10月23日总结
    今天继续学习了数据库的连接,这是今日总结完成的模版(还有一些地方有问题)packagemapper;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.SQLException;publicclassstudentsystemmapper{Connectionconn=n......
  • 人体解剖生理学——知识点1
    1、细胞受刺激后产生动作电位的能力称为兴奋性。2、自身调节是指许多器官、组织、细胞不依赖于神经或体液调节而自身也能对周围环境变化产生适应性反应的过程。3、生命活动的基本特征:新陈代谢、适应性、生殖、内环境稳态。4、疏松结缔组织是种类最多的组织细胞。5、有髓神......
  • MyBatis-Plus知识点总结
    官方文档:https://baomidou.com/introduce/ 快速开始1.引入MyBatis-PlusStarter依赖<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.7</version></......
  • Java Springboot 接收前端上传图片,并返回路径让前端显示图片
    一、接收前端图片并保存并为前端返回一个图片路径. @RestController@RequestMapping("/upload")publicclassUploadImgController{@Autowired(required=false)privateResourceLoaderresourceLoader;@Value(value="/Users/user/Java/Upload/Serve......
  • springboot 整合mybatis
    1、SpringBoot+MyBatis一、简单回顾一下MyBatis核心对象包括以下三个:SqlSessionFactoryBuliderSqlSessionFactorySqlSessionSqlSessionFactoryBuilder-->SqlSessionFactory-->SqlSession 关于MyBatis的事务管理机制(两种)<transactionManagertype="JDBC"/......
  • springboot+vue展位设计模型库网站的java开发与设计
    目录系统实现截图详细技术介绍本课题软硬件环境核心代码部分展示其他springboot项目推荐详细视频演示源码获取系统实现截图详细技术介绍系统采用了基于SpringBoot、Vue和MySQL的三层结构体系。前端部分通过Vue框架构建用户界面,实现页面展示和交互功能,后端则利......
  • JAVA开源项目 基于Vue和SpringBoot购物商城网站
    本文项目编号T032,文末自助获取源码\color{red}{T032,文末自助获取源码}......
  • JAVA开源项目 基于Vue和SpringBoot高校心理教育辅导系统
    本文项目编号T031,文末自助获取源码\color{red}{T031,文末自助获取源码}......
  • 蓝桥杯EDA赛道经验分享(一)&12、13、14届省赛客观题知识点
    一、经验分享1.文件提取离线模式——>文件——>(大压缩包)导入专业版——>导入文件;(小压缩包)提取库文件。2.布线规则先根据参赛文件改布线规则(间距,线宽)。3.PCBlayout注意事项(1)避免重叠:确保元件间无物理重叠,为布线留出足够空间。(2)元件放置:大功率元件及发热元件应分散布局......
  • 20241023 模拟赛总结
    期望得分:100+100+0+20=220实际得分:100+0+0+0=100(满昏)这算哪门子信心赛……分挂没了,懒得喷。T1人机分类讨论题。T2一眼二分答案,二分最终的最小的最大值,记bi表示把i这个位置加到至少ai需要多少次,然后手玩不知道多少组发现每个位置至少要操作一次,那机器人的启动位置是无......