@ComponentScan(value = "com.xuyuan")
public class AppConfig {
}
public class SendService {
public void test(){
System.out.println("send");
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// 生成一个BeanDefinition对象,并设置beanClass为MoneyService.class,并注册到ApplicationContext中
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(MoneyService.class);
beanDefinition.setScope("prototype");
context.registerBeanDefinition("moneyService",beanDefinition);
MoneyService moneyService = (MoneyService)context.getBean("moneyService");
MoneyService moneyService1 = (MoneyService)context.getBean("moneyService");
System.out.println(moneyService);
System.out.println(moneyService1);
}
运行结果:
com.xuyuan.service.MoneyService@2e6a8155 com.xuyuan.service.MoneyService@6221a451 |
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
AnnotatedBeanDefinitionReader annotatedBeanDefinitionReader = new AnnotatedBeanDefinitionReader(context);
annotatedBeanDefinitionReader.register(MoneyService.class);
MoneyService moneyService = (MoneyService)context.getBean("moneyService");
System.out.println(moneyService);
}
运行结果:
com.xuyuan.service.MoneyService@2e6a8155 |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="moneyService" class="com.xuyuan.service.MoneyService"/>
</beans>
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(context);
int i = xmlBeanDefinitionReader.loadBeanDefinitions("spring.xml");
System.out.println(context.getBean("moneyService"));
}
运行结果:
com.xuyuan.service.MoneyService@563f38c4 |
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.refresh();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
int scan = scanner.scan("com.xuyuan");
System.out.println(context.getBean("moneyService"));
}
运行结果:
com.xuyuan.service.MoneyService@8646db9 |
public static void main(String[] args) {
DefaultListableBeanFactory beanfactory = new DefaultListableBeanFactory();
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(MoneyService.class);
beanfactory.registerBeanDefinition("moneyService",beanDefinition);
System.out.println(beanfactory.getBean("moneyService"));
}
运行结果:
com.xuyuan.service.MoneyService@7b1d7fff |
国际化
@ComponentScan(value = "com.xuyuan")
public class AppConfig {
@Bean
public MessageSource messageSource(){
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//国际化
String message = context.getMessage("test", null, new Locale("en"));
System.out.println(message);
}
messages.properties | messages_en.properties |
test=a | test=b |
运行结果:
b |
资源加载
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Resource resource = context.getResource("spring.properties");
try {
File file = resource.getFile();
URL url = resource.getURL();
String file1 = url.getFile();
if(file.isFile()){
System.out.println("file");
}
System.out.println("----------"+resource.contentLength());
} catch (IOException e) {
e.printStackTrace();
}
}
spring.properties
a=1
b=2
c=3
运行结果:
file ----------13 |
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Resource resource = context.getResource("file://D:\\zyyangy\\workspace\\self_growth\\spring-test-demo\\src\\main\\java\\com\\xuyuan\\AppConfig.java");
System.out.println(resource.contentLength());
System.out.println(resource.getFilename());
Resource resource1 = context.getResource("https://www.baidu.com");
System.out.println(resource1.contentLength());
System.out.println(resource1.getURL());
Resource resource2 = context.getResource("classpath:spring.xml");
System.out.println(resource2.contentLength());
System.out.println(resource2.getURL());
}
运行结果:
4305 AppConfig.java 2443 https://www.baidu.com 743 file:/D:/zyyangy/workspace/self_growth/spring-test-demo/target/classes/spring.xml |
运行时环境
@PropertySource("classpath:spring.properties")
@ComponentScan(value = "com.xuyuan")
public class AppConfig {
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Map<String, Object> systemEnvironment = context.getEnvironment().getSystemEnvironment();
System.out.println(systemEnvironment);
System.out.println("=======");
Map<String, Object> systemProperties = context.getEnvironment().getSystemProperties();
System.out.println(systemProperties);
System.out.println("=======");
//包含最全的信息
MutablePropertySources propertySources = context.getEnvironment().getPropertySources();
System.out.println(propertySources);
System.out.println("=======");
System.out.println(context.getEnvironment().getProperty("NO_PROXY"));
System.out.println(context.getEnvironment().getProperty("sun.jnu.encoding"));
System.out.println(context.getEnvironment().getProperty("a"));
}
运行结果:
[PropertiesPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}, ResourcePropertySource {name='class path resource [spring.properties]'}] Process finished with exit code 0 |
事件发布
@ComponentScan(value = "com.xuyuan")
public class AppConfig {
@Bean
public ApplicationListener applicationListener(){
return new ApplicationListener() {
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("接收到一个事件"+event.getSource());
}
};
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.publishEvent("start event");
}
运行结果:
接收到一个事件org.springframework.context.annotation.AnnotationConfigApplicationContext@7a07c5b4, started on Wed Mar 20 15:49:15 CST 2024 接收到一个事件org.springframework.context.annotation.AnnotationConfigApplicationContext@7a07c5b4, started on Wed Mar 20 15:49:15 CST 2024 |
类型转换
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StringToUserPropertyEditor extends PropertyEditorSupport implements PropertyEditor {
@Override
public void setAsText(String text) throws java.lang.IllegalArgumentException {
User user = new User();
user.setName(text);
this.setValue(user);
}
}
public static void main(String[] args) {
StringToUserPropertyEditor propertyEditor = new StringToUserPropertyEditor();
propertyEditor.setAsText("1");
User user = (User) propertyEditor.getValue();
System.out.println(user);
System.out.println(user.getName());
}
运行结果:
com.xuyuan.service.User@4d7e1886 1 |
@ComponentScan(value = "com.xuyuan")
public class AppConfig {
@Bean
public CustomEditorConfigurer customEditorConfigurer(){
CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
HashMap<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();
// 表示StringToUserPropertyEditor可以将String转化成User类型,在Spring源码中,如果发现当前对象是String,而需要的类型是User,就会使用该PropertyEditor来做类型转化
propertyEditorMap.put(User.class,StringToUserPropertyEditor.class);
customEditorConfigurer.setCustomEditors(propertyEditorMap);
return customEditorConfigurer;
}
}
@Component
public class PayService {
@Value("xuanyuan")
private User user;
public void test(){
System.out.println(user.getName());
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
PayService payService = (PayService) context.getBean("payService");
payService.test();
}
运行结果:
xuanyuan |
public class StringToUserConverter implements ConditionalGenericConverter {
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return sourceType.getType().equals(String.class) && targetType.getType().equals(User.class);
}
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class,User.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
User user = new User();
user.setName((String) source);
return user;
}
}
public static void main(String[] args) {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new StringToUserConverter());
User value = conversionService.convert("1", User.class);
System.out.println(value.getName());
}
运行结果:
1 |
@ComponentScan(value = "com.xuyuan")
public class AppConfig {
@Bean
public ConversionServiceFactoryBean conversionService(){
ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
conversionServiceFactoryBean.setConverters(Collections.singleton(new StringToUserConverter()));
return conversionServiceFactoryBean;
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
PayService payService = (PayService) context.getBean("payService");
payService.test();
}
运行结果:
xuanyuan |
public static void main(String[] args) {
SimpleTypeConverter typeConverter = new SimpleTypeConverter();
typeConverter.registerCustomEditor(User.class,new StringToUserPropertyEditor());
// typeConverter.setConversionService(conversionService);
User user = typeConverter.convertIfNecessary("1", User.class);
System.out.println(user.getName());
}
}
运行结果:
1 |
比较器
//@Order(3)
public class A implements Ordered {
@Override
public int getOrder() {
return 3;
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
//@Order(2)
public class B implements Ordered {
@Override
public int getOrder() {
return 2;
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
public static void main(String[] args) {
A a = new A();
B b = new B();
OrderComparator comparator = new OrderComparator();
System.out.println(comparator.compare(a,b));
List list = new ArrayList<>();
list.add(a);
list.add(b);
list.sort(comparator);
System.out.println(list);
}
运行结果:
1 [B, A] |
public static void main(String[] args) {
A a = new A();
B b = new B();
AnnotationAwareOrderComparator comparator = new AnnotationAwareOrderComparator();
System.out.println(comparator.compare(a,b));
List list = new ArrayList<>();
list.add(a);
list.add(b);
list.sort(comparator);
System.out.println(list);
}
运行结果:
1 [com.xuyuan.service.Order.B@4dcbadb4, com.xuyuan.service.Order.A@4e515669] |
@Service
public class MoneyService {
class person{
}
}
public static void main(String[] args) throws IOException {
SimpleMetadataReaderFactory simpleMetadataReaderFactory = new SimpleMetadataReaderFactory();
//构造一个MetadataReader
MetadataReader metadataReader = simpleMetadataReaderFactory.getMetadataReader("com.xuyuan.service.MoneyService");
//得到一个ClassMetadata,并获取类名
ClassMetadata classMetadata = metadataReader.getClassMetadata();
System.out.println(classMetadata.getClassName());
System.out.println(classMetadata.getInterfaceNames());//实现类
System.out.println(classMetadata.getMemberClassNames()[0]);
//获取一个AnnotationMetadata,并获取类上的注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
System.out.println(annotationMetadata.hasMetaAnnotation(Component.class.getName()));
System.out.println(annotationMetadata.hasAnnotatedMethods(Component.class.getName()));
for (String annotationType : annotationMetadata.getAnnotationTypes()) {
System.out.println(annotationType);
}
}
运行结果:
com.xuyuan.service.MoneyService [Ljava.lang.String;@7637f22 com.xuyuan.service.MoneyService$person true false org.springframework.stereotype.Service |
@ComponentScan(value = "com.xuyuan",excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = UserService.class)})
public class AppConfig {
}
@Component
public class UserService{
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("userService"));
}
运行结果:
No bean named 'userService' available |
@ComponentScan(value = "com.xuyuan",includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = UserService.class)})
public class AppConfig {
}
public class UserService {
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("userService"));
}
运行结果:
com.xuyuan.service.UserService@2c1b194a |
public class CommodityService {
public void test(){
System.out.println("商品");
}
}
@Component
public class XuanYuanFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
CommodityService commodityService = new CommodityService();
return commodityService;
}
@Override
public Class<?> getObjectType() {
return CommodityService.class;
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("xuanYuanFactoryBean"));
System.out.println(context.getBean("&xuanYuanFactoryBean"));
}
com.xuyuan.service.CommodityService@6d4e5011 com.xuyuan.service.XuanYuanFactoryBean@57d7f8ca |
@Component("commodityService")
public class XuanYuanFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
CommodityService commodityService = new CommodityService();
return commodityService;
}
@Override
public Class<?> getObjectType() {
return CommodityService.class;
}
}
@Component
public class XuanYuanBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if("commodityService".equals(beanName)){
System.out.println("初始化前"+bean);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if("commodityService".equals(beanName)){
System.out.println("初始化后"+bean);
}
return bean;
}
}
public class CommodityService {
public void test(){
System.out.println("商品");
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("commodityService"));
}
运行结果:
初始化前com.xuyuan.service.XuanYuanFactoryBean@41a0aa7d 初始化后com.xuyuan.service.CommodityService@795509d9 |
@Component
public class XuanYanBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("加工beanFactory"+beanFactory);
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("commodityService"));
}
加工beanFactoryorg.springframework.beans.factory.support.DefaultListableBeanFactory@544fe44c 初始化前com.xuyuan.service.XuanYuanFactoryBean@41a0aa7d 初始化后com.xuyuan.service.CommodityService@795509d9 |