注解说明:
@Configuration :告诉spring这是一个配置类,相当于以前的spring的配置文件;
@Bean :告诉spring给容器中注册一个bean,bean的别名默认为方法的名字,也可以指定其它的名字,方式是在给@Bean指定值,如:@Bean("lisi")。
一、没有使用注解方式加载配置文件,也就是用配置文件加载bean
- 加载实体代码
package com.guang.entity;
/**
* @ClassName Person
* @author xxx
* @date xxx
* @version xxx
*/
public class Person {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer 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;
}
public Person() {}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
2. 加载spring配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.guang.entity.Person">
<property name="name" value="aiha"></property>
<property name="age" value="25"></property>
</bean>
</beans>
3. 调用的时候如下
package com.guang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.guang.config.Myconfig;
import com.guang.entity.Person;
/**
* @ClassName PersonTest
* @author xxx
* @date xxx
* @version xxx
*/
public class PersonTest {
private static ApplicationContext ac;
private static Person bean;
@Test
public void xmlApplicateonContextTest() {
ac = new ClassPathXmlApplicationContext("beans.xml");
bean = (Person) ac.getBean("person");
System.out.println(bean.toString());
}
}
二、使用注解加载配置类,及向spring的IOC容器中加载bean
- 实体类代码
package com.guang.entity;
/**
* @ClassName Person
* @author xxx
* @date xxx
* @version xxx
*/
public class Person {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer 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;
}
public Person() {}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
2. 配置类代码
package com.guang.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.guang.entity.Person;
@Configuration // 相当于spring的配置文件bean.xml
public class Myconfig {
@Bean("person") // 相当于配置文件中的配置的bean
public Person person01() {
return new Person("aihai", 25);
}
}
3. 利用注解加载配置类,注入bean到IOC容器中
package com.guang.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.guang.config.Myconfig;
import com.guang.entity.Person;
/**
* @ClassName PersonTest
* @author xxx
* @date xxx
* @version xx
*/
public class PersonTest {
private static ApplicationContext ac;
private static Person bean;
@Test
public void annotationApplicateonContextTest() {
ac = new AnnotationConfigApplicationContext(Myconfig.class);
bean = (Person) ac.getBean("person");
System.out.println(bean.toString());
}
}
三、关于ClasspathXmlApplicationContext和AnnotationConfigApplication的实现图如下
他们都实现了ApplicationContext接口,同时也都实现了抽象类AbstracApplicationContext,内容上下文中的大部分方法都在里面可查询到,如下图