Spring Boot 实战 第一章Spring基础

spring介绍

Spring 1.X
xml配置

Spring 2.x
注解配置 (@Component @Service)

基本配置(如:数据库配置)用XML,
业务配置用用注解

Spring 3.x Spring 4.x 最新是 5.1.5 20190323
Java配置

Spring框架
轻量级的企业级开发的一站式解决方案
Ioc容器,Aop,数据访问,Web开发,消息,测试

POJO Plain Old Java Object 无任何限制的普通Java对象
被Spring管理的对象称之为 Bean

maven介绍

M2_HOME
E:\apache-maven-3.2.2

;%M2_HOME%\bin

groupId 组织的以为标识 com.zhangsan artifactId 项目的唯一标识 test-demo 中划线 version 版本 4.1.5.RELEASE

变量定义

${spring-framework.version}

编译插件

		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>

jar的依赖不会写,到http://mvnrepository.com检索

自己的jar导入到自己的仓库
mvn install:install-file -DgroupId=com.oracle “-DartifactId=ojdbc14” “-Dversion=10.2.0.2.0” “-Dpackaging=jar”
“-Dfile=D:\ojdbc14.jar”

常用依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		boot开始引用了webmvc tomact等,webmvc中引入了context spring-core等
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

spring tool suite STS 开发工具
Eclipse 安装M2E插件
NetBeans

spring的基础配置

依赖注入 注解配置

spring的四大原则
pojo轻量级最小侵入开发
依赖注入 基于接口编程,松耦合
Aop和声明式编程
模板

约定优于配置(convention over configuration),也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性

控制反转 Inversion Of Control IOC
依赖注入 dependency Injection DI
在spring环境下是等同的概念
控制反转是通过依赖注入实现的
依赖注入 是容器负责创建对象和维护对象间的依赖关系
解耦,组合
一个类具备某个功能,组合另个类有此功能的类

Spring 使用 xml 注解 java配置 groovy配置,实现bean的创建和注入

生命bean
@Component没有明确角色
@Service @Reponsitory @Controller

注入bean 可设置在set方法和属性上
@Autowired
@Inject
@Resource

Spring容器类 AnnotationConfigApplicationContext

@Service
public class FunctionService {
    public String sayHello(String word){
        return "hello"+word+"!";
    }
}

@Service
public class UseFunctionService {
    @Autowired
    FunctionService functionService;
    public String SayHello(String word){
        return functionService.sayHello(word);
    }
}

@Configuration //声明式配置类
@ComponentScan("com.example.demo1") //自动扫描这个包下的 @Service @Componet 并注册为Bean
public class DiConfig {
}

	public static void main(String[] args) {
		//使用 注解 配置 应用 上下文 作为 Spring容器
		AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(DiConfig.class);
		//获得声明式配置 的这个类
		UseFunctionService us=context.getBean(UseFunctionService.class);
		System.out.println(us.SayHello("张三"));
		context.close();
	}

JAVA配置

Java配置 通过@Configuration 和 @Bean实现
声明当前类是一个配置类,相当于一个Spring配置的xml文件
@Bean注解在方法上,声明当前方法返回值为一个Bean

全局配置使用Java配置,(如数据库相关,MVC相关)
业务Bean配置,使用注解配置(@Service)

public class FunctionService {
    public String sayHello(String word){
        return "hello"+word+"!";
    }
}

public class UseFunctionService {
    FunctionService functionService;
    public void setFunctionService(FunctionService functionService){
        this.functionService=functionService;
    }
    public String SayHello(String word){
        return functionService.sayHello(word);
    }
}
@Configuration
public class JavaConfig {
    @Bean
    public FunctionService functionService(){
        return new FunctionService();
    }
    @Bean //当前方法返回一个bean,bean是方法名
    public UseFunctionService useFunctionService(){
        UseFunctionService us=new UseFunctionService();
        us.setFunctionService(functionService());
        return us;
    }
    //@Bean //直接将FunctionService作为参数,只要存在某个Bean,就可当做参数
    /*public UseFunctionService useFunctionService(FunctionService fs) {
        UseFunctionService us = new UseFunctionService();
        us.setFunctionService(fs);
        return us;
    }*/
}

	AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(JavaConfig.class);
	UseFunctionService us=context.getBean(UseFunctionService.class);
	System.out.println(us.SayHello("java Config"));
	context.close();
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值