spring注解驱动第一节之配置类初体验

[TOC]
从本章开始,总结一下之前看到的关于spring-annotation的相关知识点,从最基本的入门到后面与mvc的整合,全部使用注解版来实现,从此告别繁杂的配置文件,该系列总结会为后面理解spring-boot提供一定的帮助

AnnotationConfigApplicationContext

现在开始创建一个maven工程,使用main函数来读取配置类,创建Spring的容器,基于注解的ApplicationContext这个类就是AnnotationConfigApplicationContext,通过指定一个主配置类,来完成IOC容器的初始化,该项目不用是一个web工程

1. 项目准备

pom.xml导入如下jar

<?xml version="1.0" encoding="UTF-8"?>
<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.ddf.spring</groupId>
    <artifactId>spring-annotation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>
</project>

2. @Configuration配置类

@Configuration 该注解标注在某个class上,则可以被spring识别为一个配置类,配置类的作用等同于以前配置文件版的xml.当然具体需要在配置类中配置什么属性或起到什么作用,这就是另外注解的支持了,@Configuration只是保证你可以在该类中配置的功能能够被解析,如同一个空的xxx.xml配置文件,这是一切功能型注解生效的前提!

3. @Bean注解注入bean

@Bean该注解可以标注在配置类的某个方法上,它实现的意义是为IOC中容器注入bean,此时bean的名称为方法名称,bean的类型为方法返回类型.以下为向容器中注入一个typeUserbean,该bean的名称为user,默认都是单实例bean

@Bean
public User user() {
    return new User();
}

等同于

<bean id="user" class="xxx.xxx.User"/>

4. 创建AnnotationConfigApplicationContext

  • 创建一个Bean对象User.java
package com.ddf.spring.annotation.entity;

/**
 * @author DDf on 2018/7/19
 */
public class User {

    private Integer id;
    private String userName;
    private String password;
    private String tel;

    public User() {
        System.out.println("User创建完成...............");
    }

    public User(Integer id, String userName, String password, String tel) {
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.tel = tel;
    }
    // getter and setter

}
  • 新建一个配置类AnnotationConfiguration
package com.ddf.spring.annotation.configuration;

import com.ddf.spring.annotation.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author DDf on 2018/7/19
 */
@Configuration
public class AnnotationConfiguration {

    /**
     * 注入一个Type为User(方法返回值)的bean,bean的名称为user(方法名)
     * @return
     */
    @Bean
    public User user() {
        return new User();
    }
}
  • 新建主启动类Application
package com.ddf.spring.annotation;

import com.ddf.spring.annotation.configuration.AnnotationConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author DDf on 2018/7/19
 */
public class Application {
    public static void main(String[] args) {
        System.out.println("-----------------------IOC容器初始化-------------------------");
        // 创建一个基于配置类启动的IOC容器,如果主配置类扫描包的路径下包含其他配置类,则其他配置类可以被自动识别,如果主配置类扫描包的路径下包含其他配置类,则其他配置类可以被自动识别
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
        System.out.println("-----------------------IOC容器初始化完成-------------------------");
        // 获取当前IOC中所有bean的名称
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        // 打印当前IOC中对应名称的bean和bean的类型
        for (String name : definitionNames) {
            Object bean = applicationContext.getBean(name);
            System.out.println("bean name:" + name + ", type: " + bean.getClass());
        }
    }
}
  • 启动main,打印如下,可以看到除了最后一行我们注入的User以外,容器启动后会默认注入一些bean,但这些bean默认都是懒加载的,通过打印可以看出来,这是在容器已经初始化完成之后,获取预定义bean的名称然后调用getBean()方法之后获取才创建的,懒加载就是针对单实例的而且是在第一次过去才会被创建,而默认情况下都是容器启动后就会完成创建
-----------------------IOC容器初始化-------------------------
User创建完成...............
-----------------------IOC容器初始化完成-------------------------
bean name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor, type: class org.springframework.context.annotation.ConfigurationClassPostProcessor
bean name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor, type: class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
bean name:org.springframework.context.annotation.internalRequiredAnnotationProcessor, type: class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
bean name:org.springframework.context.annotation.internalCommonAnnotationProcessor, type: class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
bean name:org.springframework.context.event.internalEventListenerProcessor, type: class org.springframework.context.event.EventListenerMethodProcessor
bean name:org.springframework.context.event.internalEventListenerFactory, type: class org.springframework.context.event.DefaultEventListenerFactory
bean name:annotationConfiguration, type: class com.ddf.spring.annotation.configuration.AnnotationConfiguration$$EnhancerBySpringCGLIB$$1906ac13
bean name:user, type: class com.ddf.spring.annotation.entity.User
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值