spring註解開發:Configuration&Bean

一、使用xml建立bean的方式


一、首先新建一個maven工程,添加以下依賴spring

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

二、其次新建一個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 +
               '}';
    }
}

三、編寫spring配置文件maven

<bean id="person" class="com.yefengyu.annotation.bean.Person">
    <property name="name" value="yefengyu"></property>
    <property name="age" value="28"></property>
</bean>

四、測試ide

public static void main(String[] args)
{
    ApplicationContext ctx= new ClassPathXmlApplicationContext("spring.xml");
    Person person = (Person) ctx.getBean("person");
    System.out.println(person);
}

二、使用註解建立bean的方式


一、不須要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);
    }
}

二、測試ui

public static void main(String[] args)
{
     ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
    Person person = (Person) ctx.getBean("person");
    System.out.println(person);
}

三、注意點:this

(1)ApplicationContext 使用AnnotationConfigApplicationContext來獲取,而且參數爲配置類而非配置文件。spa

(2)在MainConfig配置類中,使用Bean註解給容器註冊了一個bean,bean的id爲方法名稱person,所以能夠在測試main方法中使用 person 做爲bean的id獲取bean。code

(3)除了使用bean的id來從容器中獲取bean,還能夠使用bean的類型來獲取,所以下面的這句xml

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了。

相關文章
相關標籤/搜索