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
@Configuration
public 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.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
person
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。