本文最后更新于:June 14, 2022 pm
Spring 是目前主流的 Java Web 开发框架,是 Java 世界最为成功的框架。Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。该框架是一个轻量级的开源框架,具有很高的凝聚力和吸引力。Spring 框架不局限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 框架还是一个超级粘合平台,除了自己提供功能外,还提供粘合其他技术和框架的能力。
目录 Bean的创建方式:
前面几个注解这里就不再演示了,可以通过前面的专门讲解某一个注解的博客进行查看。
@ImportResource 目标类:
package com.SpringTestAnnotation.TestValue;public class OrderS { public void show () { System.out.println("Orders Show" ); } }
配置类:
package com.SpringTestAnnotation;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ImportResource;@ComponentScan(value = "com.SpringTestAnnotation") @ImportResource("classpath:beans.xml") public class TestConfig { }
通过注解@ImportResource指定文件名称。
配置文件beans.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:context ="http://www.springframework.org/schema/context" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id ="orderSSS" class ="com.SpringTestAnnotation.TestValue.OrderS" /> </beans >
测试:
package com.SpringTestAnnotation;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestAnnotation { public static void main (String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class); System.out.println(applicationContext.getBean("orderSSS" )); } }
输出:
com.SpringTestAnnotation.TestValue.OrderS@1cab0bfb
BeanDefinitionRegistryPostProcessor接口 目标类:
package com.SpringTestAnnotation.TestValue;public class OrderS { }
接口实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.SpringTestAnnotation.TestValue;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.support.AbstractBeanDefinition;import org.springframework.beans.factory.support.BeanDefinitionBuilder;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;import org.springframework.stereotype.Component;@Component public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition(); beanDefinition.setBeanClass(OrderS.class); beanDefinitionRegistry.registerBeanDefinition("orderS7" ,beanDefinition); } @Override public void postProcessBeanFactory (ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { } }
测试:
package com.SpringTestAnnotation;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestAnnotation { public static void main (String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class); System.out.println(applicationContext.getBean("orderS7" )); } }
结果输出:
com.SpringTestAnnotation.TestValue.OrderS@47db50c5
FactoryBean接口 目标类:
package com.SpringTestAnnotation.TestValue;public class OrderS { }
接口实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.SpringTestAnnotation.TestValue;import org.springframework.beans.factory.FactoryBean;import org.springframework.stereotype.Component;@Component public class MyFactoryBean implements FactoryBean { @Override public Object getObject () throws Exception { return new OrderS(); } @Override public Class<?> getObjectType() { return OrderS.class; } }
需要注意两个地方:
getObject()方法:new需要的对象。
getObjectType()方法:上面方法所new的对象的class。
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.SpringTestAnnotation;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestAnnotation { public static void main (String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class); System.out.println(applicationContext.getBean("myFactoryBean" )); } } com.SpringTestAnnotation.TestValue.OrderS@5c072e3f
📢注意: 获取Bean的名称是实现类的小驼峰名称!!!
当然了,也可以在注解@Component中指定Bean的名称。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.SpringTestAnnotation.TestValue;import org.springframework.beans.factory.FactoryBean;import org.springframework.stereotype.Component;@Component("orderS13") public class MyFactoryBean implements FactoryBean { @Override public Object getObject () throws Exception { return new OrderS(); } @Override public Class<?> getObjectType() { return OrderS.class; } }
获取的时候,使用Bean的时候使用指定的名称进行获取,如下:
System.out.println(applicationContext.getBean("orderS13" ));
ApplicationContext注册Bean的三种方式 目标类:
package com.SpringTestAnnotation.TestValue;public class OrderS { }
register 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.SpringTestAnnotation;import com.SpringTestAnnotation.TestValue.OrderS;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestAnnotation { public static void main (String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class); applicationContext.register(OrderS.class); System.out.println(applicationContext.getBean("orderS" )); } }
registerBean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.SpringTestAnnotation;import com.SpringTestAnnotation.TestValue.OrderS;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.function.Supplier;public class TestAnnotation { public static void main (String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class); applicationContext.registerBean("orderS" , OrderS.class, new Supplier<OrderS>() { @Override public OrderS get () { return new OrderS(); } }); System.out.println(applicationContext.getBean("orderS" )); } }
registerBeanDefinition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.SpringTestAnnotation;import com.SpringTestAnnotation.TestValue.OrderS;import org.springframework.beans.factory.support.AbstractBeanDefinition;import org.springframework.beans.factory.support.BeanDefinitionBuilder;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestAnnotation { public static void main (String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class); AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition(); beanDefinition.setBeanClass(OrderS.class); applicationContext.registerBeanDefinition("orderS" , beanDefinition); System.out.println(applicationContext.getBean("orderS" )); } }