1、使用xml创建bean的方式
1、首先新建一个maven工程,添加如下依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2、其次新建一个Person对象
package com.yefengyu.annotation.bean;
public class Person
{
private String name;
private Integer age;
public Person()
{
}
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 +
'}';
}
}
3、编写spring配置文件
<bean id="person" class="com.yefengyu.annotation.bean.Person">
<property name="name" value="yefengyu"></property>
<property name="age" value="28"></property>
</bean>
4、测试
public static void main(String[] args)
{
ApplicationContext ctx= new ClassPathXmlApplicationContext("spring.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
2、使用注解创建bean的方式
1、不需要xml文件,但是需要一个可以替换xml配置文件的配置类,也就是上面第三步可以被删除,使用如下代码:
package com.yefengyu.annotation.config;
import com.yefengyu.annotation.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//配置类等于配置文件
@Configuration//告诉spring这是一个配置类
public class MainConfig
{
@Bean//给容器注册一个bean,类型为返回值类型,id默认为方法名称
public Person person()
{
return new Person("yefengyu", 28);
}
}
2、测试
public static void main(String[] args)
{
ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
3、注意点:
(1)ApplicationContext 使用AnnotationConfigApplicationContext来获取,并且参数为配置类而非配置文件。
(2)在MainConfig配置类中,使用Bean注解给容器注册了一个bean,bean的id为方法名称person,因此可以在测试main方法中使用 person 作为bean的id获取bean。
(3)除了使用bean的id来从容器中获取bean,还可以使用bean的类型来获取,因此下面的这句
Person person = (Person) ctx.getBean("person");
可以改为如下代码,无需强转:
Person person = ctx.getBean(Person.class);
(4)如何给bean起个名字?在上面的MainConfig类中,id默认为方法名称,如何另起一个名称呢?只需要在bean注解上面加一个值,表示使用我们指定的注解,而不使用默认的注解。
@Bean("person")
public Person getPerson()
{
return new Person("yefengyu", 28);
}
上面的代码中,我们可以指定了bean的id为 person,而非默认的 getPerson。注意只要指定了bean的id,就不能使用默认的id。
(5)查看注册的bean的名称(ID)
public static void main(String[] args)
{
ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.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.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory mainConfig person
这里我们看到除了spring框架自身的bean之外,还有mainConfig,也就是注解配置实例,此外就是我们关注的person这个bean了。
来源:https://www.cnblogs.com/ye-feng-yu/p/11069865.html