Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 40 章 Spring 框架

目录

40.1. @Bean
40.1.1. @Scope 定义类型
40.1.2. InitializingBean
40.2. URL 拼装/解析
40.3. ServletUriComponentsBuilder
40.4. URL 路径相关

Spring框架为基于Java的现代企业级应用程序提供了全面的编程和配置模型——适用于任何类型的部署平台。

Spring框架的关键要素是应用程序级别的基础设施支持:Spring专注于企业级应用程序的“管道”,以便团队可以专注于应用程序级别的业务逻辑,而无需与特定的部署环境产生不必要的联系。

40.1. @Bean

	
package cn.netkiller.config;

import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Configuration
@Data
public class Config {

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    public MyBean myBean2() {
        return new MyBean();
    }

    @Bean({"myBean4", "myBean5"})
    @Scope("prototype")
    public MyBean myBean3() {
        return new MyBean();
    }

    public class MyBean {

        public MyBean() {
            System.out.println("MyBean Initializing");
        }

        public String output() {
            return "Helloworld!!!";
        }

        public void init() {
            System.out.println("Bean 初始化方法被调用");
        }

        public void destroy() {
            System.out.println("Bean 销毁方法被调用");
        }
    }
}

	
		

查看注入情况

	
neo@MacBook-Pro-M2 ~/w/watch (main)> curl -s 'http://neo:chen@localhost:8080/actuator/beans' |jq | grep -i mybean -A 5
        "myBean2": {
          "aliases": [],
          "scope": "singleton",
          "type": "cn.netkiller.config.Config$MyBean",
          "resource": "class path resource [cn/netkiller/config/Config.class]",
          "dependencies": [
            "config"
          ]
        },
--
        "myBean4": {
          "aliases": [
            "myBean5"
          ],
          "scope": "prototype",
          "type": "cn.netkiller.config.Config$MyBean",
          "resource": "class path resource [cn/netkiller/config/Config.class]",
          "dependencies": []
        },
        "dbHealthContributor": {
          "aliases": [],
--
        "myBean": {
          "aliases": [],
          "scope": "singleton",
          "type": "cn.netkiller.config.Config$MyBean",
          "resource": "class path resource [cn/netkiller/config/Config.class]",
          "dependencies": [
            "config"
          ]
        },	
	
		

40.1.1. @Scope 定义类型

		
    @Bean({"neo","netkiller"})
    @Scope("prototype")
    public MyBean myBean(){
        return new MyBean();
    }		
		
			
		
Scope		详解
singleton	默认单例的bean定义信息,对于每个IOC容器来说都是单例对象
prototype	bean对象的定义为任意数量的对象实例
request		bean对象的定义为一次HTTP请求的生命周期,也就是说,每个HTTP请求都有自己的bean实例,它是在单个bean定义的后面创建的。仅仅在web-aware的上下文中有效
session		bean对象的定义为一次HTTP会话的生命周期。仅仅在web-aware的上下文中有效
application	bean对象的定义范围在ServletContext生命周期内。仅仅在web-aware的上下文中有效
websocket	bean对象的定义为WebSocket的生命周期内。仅仅在web-aware的上下文中有效
		
			

singleton和prototype 一般都用在普通的Java项目中,而request、session、application、websocket都用于web应用中。

		
	@Bean
    @Scope(value = "singleton")
    public Person person(){
        return new Person();
    }		
		
			
		
ConfigurableBeanFactory.SCOPE_PROTOTYPE,即“prototype”
ConfigurableBeanFactory.SCOPE_SINGLETON,即“singleton”
WebApplicationContext.SCOPE_REQUEST,即“request”
WebApplicationContext.SCOPE_SESSION,即“session”
		
			

40.1.2. InitializingBean

afterPropertiesSet() 在 @Value 完成之后执行

		
package cn.netkiller.services;

import lombok.Data;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Data
@Slf4j
public class TestService implements InitializingBean {

    private String appId;

    public TestService(@Value("${app.id}") String appId) {
        this.appId = appId;
        log.info(this.appId);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info(this.appId);
    }
}