2 spring常用配置

2 spring常用配置

在这里插入图片描述


2.1 Bean的Scope

1.Scope描述的是Spring如果新建Bean实例的
2.@Scope的5中范围
  2.1 Singleton : 一个Spring只有一个Bean实例(默认),全局共享
  2.2 Prototype : 每次调用新建一个Bean的实例
  2.3 Request   : 每个Http Request新建一个Bean实例
  2.4 Session   : 每个http Session新建一个Bean
  ——
  2.5 GobalSession: 只在portal应用中有用 

举例:

@Autowired
@Scope(name = "Session", description = "会话持有")
private SessionVo sessionVo;

问题:

Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

2.2 SpringEL和资源调用

1.Spring EL-Spring 表达式语言,支持在XML中和注解使用表达式
2.使用EL的场景
  2.1 注入普通字符
  2.2 注入操作系统属性
  2.3 注入表达式运输结果
  2.4 注入其他Bean 
  2.4 注入文件内容
  2.5 注入网站内容
  2.6 注入属性文件 

举例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
@Component
public class ValueVo {
2 spring常用配置
    //字符串注入
    @Value(value = "字符串注入")
    private String stringDI;

    //系统属性注入 #{}
    @Value(value = "#{systemEnvironment['os.name']}")
    private String sysInfoDI;

    //注入表达式 #{T()}
    @Value(value = "#{T(Math).random()}")
    private String ElDI;

    //注入其他Bean的属性 #{}
    @Value(value = "#{otherValueVo.stringEL}")
    private String otherBeanValueDI;

    //注入文件内容 
    @Value(value = "classpath:com.demo.bug.controller.ValueVo")
    private Resource fileDI;

    //注入url
    @Value(value = "http://www.baidu.com")
    private Resource urlDI;

    //注入配置文件的属性 ${}
    @Value(value = "${prealtruck_sys_jdbc_url}")
    private Resource propertyDI;

}

另外的vo

@Component
public class OtherValueVo {

    //字符串注入
    @Value(value = "字符串注入 - OtherValueVo")
    private String stringEL;

    public String getStringEL() {
        return stringEL;
    }

    public void setStringEL(String stringEL) {
        this.stringEL = stringEL;
    }
}

2.3 Bean的初始化和销毁

spring提供了两种方式去在生成Ben和销毁Bean的时候执行的方法
1.java配置
  使用@Bean的initMethod和destroyMethod
2.注解方式
  利用JSR-250的@PostConstruct和@PreDestroy

示例1:(注解方式)

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class TestVo {
    @PostConstruct
    private void init() {
        System.out.println("init");
    }

    @PreDestroy
    private void destroy() {
        System.out.println("destroy");
    }
}

示例2:(java配置)

public class TestVo2 {
    private void init() {
        System.out.println("ScopeVo2-  init");
    }

    private void destroy() {
        System.out.println("ScopeVo2 - destroy");
    }
}

import com.demo.bug.controller.TestVo2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(value = "com.demo.bug")
public class DIConfig {
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public TestVo2 grnerate() {
        return new ScopeVo2();
    }


    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DIConfig.class);
        context.getBean(TestVo2.class);
        context.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值