每天进步一点点~注解篇

收藏从未停止,学习从未开始,

与其长篇大论,不如简短精悍。

每天进步一点点,目标距离缩小点。

每天进步一点点,成功就会在眼前。

本人水平和能力有限,文章如有不对或有需要修改的地方,还望各位指明纠正

1. @SpringBootApplication

@SpringBootApplication
public class PxjrApplication {
    public static void main(String[] args) {
        SpringApplication.run(PxjrApplication.class, args);
    }
}

@SpringBootApplication包括几个注解:

@EnableAutoConfiguration、@Configuration和@ComponentScan。

@EnableAutoConfiguration:启用 SpringBoot 的自动配置机制,将所有符合自动配置条件的bean定义加载到IoC容器。

@Configuration:是JavaConfig形式的Spring Ioc容器的配置类。允许在 Spring 上下文中注册额外的 bean 或导入其他配置类。

提到@Configuration就要提到它的搭档@Bean,使用这两个注解可以创建一个简单的spring配置类,用于替代传统的xml配置文件;

@Configuration注解主要标注在某个类上,相当于xml配置文件中的<beans>

@Bean注解主要标注在某个方法上,相当于xml配置文件中的<bean>

<beans> 
    <bean id = "car" class="com.test.Car"> 
        <property name="wheel" ref = "wheel"></property> 
    </bean> 
    <bean id = "wheel" class="com.test.Wheel"></bean> 
</beans> 

=

@Configuration 
public class Factory{ 
    @Bean 
    public Car car() { 
        Car car = new Car(); 
        car.setWheel(wheel()); 
        return car; 
    } 
    @Bean  
    public Wheel wheel() { 
        return new Wheel(); 
    } 
}

@ComponentScan:

扫描被@Component (@Controller,@Service,@Repository,

@Component)注解的 bean,注解默认会扫描该类所在的包下所有的类。

注:

@ComponentScan默认会装配标识@Controller,@Service,@Repository,@Component注解的类到spring容器中。

如果你指定了路径,Spring将会将在被指定的包及其子的包中寻找bean。如果没有指定路径Spring默认的在@ComponentScan注解所在的包及其子包中寻找bean。这也是SpringBoot的main方法为什么必须要放在项目目录结构最外层的原因。

新开通的个人VX公众号:程序员Hotel,-----------------》传送门

猜你喜欢

转载自blog.csdn.net/lihua5419/article/details/105863421