springboot入门

1.spring核心

Spring两大核心:基于工厂模式IOC(DI)和基于动态代理AOP。

IOC(DI):指控制器反转(依赖注入),原来要使用某个类对象实例是必须自己创建,使用spring后就不需要自己创建,由spring创建,需要时直接从spring中获取并且有依赖关系是会spring会通过反射自动注入。

AOP:就是不影响正常执行过程的前后加入额外的逻辑。比如权限,日志等,该执行的业务逻辑正常执行知识可以进行权限的判断核日志记录。

2.注解式编程

2.1. 创建项目并且导入pom
创建maven并导入一下内容:

org.springframework
spring-context
4.3.12.RELEASE

3.2.2. 配置类&Bean注解
@Configuration:加了这个注解的类就相当于传统的一个applicationContext-xxx.xml
@Bean:在标注了@Configuration的类里面的方式上面打上@bean就相当于在applicationContext-xxx.xml配置的一个
Dao的名字默认就是方法名,如果想改方法名使用@Bean(“beanName”)

测试类

public class JavaConfigTest {
    @Test
    public void test() throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        MyBean bean = applicationContext.getBean(MyBean.class);
        System.out.println(bean);
    }
}

配置类

@Configuration
public class ApplicationConfig {

    @Bean
    public MyBean myBean(){
        return new MyBean();
    }
}

2.3. @ComponentScan扫描bean
我们原来使用spring的使用不会在xml中一个一个配置bean,我们在再类上加上@Repository,@Service,@Controller @Component,并且注入时可以使用@AutoWired的注解注入。 这一切的功能都需要我们配置包扫描<context:component-scan base-package=“cn.itsource”/>.
然而现在注解驱动开发已经没有了配置文件,不能配置。但是提供了
@ComponentScan,我们可以在配置类上面加上这个注解也是一样,并且也能扫描配置包项目的相关注解,也能完成自动注入。

配置类

@Configuration
@ComponentScan
public class ApplicationConfig {

}

MybBean

@Component
public class MyBean {
}

2.4组件信息
1)@Scope单例测试
2)@Lazy懒加载
3)initMethod:初始化方法
4)destroyMethod:销毁方法

2.5. @Conditional-按照条件注册
1)放到类上面下面所有方法都生效,但是如果方法上加了优先级更高
2)可以加到方法上面,也可以加到类上面. 根据当前os.name的环境来判断

2.6. @Import
@Import()参数可以传入以下参数

  • {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}

2.7FactoryBean注册组件

public class MyfactoryBean implements FactoryBean<MyBean> {
    public MyBean getObject() throws Exception {
        return new MyBean();
    }

    public Class<?> getObjectType() {
        return MyBean.class;
    }

    public boolean isSingleton() {
        return true;
    }
}

写一个实现类实现FactoryBean,然后通过MyfactoryBean 对象拿到mybean对象

3.组件生命周期

3.1 接口InitializingBean和DisposableBean
InitializingBean:afterPropertiesSet,设置完属性后调用
DisposableBean:容器关闭时调用

3.2 注解@PostConstruct @PreDestroy
放在方法上,@PostConstruct 在构造器执行之后执行,
@PreDestroy 在对象销毁之前执行

3.3 BeanPostProcessor(接口),bean后置处理器
Object postProcessBeforeInitialization:初始化之前调用
Object postProcessAfterInitialization:初始化之后调用

4.springboot

5.2.1. 搭建项目
1)创建springboot-parent(pom)

<dependencyManagement>
        <dependencies>
            <!--springboot版本管理,springboot相关模块引入是就不需要制定版本了-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2)创建springboot_01_helllo maven模块

 <parent>
        <artifactId>springboot_parent</artifactId>
        <groupId>cn.itsource.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

5.2.2. 编码测试
1)新建启动类(App – Main方法)

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

2)新建一个Controller类

@RestController
@EnableAutoConfiguration
public class Example {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}

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

}

3)测试代码
运行:App
浏览器:http://localhost:8080/hello

Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了一种简化的方法来配置和部署应用程序,使开发人员能够更快地开发和运行应用程序。 Spring Boot Actuator是Spring Boot的一个组件,它可以帮助我们监控和管理Spring Boot应用程序,包括健康检查、审计、统计和HTTP追踪等功能。要使用Spring Boot Actuator,只需引入相应的起步依赖,并在应用程序的入口点类上添加@SpringBootApplication注解即可。在该类中,使用@SpringBootApplication注解相当于同时添加了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,它标识了当前应用程序是一个Spring Boot应用程序。要启动Spring Boot应用程序,只需在主启动类中编写main函数,通过调用SpringApplication.run(Application.class, args)方法来启动应用程序。在开发过程中,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot入门](https://blog.csdn.net/weixin_45905210/article/details/121712027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring boot 入门](https://blog.csdn.net/zhshx19900318/article/details/129476812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值