Springboot maven及spring 介绍
关于maven 的介绍: apache maven 是一个软件项目管理,基于对象模型(POM)的概念,用来管理项目的依赖,编译,文档等信息
spring项目的搭建
- 新建Maven 项目 ,单击file->new ->Project ->maven
- 输入maven项目坐标
- 选择存库路径
- 修改pom.xml 的配置文件
下面给一个配置实例
<?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.wuhao.cn</groupId>
<artifactId>hight_spring4</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version> 1.7 </java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-compiler-plugin </artifactId>
<version> 2.3.2 </version>
<configuration>
<source> $(java.version) </source>
<target> $(java.version) </target>
</configuration>
</plugin>
</plugins>
</build>
</project>
IoC: 实际上是指 Spring 框架提供的 IoC 容器实现(IoC Container ) ,使用IOC 容器的一个典型代码片段:
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplication-Context("...");
// ...
MockService service = context.getBean(MockService.class);
service.doSomething();
}
}
IoC有两种方式,一种是DI , 一种是DL;前者是当前软件实体被动接受其依赖的其他组件被 IoC 容器注入,而后者则是当前软件实体主动去某个服务注册地查找其依赖的那些服务
Spring IoC 容器的依赖注入工作可以分为两个阶段:
1 收集和注册
第一个阶段可以认为是构建和收集 bean 定义的阶段,在这个阶段中,我们可以通过 XML 或者 Java 代码的方式定义一些 bean,然后通过手动组装或者让容器基于某些机制自动扫描的形式,将这些 bean 定义收集到 IoC 容器中。
声明 Bean的注解:
- @Compoent 组件
- @Service 在业务层使用(service)
- @Controller 在展现层(MVC->SpringMVC)中使用
注入Bean 的注解 - @Autowired: Spring提供的注解
- @Inject: JSR-330提供的注解
- @Resource: JSR-250提供的注解 他们可以注解在set方法上或者属性上 ,优点:代码少,层次清晰
示例:
编写功能类的示例:
package com.wuho.cn;
import org.springframework.stereotype.Service;
@Service //声明ben
public class FunctionService {
public String sayHello(String word) {
return "Hello" + "world" + "!";
}
}
使用功能类的示例
package com.wuho.cn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UseFunctionService {
@Autowired //使用bean
FunctionService functionService;
public String sayHello(String world) {
return functionService.sayHello(world);
}
}
2 分析和组装
当第一阶段工作完成后,我们可以先暂且认为 IoC 容器中充斥着一个个独立的 bean,它们之间没有任何关系。 IoC 容器在第二阶段要干的事情就是分析这些已经在 IoC 容器之中的 bean,然后根据它们之间的依赖关系先后组装它们 Spring 的 XML 配置文件是一种配置(Configuration),但本质上,这些配置文件更应该是一种代码形式,XML 在这里其实可以看作一种 DSL(domain specific language:即领域专用语言),它用来表述的是 bean 与 bean 之间的依赖绑定关系,如果没有 IoC 容器就要自己写代码新建(new)对象并配置(set)依赖。
配置类
package com.wuho.cn;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //配置
@ComponentScan("com.wuho.cn") //自动扫描包名下的所有@Service @Compoent @Repository @Controller 的类,并注册为Bean
public class DIConfig {
}
运行
package com.wuho.cn;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
//声明 使用 AnnotationConfigApplicationContext 作为Spring的容器,接受输入一个配置类作为参数
AnnotationConfigApplicationContext content = new AnnotationConfigApplicationContext(DIConfig.class);
//获取声明的UseFunctionService 的bean
UseFunctionService useFunctionService = content.getBean(UseFunctionService.class);
System.out.println(useFunctionService.sayHello("di"));
content.close();
}
}