`

spring注解配置 @Configuration 跟 @ComponentScan

 
阅读更多

spring 注解配置

 

 以前刚开始学spring的时候,我们最初用的配置是通过XML的方式读取bean,而如今随着SpringBoot跟SpringCloud的流行,越来越多的企业都开始使用Spring注解方式获取Bean.

1 @Configuration 类似Spring中的xml配置文件

 主要pom.xml

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

 2 Person类 

package com.melon.bean;

public class Person {

    private String name;
    private Integer 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 +
                '}';
    }
}

 2 spring的bean.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!--2 xml配置的包扫描 只要标注了@controller @service @Repository @component 任何一个组件-->
    <!--<context:component-scan base-package="com.melon"></context:component-scan>-->
    <!--1第一节xml bean 配置 跟 @bean-->
    <bean id="person" class="com.melon.bean.Person">
        <property name="age" value="10"></property>
        <property name="name" value="zhangsan"></property>
    </bean>

</beans>

  3 原始的测试方式

 

  

package com.melon.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person bean = (Person)applicationContext.getBean("person");
        System.out.println(bean);
    }
} 

  结果成功了。

 

 

   下面改成Spring注解方式读取bean,也就是不需要bean.xml,然后Perons类不变,多个Config类

 

@Configuration
public class MainConfig {
    //给容器注册个Bean,类型为返回的类型,id默认是用方法名作为id
    @Bean
    public Person person(){
        Person person = new Person();
        person.setAge(121);
        person.setName("zhangsan");
        return person;
    }
}

  测试注解方式

 

 

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

 二 利用注解方式@ComponantScan代替Xml方式的ComponentScan

 

原先xml方式

 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!--2 xml配置的包扫描 只要标注了@controller @service @Repository @component 任何一个组件-->
   <context:component-scan base-package="com.melon"></context:component-scan>

</beans>

测试方式差不多

 

现在改成@componantScan方式

package com.melon.config;

import com.melon.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

//配置类=配置文件
@Configuration //告诉spring这是一个配置类
@ComponentScan(value = "com.melon",
        //扫描时候需要排除的组件
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes ={Controller.class,Service.class})}
        //includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes ={Controller.class})}
        //,useDefaultFilters = false

        ) //包扫描的根目录
        //扫描时候只包含

public class MainConfig {
    //给容器注册个Bean,类型为返回的类型,id默认是用方法名作为id
    @Bean
    public Person person(){
        Person person = new Person();
        person.setAge(121);
        person.setName("zhangsan");
        return person;
    }
}

 测试

package com.melon.config;

import com.melon.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringAnnotationTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

        //Person person = applicationContext.getBean(Person.class);
        //System.out.println(person);
        //打印组件
        String[] names =applicationContext.getBeanDefinitionNames();
        for(String name : names){
            System.out.println(name);
        }
    }
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics