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

相关文章
相关标签/搜索