Singleton
package com.zh.ch2.scope;
import org.springframework.stereotype.Service;
/**
* Bean的Scope
* Scope:spring以何种形式创建Bean的实例。
* 1.Singleton:一个spring容器中只有一个bean的实例,该形式是spring默认的创建方式,全容器共享一个实例
* 2.Prototype:每次调用新建一个Bean的实例
* 3.Request:Web项目中,给每一个http request新建一个bean实例
* 4.Session:Web项目中,给每一个http session新建一个bean实例
* 5.GlobalSession:这个只在protal(类似spring mvc),给每一个global http session新建一个bean实例
* 在spring batch中还有一个scope事使用@StepScope。
* @author Hiiso
*/
@Service //1:默认为Singleton
public class DemoSingletonService {
}
Prototype
package com.zh.ch2.scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")//1:Protptype
public class DemoPrototypeService {
}
测试:
package com.zh.ch2.scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
//1.Singleton:一个spring容器中只有一个bean的实例,该形式是spring默认的创建方式,全容器共享一个实例
System.out.println("Singleton1==Singleton2:" + s1.equals(s2));
//2.Prototype:每次调用新建一个Bean的实例
System.out.println("prototype1==prototype2:" + p1.equals(p2));
context.close();
}
}
结果:
Singleton1==Singleton2:true
prototype1==prototype2:false
=====================================================
spring el资源调用
package com.zh.ch2.el;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Value("another attr")//1:注入普通字符串
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}
/**
* spring el 和资源调用
* 使用场景:普通文件、网址、配置文件、系统环境变量等、
* 使用:主要在注解@value的参数中使用表达式
* @author Hiiso
*
*/
package com.zh.ch2.el;
import org.apache.commons.io.IOUtils;
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;
/**
* spring el 和资源调用
* 使用场景:普通文件、网址、配置文件、系统环境变量等、
* 使用:主要在注解@value的参数中使用表达式
* @author Hiiso
*
*/
@Configuration
@ComponentScan("com.zh.ch2.el")
@PropertySource("classpath:/test.properties")//7
public class ElConfig {
@Value("I Love You!") //1注入普通字符串
private String normal;
@Value("#{systemProperties['os.name']}") //2注入操作系统属性
private String osName;
@Value("#{ T(java.lang.Math).random() * 100.0 }") //3注入表达式结果
private double randomNumber;
@Value("#{demoService.another}") //4注入其他bean属性
private String fromAnother;
@Value("classpath:/test.txt") //5注入文件资源
private Resource testFile;
@Value("http://www.baidu.com") //6注入网址资源
private Resource testUrl;
@Value("${book.name}") //7注入配置文件
private String bookName;
@Autowired
private Environment environment; //7
@Bean //7
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
}
public void outputResource() {
try {
System.out.println("//1注入普通字符串:"+normal);
System.out.println("//2注入操作系统属性:"+osName);
System.out.println("//3注入表达式结果:"+randomNumber);
System.out.println("//4注入其他bean属性:"+fromAnother);
System.out.println("//5注入文件资源:"+IOUtils.toString(testFile.getInputStream()));
System.out.println("//6注入网址资源:"+IOUtils.toString(testUrl.getInputStream()));
System.out.println("//7注入配置文件:"+bookName);
System.out.println("//7通过environment获取中获取配置文件属性:"+environment.getProperty("book.author"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.zh.ch2.el;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig resourceService = context.getBean(ElConfig.class);
resourceService.outputResource();
context.close();
}
}