编写springboot的start

默认情况下,springboot只自动加载项目main方法目录下的bean,对于依赖的jar包,并不会自动加载jar中的bean。所以有时候我们编写jar包给他人使用,需要完成jar包自动加载配置的功能,就需要编写start。

1、首先需要引入maven依赖

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
		</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-configuration-processor</artifactId>
	</dependency>

2、编写相关bean初始化代码

@Data
public class AutoServer {
    private String param1;
}

@Data
@Configuration
@ConditionalOnClass
@ConfigurationProperties(prefix = "test1")

public class MyAutoConfiguration {
    private String param1="10";
    @Bean
    @ConditionalOnClass(AutoServer.class)
    @ConditionalOnProperty(name="test1.param1",matchIfMissing = true)
    public AutoServer autoServerResolver(){
        AutoServer autoServer=new AutoServer();
        autoServer.setParam1(param1);
        return autoServer;
    }

}

2、添加spring.factories

在这里插入图片描述
内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example2.autoconfig.MyAutoConfiguration

4、编译打包
5、新项目中引入jar

		<dependency>
			<groupId>com.example2</groupId>
			<artifactId>demo2</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/lib/demo2-0.0.1-SNAPSHOT.jar</systemPath>
		</dependency>

6、编写配置文件

test1:
  param1: 11

7、在项目中使用bean

    @Autowired
    private AutoServer autoServer;
    @RequestMapping("/test1")
    public String test1(){
        return "hello test "+autoServer.getParam1();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Spring Boot整合Zookeeper的Demo代码。 首先,需要在pom.xml文件中添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> ``` 然后,在application.properties文件中配置Zookeeper的连接地址和端口号: ``` spring.cloud.zookeeper.connect-string=localhost:2181 ``` 接下来,创建一个ZookeeperServiceDiscovery类,用于向Zookeeper注册服务: ```java import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.stereotype.Component; @Component public class ZookeeperServiceDiscovery implements ServiceRegistry<Registration> { @Override public void register(Registration registration) { // 获取服务的名称和地址 String serviceName = registration.getServiceId(); String serviceAddress = registration.getHost() + ":" + registration.getPort(); // 向Zookeeper注册服务 // 这里可以使用ZooKeeper客户端库,也可以使用Curator框架 // 这里使用Curator框架进行注册 CuratorFramework client = CuratorFrameworkFactory.newClient( "localhost:2181", new RetryNTimes(3, 1000) ); client.start(); try { String path = "/services/" + serviceName + "/" + serviceAddress; client.create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(path); } catch (Exception e) { e.printStackTrace(); } } @Override public void deregister(Registration registration) { // 获取服务的名称和地址 String serviceName = registration.getServiceId(); String serviceAddress = registration.getHost() + ":" + registration.getPort(); // 从Zookeeper注销服务 // 这里可以使用ZooKeeper客户端库,也可以使用Curator框架 // 这里使用Curator框架进行注销 CuratorFramework client = CuratorFrameworkFactory.newClient( "localhost:2181", new RetryNTimes(3, 1000) ); client.start(); try { String path = "/services/" + serviceName + "/" + serviceAddress; client.delete().forPath(path); } catch (Exception e) { e.printStackTrace(); } } @Override public void close() { } @Override public void setStatus(Registration registration, String status) { } @Override public <T> T getStatus(Registration registration) { return null; } @Override public Iterable<Registration> getRegistrations() { return null; } @Override public Registration getRegistration(String s) { return null; } } ``` 最后,在启动类中加入@EnableDiscoveryClient注解,启用服务注册和发现功能: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样就完成了一个简单的Spring Boot整合Zookeeper的Demo。注意,这里只是演示了服务注册功能,如果需要使用服务发现功能,还需要在客户端中加入@EnableDiscovery注解,并使用@LoadBalanced注解来实现负载均衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值