Configuration&Bean
应用XML形式
首先新建一个maven工程,增加如下依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version></dependency>
其次新建一个bean
package com.yefengyu.spring.test.bean;public class Person { private String name; private Integer age; public Person(String name, Integer age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; }}
再次创立一个xml配置文件:spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.yefengyu.spring.test.bean.Person"> <property name="age" value="30"/> <property name="name" value="yefengyu"/> </bean> </beans>
最初测试
package com.yefengyu.spring.test.bean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); Person person = (Person) ctx.getBean("person"); System.out.println(person);//Person{name='yefengyu', age=30} }}
应用注解的形式
不须要xml文件,然而须要一个能够替换xml配置文件的配置类
package com.yefengyu.spring.test.bean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;//配置类等于配置文件@Configuration//通知spring这是一个配置类public class Config { @Bean//给容器注册一个bean,类型为返回值类型,id默认为办法名称 public Person person() { return new Person("yefengyu", 28); }}
测试
package com.yefengyu.spring.test.bean;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Client { public static void main(String[] args) { ApplicationContext ctx= new AnnotationConfigApplicationContext(Config.class); Person person = ctx.getBean(Person.class); System.out.println(person);//Person{name='yefengyu', age=30} }}
下面是用的bean的类型来获取,也能够应用bean的名称,也就是以前配置文件的id
Person person = (Person) applicationContext.getBean("person");
留神:
- 首先id默认为办法名称,也就是Config类 中的person办法
- 其次也能够不和办法名称统一,这个时候次要在bean的注解上加上一个值,如下所示,示意不应用默认形式,咱们给bean自定义ID
@Configurationpublic class Config { @Bean("getPerson") public Person person() { return new Person("yefengyu", 30); }}
此时如果使ID来获取bean,只能是 getPerson ,不是应用person,你指定了ID,就不能应用默认的ID。
最初阐明下:
在xml中bean的id也就是bean的名称,只是一个标识,在xml中应用id这个属性而不是用name,然而咱们能够通过一个办法来获取,依据这个办法的名称咱们猜想id和名称表白的意思很相近,就是注解和xml配置中惟一标识bean的标记。
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); String[] names = ctx.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); }}
后果如下:
org.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.event.internalEventListenerProcessororg.springframework.context.event.internalEventListenerFactoryconfigperson