目录:一、@Configuration与@Bean给容器注册组件 二、@ConponentScan自动扫描注解java
//实体类spring
package com.lee.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data//生成GET SET方法 @NoArgsConstructor//无参构造函数 @AllArgsConstructor//有参构造函数 public class Person { private Integer id; private String username; private String gender; }
配置文件函数
<?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 id="person" class="com.lee.bean.Person"> <property name="id" value="1"></property> <property name="username" value="lee"></property> <property name="gender" value="male"></property> </bean> </beans>
测试测试
package com.lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Object person = context.getBean("person"); System.out.println(person); } }
结果:ui
Person(id=1, username=lee, gender=male)
配置类.net
package com.lee.config; import com.lee.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration//告诉Spring这是一个配置类 public class MainConfig { //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名做为id //@Bean("名字")能够给bean修更名称 @Bean public Person person(){ return new Person(2,"lee2","male2"); } }
测试类code
package com.lee; import com.lee.bean.Person; import com.lee.config.MainConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainTest { public static void main(String[] args) { // ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); // Object person = context.getBean("person"); // System.out.println(person); ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); Person bean = context.getBean(Person.class); System.out.println(bean); } }
测试结果:component
Person(id=2, username=lee2, gender=male2)
总结xml
@Configuration等同于之前的配置文件---告诉spring这是一个配置类开发
@Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名做为id
<?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.xsd"> <context:component-scan base-package="com.lee"></context:component-scan> </beans>
准备类
package com.lee.controller; import org.springframework.stereotype.Controller; @Controller public class BookController { }
package com.lee.service; import org.springframework.stereotype.Service; @Service public class BookService { }
package com.lee.dao; import org.springframework.stereotype.Repository; @Repository public class BookDao { }
配置文件自动扫描
package com.lee.config; import com.lee.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @ComponentScan("com.lee")//自动扫描 value指定要扫描的包 @Configuration//告诉Spring这是一个配置类 public class MainConfig { //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名做为id //@Bean("名字")能够给bean修更名称 @Bean public Person person(){ return new Person(2,"lee2","male2"); } }
测试类
package com.lee; import com.lee.bean.Person; import com.lee.config.MainConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); String[] names = context.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 bookController bookDao bookService person
源码,component中有includeFilters和excludeFilters功能,可以过滤要筛选的包
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.core.annotation.AliasFor; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan { @AliasFor("basePackages") String[] value() default {}; boolean useDefaultFilters() default true; ComponentScan.Filter[] includeFilters() default {}; ComponentScan.Filter[] excludeFilters() default {}; @Retention(RetentionPolicy.RUNTIME) @Target({}) public @interface Filter { FilterType type() default FilterType.ANNOTATION; @AliasFor("classes") Class<?>[] value() default {}; @AliasFor("value") Class<?>[] classes() default {}; String[] pattern() default {}; } }
配置类1
package com.lee.config; import com.lee.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; //@ComponentScan("com.lee")//自动扫描 value指定要扫描的包 @Configuration//告诉Spring这是一个配置类 @ComponentScan(value="com.lee",excludeFilters = { @ComponentScan.Filter(type=FilterType.ANNOTATION,value = Controller.class) }) public class MainConfig { //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名做为id //@Bean("名字")能够给bean修更名称 @Bean public Person person(){ return new Person(2,"lee2","male2"); } }
测试1
mainConfig bookDao bookService person
配置类2
package com.lee.config; import com.lee.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; //@ComponentScan("com.lee")//自动扫描 value指定要扫描的包 @Configuration//告诉Spring这是一个配置类 //@ComponentScan(value="com.lee",excludeFilters = { // @ComponentScan.Filter(type=FilterType.ANNOTATION,value = Controller.class) //}) //includeFilters必须和useDefaultFilters=false一块儿使用才能起到做用 //useDefaultFilters默认是true,即默认扫描所有的包 @ComponentScan(value = "com.lee",useDefaultFilters=false,includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class) }) public class MainConfig { //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名做为id //@Bean("名字")能够给bean修更名称 @Bean public Person person(){ return new Person(2,"lee2","male2"); } }
测试2
mainConfig bookController person