從零開始Spring註解驅動開發-組件註冊(@Configuration和@Bean)

1.經過傳統配置文件(xml)的方式來實現Spring容器中的組件註冊

項目工程目錄結構:java

1.1 建立maven工程,pom文件導入spring-context(核心組件包)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.stark</groupId>
  <artifactId>SpringOne</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringOne</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.4.RELEASE</version>
    </dependency>


  </dependencies>
</project>

1.2 在com/stark/bean下建立Person類

package com.stark.bean;

public class Person {

    private Integer id;

    private String name;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Person() {

    }
}

1.3 在src下建立spring的配置文件beans.xml,將person類註冊到容器中

<?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組件-->
    <bean id="person" class="com.stark.bean.Person">
        <property name="id" value="1"/>
        <property name="name" value="張三"/>
        <property name="age" value="20"/>
    </bean>
</beans>

1.4經過配置文件方式讀取bean測試

package com.stark;
import com.stark.bean.Person;
import com.stark.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

    public static void main(String args[]){
        //實例化容器
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        //獲取組件
        Person person=(Person)context.getBean("person");

        System.out.println(person);
      
    }
}

1.5 能夠看下面獲取bean組件的打印信息

2 經過註解方式來實現Spring容器中的組件註冊

2.1 在com/stark/config下建立一個配置類MainConfig

package com.stark.config;
import com.stark.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //告訴Spring 這是一個註解類
public class MainConfig {
    @Bean(value = "persion1") //告訴Spring這是一個bean組件,類型爲返回值的類型,id默認爲方法名
    public Person person(){
        return new Person(1,"張三",20);
    }
}

2.2經過註解方式讀取bean測試

package com.stark;
import com.stark.bean.Person;
import com.stark.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

    public static void main(String args[]){

        //註解方式 獲取
        //實例化容器
        ApplicationContext contexts=new AnnotationConfigApplicationContext(MainConfig.class);
        //獲取組卷
        Person person1=contexts.getBean(Person.class);

        System.out.println(person1);

    }
}

總結:經過註解方式實現的bean註冊,遠遠要比傳統配置 文件方式方便的多,主要用到了@Configuration和@Beanspring

  帶有@Configuration註解的MainConfig類 代替了 Beans.xml配置文件,會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器.apache

@Configuration會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,
並用於構建bean定義,初始化Spring容器

帶有@Bean註解的方法代替了配置方式<bean id="" class=""></bean>,maven

@Bean是一個方法級別上的註解,主要用在@Configuration註解的類裏,也能夠用在@Component註解的類裏。

Spring註解驅動系列持續更新中ide

相關文章
相關標籤/搜索