spring注解開發:Configuration&Bean


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM