Spring学习笔记


Spring

引入依赖

junit为测试框架

可以在方法上标注@Test进行直接的测试

image-20220319234522266

 
        
            org.springframework
            spring-context
            5.3.15
        

        
            junit
            junit
            4.11
        
    

简单的小程序

配置好配置文件

在resource创建spirng的配置文件 applicationContext.xml

然后引入实现类的对象

prototype表示有多例 在执行getBean对象时创建

singleton表示一例 在spring文件配置导入时创建

id为 app.getBean时索引的


在main文件里进行创建spring容器

  ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

然后通过容器的调取来获取实现类对象,挺类似于JDBC操作中的会话工厂

 UserDao userDao = (UserDao) app.getBean("userDao");

通过容器的存取,统一将类的API放入容器中,需要的时候通过spring容器调取,避免了大量的重复的对dao的先申明创建。

生命周期配置

默认先调用无参构造

image-20220319235518924

spring对象创建时 要强转为功能更为强大的实现类

Bean实例化的三种方式

image-20220320000129587

无参构造方法

通过配置文件映射对应的类

scope表示创建的生命周期


工厂静态方法

image-20220320000404607

创建静态工厂的类,创建返回应该实体对象的静态获取方法

然后通过配置文件配置Bean

    

class 指定对应的静态工厂类 , factory-method 指定对应id的静态方法

工厂实例方法

因为不是静态方法,所以一般的做法是需要先创建工厂的实体类,然后通过工厂的实体类来调用其中的方法

  
先创建了工厂的实体对象
然后再通过工厂的实体化对象来映射其中的方法

Spring boot

引入maven依赖

 
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

父工程中 包含了相对应用场景的依赖

主程序固定模式

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

/**
 * @SpringBootApplication: 注解这是一个springboot应用
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

application.properties

springboot的依赖文件,可以通过更改依赖的属性来配置springboot

属性参考文档

将项目直接打包为可执行的jar包

maven配置


    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    


执行jar包的关闭

该方法为通过springboot的组件在网页中嵌入中进行关闭的操作 下面的方法为Linux命令行中操作 直接杀进程 kill -9 [PID]

同时Windows中,可以cmd - taskmgr - 找到相应的java-jar进程关闭

参考地址

依赖



    org.springframework.boot

    spring-boot-starter-actuator


application.properties中添加:

#启用shutdown

endpoints.shutdown.enabled=true

#禁用密码验证

endpoints.shutdown.sensitive=false

然后可以调用 http://localhost:8080/shutdown 进行关闭,其中localhost:8080 需要替换为自己实际的地址和端口

如果要配置路径,需要在application.properties中添加

management.context-path=/manage

则关闭命令http://localhost:8080//manage/shutdown

如果在关闭时需要安全验证,则在pom.xml文件中添加



     org.springframework.boot

     spring-boot-starter-security


application.properties中添加:

#开启shutdown的安全验证

endpoints.shutdown.sensitive=true

#验证用户名

security.user.name=admin

#验证密码

security.user.password=admin

#角色

management.security.role=SUPERUSER

# 指定端口

management.port=8081

# 指定地址

management.address=127.0.0.1

or

启动:java -jar 命令的最后添加 "&",后台运行java程序;

  关闭:”fg jobnum“命令将后台程序转到前台执行,然后“CTRL+C组合健“关闭;

Linux下jar包的启动

java -jar platform-scm-v108.jar --spring.profiles.active=product --server.port=8080 & //简单启动
nohup java -jar platform-scm-v108.jar --spring.profiles.active=product --server.port=8080 &  //不挂断后台启动
nohup java -jar platform-scm-v108.jar --spring.profiles.active=product --server.port=8080 >/data/platform/log/platform-scm.txt & //不挂断后台启动且重定向日志

停止jar包

ps -ef | grep platform-scm-v108.jar

# 找到对应的进程然后杀掉

kill -9 进程号
kill -9 `ps -ef | grep platform-scm-v108.jar | grep -v 'grep' `
说明:两次使用管道,排除其他进程,直接杀掉相应的进程。

springboot的包扫描

@SpringBootApplication(scanBasePackages="从根目录下的包")

@Conditional - 条件装配 符合条件时,才装配进该组件

@ImportResouce("classpath:xx.xml")

导入spring配置文件

自动配置绑定

1.@ConfigurationProperties(prefix="")

省去了用正则表达式读取截取xml文件

只有在容器中的组件才能用注解的方式用springboot

所以需要在前面加个@Component自动注册Bean组件

2.@EnableConfigurationProperties(xx.class)