在配置文件中指定要注入的bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.ttpfx.entity.Dog">
<property name="name" value="旺财"/>
<property name="age" value="18"/>
bean>
beans>
然后spring加载这个xml文件就可以实现注入
public class SpringTest1 {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
}
}
输出为
dog
编写xml配置文件,里面指定要扫描的包
<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">
<context:component-scan base-package="com.ttpfx.entity.t2"/>
beans>
然后在要注入的bean上加入Component注解即可(如果里面方法上面有@Bean,那么也会进行处理)
@Component
public class Cat {
}
public class SpringTest2 {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext2.xml");
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
}
}
输出为
cat
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
可以不使用xml文件,通过@ComponentScan注解来完成定义扫描路径的功能
@ComponentScan(basePackages = "com.ttpfx.entity.t3")
public class SpringConfig3 {
}
public class SpringTest3 {
public static void main(String[] args) {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig3.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
}
}
使用@ComponentScan也会将自身加入到容器中。我们可以在方法上加入@Bean来进行注入,具体如下
二者用法基本一样,只不过@Configuration可以控制注入的Bean是不是一个代理对象,如果是代理对象,那么调用@Bean方法返回的都是同一个对象,否则就不是同一个对象。
在默认情况下,@Configuration注入的对象是一个代理对象
默认情况,proxyBeanMethods = true
@Configuration(proxyBeanMethods = true)
public class Cat {
@Bean
public Cat bigCat() {
return new Cat();
}
}
得到这个对象,然后调用bigCat这个方法
public class SpringTest2 {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext2.xml");
Cat cat = ioc.getBean("cat", Cat.class);
System.out.println(cat);
Cat bigCat1 = cat.bigCat();
Cat bigCat2 = cat.bigCat();
System.out.println("---------------------");
System.out.println(bigCat1);
System.out.println(bigCat2);
System.out.println(bigCat1 == bigCat2);
}
}
这时返回Cat已经是一个代理对象了,bigCat返回的都是同一个对象,就是单例模式的。
com.ttpfx.entity.t2.Cat$$EnhancerBySpringCGLIB$$bc3ad26b@4c1d9d4b
---------------------
com.ttpfx.entity.t2.Cat@7b227d8d
com.ttpfx.entity.t2.Cat@7b227d8d
true
如果将proxyBeanMethods 改成false,情况如下
@Configuration(proxyBeanMethods = false)
public class Cat {
@Bean
public Cat bigCat() {
return new Cat();
}
}
其他代码不变,可以发现没有进行代理。
com.ttpfx.entity.t2.Cat@62fdb4a6
---------------------
com.ttpfx.entity.t2.Cat@11e21d0e
com.ttpfx.entity.t2.Cat@1dd02175
false
如果使用@Component,那么就相当于@Configuration的proxyBeanMethods 设置为false
我们可以让一个类实现FactoryBean,这个接口有一个getObject方法,如果一个使用@Bean标记的方法返回FactoryBean,那么最终返回的是FactoryBean的getObject方法返回的值
public class PeopleFactory implements FactoryBean<People> {
@Override
public People getObject() throws Exception {
return new People();
}
@Override
public Class<?> getObjectType() {
return People.class;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
@Component
public class People {
@Bean
public PeopleFactory peopleFactory(){
return new PeopleFactory();
}
}
此时获取peopleFactory,它的类型如下,是一个People类型
com.ttpfx.entity.t3.People@587c290d
可以使用@Import进行导入
@Import({User.class})
public class SpringConfig4 {
}
public class SpringTest4 {
public static void main(String[] args) {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig4.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
}
}
输出如下,可以发现使用@Import标注的类也会被注入。使用@Import导入的类,名称为全类名,如果重复导入,那么后面覆盖前面。要指定名称,那么就在对应的bean上面使用@Component即可
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig4
com.ttpfx.entity.t4.User
可以直接通过GenericApplicationContext这个类的registerBean方法进行注入
public class SpringTest5 {
public static void main(String[] args) {
AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig5.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
System.out.println("-----------------------");
ioc.registerBean("monster01", Monster.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
}
}
输出如下
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig5
-----------------------
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig5
monster01
定义一个类实现ImportSelector
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.ttpfx.entity.t6.Pig"};
}
}
@Import({MyImportSelector.class})
public class SpringConfig6 {
}
public class SpringTest6 {
public static void main(String[] args) {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig6.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
}
}
输出如下,可以发现selectImports返回的String字符串中的内容会进行注入
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig6
com.ttpfx.entity.t6.Pig
通过ImportBeanDefinitionRegistrar可以进行注入,只需要在registerBeanDefinitions方法中使用BeanDefinitionRegistry的registerBeanDefinition方法即可
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry, importBeanNameGenerator);
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Manager.class).getBeanDefinition();
registry.registerBeanDefinition("manager", beanDefinition);
ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);
}
}
public class SpringTest7 {
public static void main(String[] args) {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig7.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
Manager bean = ioc.getBean(Manager.class);
System.out.println(bean);
}
}
代码输出如下
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig7
manager
com.ttpfx.entity.t7.Manager@1d8d30f7
实现这个接口,通过方法上面的参数可以进行注入
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Employee.class).getBeanDefinition();
registry.registerBeanDefinition("employee", beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
@Import({MyBeanDefinitionRegistryPostProcessor.class})
public class SpringConfig8 {
}
public class SpringTest8 {
public static void main(String[] args) {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig8.class);
Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
System.out.println("-----------------");
Employee bean = ioc.getBean(Employee.class);
System.out.println(bean);
}
}
输出如下,可以发现实现BeanDefinitionRegistryPostProcessor的这个类也被注入了
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig8
com.ttpfx.entity.t8.MyBeanDefinitionRegistryPostProcessor
employee
-----------------
com.ttpfx.entity.t8.Employee@58c1670b