Spring基础使用教程(自我存档使用)
欢迎学习Spring
开发刚刚起步,学习基础Spring以打基础,供之后学习工作使用。
环境配置
- 使用IDEA ,用过才知道舒服~~;
- 使用Meaven创建Spring工程:New一>project一>meaven创建工程;
- 环境配置 ,使用“前辈”那里搞来的settings.xml(公司使用的),将其中仓库地址修改为自己本地地址即可。IDEA有一个巨好用的功能,在修改完配置文件后点击右上角的“刷新”小图标,完成pom文件修改后的整合应用,简单方便!;
Spring简明解释
IOC:学名:控制反转,将创建对象以及类、对象之间的关联关系交给Spring管理
通俗一点:不用每次使用new来创建对象,或是将一个类作为另外一个类的成员变量来实现类与类之间的关联。
AOP:面向切面编程(暂时还没有接触到)
其他:集成jdbc等模块
注解及使用
@Component:添加这个注解可以把创建这个类交给Spring来做。
@ComponentScan:与@Component配合使用,扫描加了@Component的类,完成类对象的创建。
@Autowired:对类成员变量、构造函数、方法进行标注,完成自动装配。
@Configuration:标注该类为配置类。
使用@Autowried的三种方式
1.直接用成员变量
@Autowired
private CompactDisc cd;
@AutoWired
private Power power;
2.构造函数注入
@Autowired
public CDPlay(CompactDisc cd,Power power){
this.cd = cd;
this.power = power;
}
3.setter方法注入
@Autowired
public void setPower(Power power){
this.power = power;
}
4.任意函数
@Autowired
public void prepare(CompactDisc cd, Power power){
this.cd = cd;
this.power = power;
}
实例
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>org.example</groupId>
<artifactId>Spring_one</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
</project>
MessageService:
消息服务类:
package hello;
import org.springframework.stereotype.Component;
@Component
public class MessageService {
public MessageService() {
super();
System.out.println("MessageService...");
}
public String getMessage(){
return "Hellow World!";
}
}
MessagePrinter:
消息打印机类:
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
public MessagePrinter() {
super();
System.out.println("MessagePriter...");
}
private MessageService service;
@Autowired
public void setService(MessageService service) {
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
Application_Spring:
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class Application_Spring {
public static void main(String args[]){
System.out.println("applicationSpring");
//初始化Spring容器
ApplicationContext context = new AnnotationConfigApplicationContext(Application_Spring.class);
//从容器中获取对象
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
进一步解耦
使用AppConfig:
代码:
创建AppConfig类:
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class AppConfig {
}
App类:
package soundsystem;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
public class App {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
CDPlayer player = context.getBean(CDPlayer.class);
player.paly();
}
}
通过在配置类中添加注解,完成扫描和对象、关联关系的创建,进一步解耦
——————————————————————————————————————————
引入日志
之后用到会补充
引入单元测试
之后用到会补充