由 Spring进行管理 scope 默认是 一个容器中只有一个bean
* (1) Singleton:一个 Spring容器中只有一个Bean的实例,此为 Spring的默认配置,全局容器共享一个实例。
2) Prototype:每次调用新建一个Bean的实例。
(3)Request:web项目中,给每一个httprequest新建一个Bean实例。
(4)Session:Web项目中,给每一个httpsession新建一个Bean实例。
(5)GlobalSession:这个只在portal应用中有用,给每一个globalhttpsession新建
单例类 和 每次调用 创建一个类
package spring.scope8EL8Value.Scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
/**
* Created by lovejing on 2020/6/16.
*
* 由 Spring进行管理 scope 默认是 一个容器中只有一个bean
* (1) Singleton:一个 Spring容器中只有一个Bean的实例,此为 Spring的默认配置,全局容器共享一个实例。
2) Prototype:每次调用新建一个Bean的实例。
(3)Request:web项目中,给每一个httprequest新建一个Bean实例。
(4)Session:Web项目中,给每一个httpsession新建一个Bean实例。
(5)GlobalSession:这个只在portal应用中有用,给每一个globalhttpsession新建
实例
*/
@Service
@Scope("singleton")
public class ScopeSingleService {
String param1;
}
package spring.scope8EL8Value.Scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
/**
* Created by lovejing on 2020/6/16.
* Prototype:每次调用新建一个Bean的实例。
*/
@Service
@Scope("prototype")
public class ScopeProtorypeService {
}
配置类
package spring.scope8EL8Value.Scope;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by lovejing on 2020/6/16.
* 声明配置类 配置扫描包
*/
@Configuration
@ComponentScan("spring")
public class ConfigJava {
}
main 测试
package spring.scope8EL8Value.Scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by lovejing on 2020/6/16.
*/
public class MainJava {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigJava.class);// 加载容器 指定配置类
ScopeSingleService singleService = context.getBean(ScopeSingleService.class);
ScopeSingleService singleService1 = context.getBean(ScopeSingleService.class);
ScopeProtorypeService scopeProtorypeService = context.getBean(ScopeProtorypeService.class);
ScopeProtorypeService scopeProtorypeService2 = context.getBean(ScopeProtorypeService.class);
System.out.println("single注解:"+singleService.equals(singleService1));
System.out.println("Protorype注解:"+scopeProtorypeService.equals(scopeProtorypeService2));
}
}
运行结果:
@Value 使用
package spring.scope8EL8Value.value;
import org.apache.commons.io.IOUtils;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import java.io.IOException;
/**
* Created by lovejing on 2020/6/16.
*/
@Configuration
@ComponentScan("spring")
//@PropertySource("classpath:spring/scope8EL8Value/value/test.txt")
public class ValueConfigJava {
@Value("注入普通字符串") // 注入普通字符串
private String name;
// @Value("#{SystemProperties['os.name']}")// 注入操作系统属性
// private String osName;
@Value("#{T(java.lang.Math).random()*333.1}")
private double number;
// @Value("classpath:spring/scope8EL8Value/value/test.txt")
// private Resource textFile;
// @Value("${aaa.bb}")
// private String param;
@Value("http://www.baidu.com")
private Resource resource;
@Autowired
private Environment environment;
@Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
public void outPrint() throws IOException {
System.out.println(name);
// System.out.println(osName);
System.out.println(number);
// System.out.println(IOUtils.toString(textFile.getInputStream()));
// System.out.println(param);
System.out.println(environment.getProperty("aaa.bb"));
System.out.println(IOUtils.toString(resource.getInputStream()));
}
}
main 运行:
package spring.scope8EL8Value.value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
/**
* Created by lovejing on 2020/6/16.
*/
public class MainJAVA {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ValueConfigJava.class);
ValueConfigJava bean = context.getBean(ValueConfigJava.class);
bean.outPrint();
context.close();
}
}
运行结果:
bean 的初始化和销毁
写一个bean ,随便写两个方法,然后用注解的@Bean 的时候进行关联
package spring.bean;
/**
* Created by lovejing on 2020/6/16.
*/
public class ThisIsBean {
String initS = "";
public String getDestory() {
return destory;
}
public String getInitS() {
return initS;
}
public void setInitS(String initS) {
this.initS = initS;
}
public void setDestory(String destory) {
this.destory = destory;
}
String destory = "";
public String initMethod(){
System.out.println("bean初始化");
initS = "初始化"; // 可以利用初始化给一下 变量赋值
return initS;
}
public String destoryMethod(){
System.out.println("bean destory");
destory = "destory";
return destory;
}
}
使用@Bean 关联方法
package spring.bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by lovejing on 2020/6/16.
*/
@Configuration
@ComponentScan("spring.bean")
public class ConfigBean {
// initMethod = "initMethod",destroyMethod = "destoryMethod" 分别是关联了 bean 对象ThisIsBean 中的两个方法
@Bean(initMethod = "initMethod",destroyMethod = "destoryMethod")
ThisIsBean thisIsBean(){
return new ThisIsBean();
}
}
main 运行:
package spring.bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by lovejing on 2020/6/16.
*/
public class javaMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigBean.class);
ThisIsBean bean = context.getBean(ThisIsBean.class);
ThisIsBean thisIsBean = new ThisIsBean();
String 获取到的容器bean的值 = bean.initS;
String initS = thisIsBean.getInitS();
System.out.println("new的值"+initS);
System.out.println("获取到的容器bean的值"+获取到的容器bean的值);
context.close();
}
}
运行结果: