@Condition
思考
案例实操
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
package com.ithema.springbootconditional.domain;
public class User {
}
//定义配置类,用来返回与user相关的Bean
package com.ithema.springbootconditional.config;
import com.ithema.springbootconditional.condition.ClassCondition;
import com.ithema.springbootconditional.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
//用来返回与user相关的Bean
@Bean
@Conditional(ClassCondition.class)
public User user(){
return new User();
}
}
//定义条件类
package com.ithema.springbootconditional.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class ClassCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//1.需求: 导入Jedis坐标后创建Bean
//思路:判断redis.clients.jedis.Jedis.class文件是否存在
boolean flag = true;
try {
Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
} catch (ClassNotFoundException e) {
flag = false;
}
return flag;
}
}
package com.ithema.springbootcondition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootConditionApplication {
public static void main(String[] args) {
//启动SpringBoot的应用,返回Spring的IOC容器
ConfigurableApplicationContext context = SpringApplication.run(SpringbootConditionApplication.class, args);
//获取Bean,redisTemplate
Object redisTemplate = context.getBean("redisTemplate");
System.out.println(redisTemplate);
}
}
(高级)
package com.ithema.springbootconditional.domain;
public class Student {
}
//定义配置类,用来返回与user相关的Bean
package com.ithema.springbootconditional.config;
import com.ithema.springbootconditional.condition.ConditionOnClass;
import com.ithema.springbootconditional.domain.Student;
import org.springframework.context.annotation.Bean;
public class StudentConfig {
@Bean
@ConditionOnClass("redis.clients.jedis.Jedis")
public Student student(){
return new Student();
}
}
package com.ithema.springbootconditional.condition;
import java.lang.annotation.*;
import org.springframework.context.annotation.Conditional;
@Target({ElementType.TYPE, ElementType.METHOD})//表示该注解(ConditionOnClass)可以加在哪上面TYPE(类)/Method
@Retention(RetentionPolicy.RUNTIME)//注解生效的实际
@Documented//生成文档
@Conditional(StudentConditon.class)
public @interface ConditionOnClass {
String[] value();
}
//定义条件类
package com.ithema.springbootconditional.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
public class StudentConditon implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//2.需求: 导入通过注解属性值value指定坐标后创建Bean
//获取注解属性值:value
String s = ConditionOnClass.class.getName();
System.out.println(s);
Map<String, Object> map = metadata.getAnnotationAttributes("s");
System.out.println(map); //{value=[redis.clients.jedis.Jedis]}
String[] value = (String[]) map.get("value");
boolean flag = true;
try {
for (String className : value) {
Class.forName(className);
}
} catch (ClassNotFoundException e) {
flag = false;
}
return flag;
}
}
小结
自定义条件:
定义条件类
:自定义类实现Condition接口
,重写matches方法,在matches方法中进行逻辑判断,返回 boolean值。matches方法两个参数:
判断条件
:在初始化Bean时,使用@conditional(条件类.class)注解SpringBoot提供的常用条件注解:
@ConditionalonProperty
:案例演示
//使用springboot提供的ConditionalonProperty注解 实现条件判断
@Bean("user2")
@ConditionalOnProperty(name = "itcast",havingValue = "heima")
public User getUser2(){
return new User();
}
# 在yml或这propreties中配置相关内容
# itoldlu=oldlu
itoldlu: oldlu
切换内置web服务器
4中内置服务器
供我们选择,我们可以很方便的进行切换。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcatartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<artifactId>spring-boot-starter-jettyartifactId>
<groupId>org.springframework.bootgroupId>
dependency>
@Enable*注解
思考
案例讲解
//springboot-enable引导类,我们在这里获取springboot-enable-other的user Bean。
package com.ithema.springbootenableother;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
//springboot不能直接获取其他模块的Bean,原因为@SpringBootApplication中的注解@ComponentScan
Object user = context.getBean("user");
System.out.println(user);
}
}
//domian包下创建一个User类
package com.ithema.domain;
public class User {
}
//config目录下创建一个User的配置类
package com.ithema.config;
import com.ithema.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
<dependency>
<groupId>com.ithemagroupId>
<artifactId>springboot-enable-otherartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
解决办法
package com.ithema.springbootenable;
import com.ithema.config.EnableUser;
import com.ithema.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
/**
* @ComponentScan 扫描范围:当前引导类所在包及其子包
*
* com.ithema.springbootenable
* com.ithema.config
* 方法 1.解决springboot不能直接获取其他模块的Bean,可以使用@ComponentScan扫描com.ithema.config包
*/
@SpringBootApplication
@ComponentScan("com.ithema.config") //com.ithema.domain.User@585ac855
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}
package com.ithema.springbootenable;
import com.ithema.config.EnableUser;
import com.ithema.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
/**
* @ComponentScan 扫描范围:当前引导类所在包及其子包
*
* com.ithema.springbootenable
* com.ithema.config
* 方法 1.解决springboot不能直接获取其他模块的Bean,可以使用@ComponentScan扫描com.ithema.config包
* 方法 2.可以使用@Import注解,加载类。这些类都会被Spring创建,并放入IOC容器
*/
@SpringBootApplication
@Import(UserConfig.class) //com.ithema.domain.User@7c2a69b4
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
//springboot不能直接获取其他模块的Bean,原因为@SpringBootApplication中的注解@ComponentScan
Object user = context.getBean("user");
System.out.println(user);
}
}
//提前在springboot-enable-other模块中提供一个@EnableUser供使用
package com.ithema.config;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}
package com.ithema.springbootenable;
import com.ithema.config.EnableUser;
import com.ithema.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
/**
* @ComponentScan 扫描范围:当前引导类所在包及其子包
*
* com.ithema.springbootenable
* com.ithema.config
* 方法 1.解决springboot不能直接获取其他模块的Bean,可以使用@ComponentScan扫描com.ithema.config包
* 方法 2.可以使用@Import注解,加载类。这些类都会被Spring创建,并放入IOC容器
* 方法 3.可以对Import注解进行封装。
*/
@SpringBootApplication
@EnableUser //com.ithema.domain.User@3eba57a7
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
//springboot不能直接获取其他模块的Bean,原因为@SpringBootApplication中的注解@ComponentScan
Object user = context.getBean("user");
System.out.println(user);
}
}
解析:
方法3就是我们接下来讲的SpringBoot为我们提供的@Enable*注解
。
@lmport注解
@Enable*
底层依赖于@lmport注解导入一些类,使用@lmport导入的类会被Spring加载到IOC容器
中。而@Import提供4中用法
:
用法 1:导入Bean
//用法1:导入Bean
@SpringBootApplication
@Import(User.class)
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
User user = context.getBean(User.class);
System.out.println(user);//com.ithema.domain.User@41477a6d
Map<String,User> map = context.getBeansOfType(User.class);
System.out.println(map);//获取Bean的名称,全路径:{com.ithema.domain.User=com.ithema.domain.User@41477a6d}
}
}
用法 2:导入配置类
//用法2:导入配置类,它可以同时把多个Bean 加载到IOC容器中
package com.ithema.domain;
public class Role {
}
package com.ithema.config;
import com.ithema.domain.Role;
import com.ithema.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration 使用方法2可以不加该注解
public class UserConfig {
@Bean
public User user() {
return new User();
}
@Bean
public Role role() {
return new Role();
}
}
//用法2:导入配置类,它可以同时把多个Bean 加载到IOC容器中
@SpringBootApplication
@Import(UserConfig.class)
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
User user = context.getBean(User.class);
System.out.println(user);
Role role = context.getBean(Role.class);
System.out.println(role);
}
}
用法 3:导入lmportSelector实现类。一般用于加载配置文件中的类
package com.ithema.config;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.ithema.domain.User", "com.ithema.domain.Role"};
}
}
package com.ithema.springbootenable;
import com.ithema.config.EnableUser;
import com.ithema.config.MyImportSelector;
import com.ithema.config.UserConfig;
import com.ithema.domain.Role;
import com.ithema.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(MyImportSelector.class)
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
User user = context.getBean(User.class);
System.out.println(user);
Role role = context.getBean(Role.class);
System.out.println(role);
}
}
用法4:导入lmportBeanDefinitionRegistrar实现类
//用法4
package com.ithema.config;
import com.ithema.domain.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
registry.registerBeanDefinition("user", beanDefinition);
}
}
package com.ithema.springbootenable;
import com.ithema.config.EnableUser;
import com.ithema.config.MyImportBeanDefinitionRegistrar;
import com.ithema.config.MyImportSelector;
import com.ithema.config.UserConfig;
import com.ithema.domain.Role;
import com.ithema.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(MyImportBeanDefinitionRegistrar.class)
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
User user = context.getBean(User.class);
System.out.println(user); //com.ithema.domain.User@796d3c9f
Object user2 = context.getBean("user");
System.out.println(user2);//com.ithema.domain.User@796d3c9f 为什么对象一样?是单例哦
}
}
@EnableAutoConfiguration注解
案例实操
SpringBoot的监听机制,其实是对Java提供的事件监听机制的封装
。
Java中的事件监听机制定义了以下几个角色:
SpringBoot在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成—些操作。
SpringBoot自带监控功能Actuator
,可以帮助实现对程序内部运行情况监控,比如
Bean加载情况
、配置属性
、使用步骤
第一步:
导入依赖坐标
<dependency>
<groupld>org.springframework.bootgroupld>
<artifactld>spring-boot-starter-actuatorartifactld>
dependency>
第二步:
访问 http://localhost:8080/actuator
案例实操
可以在application.properties
配置下面相关信息。若不配置则只能监控info
和health
的内容
# 注意:springboot2.7.2中,info端点默认是不启用,info开头的变量默认也是不启用的
info.name=zhangsan
info.age=23
# springboot2.7.2需要 启用配置里的info开头的变量
management.info.env.enabled=true
# 开启健康检查的完整信息
management.endpoint.health.show-details=always
# 将所有的监控endpoint暴露出来
management.endpoints.web.exposure.include=*
访问
(Client)
和服务端(Server)
。Spring Boot Admin Client
向为Spring Boot Admin Server
注册Spring Boot Admin Server
的 UI界面
将Spring Boot Admin Client
的 Actuator Endpoint
上的一些监控信息。操作步骤
1.admin-server:
引导类
上启用监控功能@EnableAdminServer2.admin-client:
配置相关信息
: server地址等案例实操
1. 创建admin-server模块
把服务器端中server.port改成9000,避免启动客户端和服务器两个模块时,端口冲突
2. 创建admin-client模块
3. 检测
localhost:9000
查看结果
打开应用墙
执行客户端的uerfindAll方法:localhost:8080/user/findAll
查看相关信息,可以看到findAll被访问了两次
SpringBoot 项目开发完毕后,支持两种方式部署到服务器:
右侧边栏找到Maven,找到要打包的项目。进行package
通过下面的地址,找到jar包,在目标文件的目录下,按(shlft+右键)执行java -jar命令
war
包。进行打包
当然,如果觉得名字太长,在打包的同时想给war包指定一个名字也是可以的。例如下图
war
包,把他放到一个tomcat
服务器的webapps
文件夹下执行。可以看到项目已经启动,访问项目
localhost:8080/springboot/.....