• 一、Spring之组件注册-@Configuration&@Bean给容器中注册组件


    xml配置方式

    首先我们创建一个实体类Person

    public class Person {
    	private String name;
    	private Integer age;
    	private String nickName;
     // 省略getter and setter ,tostring ,Constructor   
    1}
    

    以往,我们在spring的配置文件中注册一个bean,采用以下写法;

    <?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"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    	<bean id="person1" class="com.atguigu.bean.Person" >
    		<property name="age" value="24"></property>
    		<property name="name" value="zhangsan"></property>
    	</bean>
    	
    </beans>
    

    写个测试类

    public class MainTest {
    	
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    		Person bean = (Person) applicationContext.getBean("person1");
    		System.out.println(bean);
    	
    	}
    
    }
    
    

    控制台打印结果:

    Person [name=zhangsan, age=24, nickName=null]
    

    nickName我们没有注入,所以为null,

    可以看到我们已经成功在spring的ioc容器中注入了这个Person对象

    采用javaConfig的方式注入

    创建一个配置类,作用等同于spring的配置文件

    @Configuration  //告诉Spring这是一个配置类
    public class MainConfig {
    	
    	//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
    	@Bean("person")
    	public Person person01(){
    		return new Person("lisi", 20);
    	}
    
    }
    
    

    写一个测试类

    public class MainTest {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    //		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    //		Person bean = (Person) applicationContext.getBean("person");
    //		System.out.println(bean);
    		
    		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
    		Person bean = applicationContext.getBean(Person.class); // 获取Bean,注意这里的容器是AnnotationConfigApplicationContext
    		System.out.println(bean); // 输出Bean
    		
            //获取类型为Person.class的容器中所有的bean,
    		String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
           // 遍历
    		for (String name : namesForType) {
    			System.out.println(name);
    		}
    	
    	}
    
    }
    

    控制台输出结果,发现已经成功注入了

    Person [name=lisi, age=20, nickName=null]
    person
    

    通过配置类的形式注入bean,那bean的名字默认为方法名的小写,即person01,如何手动指定bean的名字呢?

    @Bean("person") // 指定bean的名字为person,如果不写为方法名的小写。
    // 以下两种写法也是可以的
    //	@Bean(value ="person")
    //	@Bean(name = "person")
    
    你所看得到的天才不过是在你看不到的时候还在努力罢了!
  • 相关阅读:
    [PA2014]Muzeum
    [TJOI2015]概率论
    To Do List
    【洛谷4172】 [WC2006]水管局长(LCT)
    HNOI2019退役记
    hdu 2159 FATE
    USACO 2019 January Contest, Platinum 题解
    luogu4774 [NOI2018]屠龙勇士
    NOI 2019游记
    loj #3145. 「APIO 2019」桥梁
  • 原文地址:https://www.cnblogs.com/heliusKing/p/11343471.html
Copyright © 2020-2023  润新知