Spring学习-01 Spring利用注解注册Bean到容器中

本文档介绍了如何在Spring中使用注解将Bean注册到容器中,包括配置pom.xml,创建Entity和Service,定义配置类MainConfig,以及通过AppMain测试Bean的获取。在配置类中使用@ComponentScan注解指定扫描的包,通过ApplicationContext.getBean()方法获取Bean。
摘要由CSDN通过智能技术生成

Spring学习-01 Spring利用注解注册Bean到容器中

01.01 依赖配置(pom.xml)

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

01.02 一个Entity(Person.java)

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private Integer id;
    private String name;
    private Integer age;
}

01.03 一个Service(PersonService.java)

该类会生成一个bean,交给Spring容器管理

@Service("personService") //
public class PersonService {
    public void say() {
        Person person = new Person(1, "zhangsan", 30);
        System.out.println(person.toString());
    }
}

01.04 配置类(MainConfig.java)

该类声明为配置类,并用@ComponentScan指定要扫描的包,@Bean @Service,@Controller,@Component等注解的类就会生成一个bean给容器管理.

//配置类==配置文件

@Configuration	//告诉Spring这是一个配置类
@ComponentScan("com.min.spring")
public class MainConfig {
}
@ ComponentScan的用法
@ComponentScan(value="com.min.spring",
   includeFilters = { //代表字扫描的内容
       @Filter(type=FilterType.ANNOTATION,classes={Controller.class})
	}, 
   useDefaultFilters = false)  //在写includeFilters是,必需为false

//@ComponentScan  value:指定要扫描的包
//excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
//includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
//FilterType.ANNOTATION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照给定的类型;
//FilterType.ASPECTJ:使用ASPECTJ表达式
//FilterType.REGEX:使用正则指定
//FilterType.CUSTOM:使用自定义规则

@ ComponentScans的用法

就是组合ComponentScan

@ComponentScans(
		value = { @ComponentScan(xxx),
					@ComponentScan(xxx)}
	)	

01.05 主类(AppMain.java)来测试

public class AppMain {
    public static void main(String[] args) {
        //创建容器,利用注解方式配置,MainConfig中用@ComponentScan指定了扫描包,
        //PersonService类上用@Servie标注,代表会生成对象交给容器管理
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        //获取容器中对象
        PersonService personService = context.getBean(PersonService.class);
        //获取对象在容器中的名字
        //@Servie注解可以指定生成的bean的名字,默认为第一个字母小写的类名
        String[] names = context.getBeanNamesForType(PersonService.class);
        //打印对象在容器中的名字
        for (String name : names) {
            System.out.println("BeanName=" + name);
        }
        //调用对象的方法
        personService.say();
    }
}
执行的输出结果
BeanName=personService
Person(id=1, name=张三, age=30)
- ApplicationContext.getBean(classname)用法的说明
  • 可以利用接口来接受对象,

    • 如果容器中接口只有一个实现对象的话,classname直接可以用接口类的类名

    • 如果接口有多个实现对象的话, classname直接可以用``实现类`的,

      也可以使用ApplicationContext.getBean(beanname,接口classname)来获取

      ApplicationContext context = 
          	new AnnotationConfigApplicationContext(MainConfig.class);
      PersonService service1 
      		= context.getBean("personServiceImpl",PersonService.class);
      PersonService service2
      		= context.getBean("person2ServiceImpl",PersonService.class);
      service1.say();	//Person(id=1, name=张三, age=30)
      service2.say();	Person(id=2, name=李四, age=20)
      
  • 当然也可以用实现类来接受对象

01.06 利用JUnit来测试

public class ConfigTest {

    @Test
    public void testConfig1() {
        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.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
person2ServiceImpl
personServiceImpl
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值