1. 项目的建立
1.1创建工程
file>new>new project>maven>命名>下一步>finish
1.路径
2.项目名称
3.下一步
注:version选择1.0,也就是保持不动
1.2 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.qfendu</groupId><artifactId>spring01quickstart</artifactId><version>1.0-SNAPSHOT</version><!--添加的依赖--><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version><!--版本号--></dependency></dependencies></project>
2.小项目(打印机)
1.0 版(普通方式)
package hello;
/**
* 打印服务
*/
public class MessageService {
public String getMessage(){
return "Hello World";
}
}
package hello;
/**
* 打印
*/
public class MessagePrinter {
/**
* 建立和messageservice的关联关系
*/
private MessageService service;
/**
* 设置service的值
* @param service
*/
public void setService(MessageService service) {
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
package hello;
public class Application {
public static void main(String[] args) {
System.out.println("applucation");
//创建打印机对象
MessagePrinter printer = new MessagePrinter();
//创建消息服务对象
MessageService service = new MessageService();
//设置打印机对象的service属性
printer.setService(service);
//打印消息
printer.printMessage();
}
}
2.0版(加入spring特性)
- 在messageprinter和messageservice类上添加@component,在applicationSpring类上@ComponentScan注解
- 初始化容器
- 获取对象
package hello;
import org.springframework.stereotype.Component;
/**
* 打印服务
*/
//
@Component
public class MessageService {
//鼠标右键或Alt+ins打开Generate,选择Override Methods...
public MessageService() {
super();
System.out.println("messageservice");
}
public String getMessage(){
return "Hello World";
}
}
package hello;
import org.springframework.stereotype.Component;
/**
* 打印
*/
@Component
public class MessagePrinter {
public MessagePrinter() {
super();
System.out.println("messageprinter");
}
/**
* 建立和messageservice的关联关系
*/
private MessageService service;
/**
* 设置service的值
* @param service
*/
public void setService(MessageService service) {
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class ApplicationSpring {
public static void main(String[] args) {
System.out.println("applicationSpring");
//初始化Spring容器
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationSpring.class);
//从容器中获取对象
//1.messagePrinter
MessagePrinter printer = context.getBean(MessagePrinter.class);
//2.messageservice
MessageService service = context.getBean(MessageService.class);
System.out.println(printer);
System.out.println(service);
//设置打印机对象的service属性
printer.setService(service);
//打印消息
printer.printMessage();
}
}
3.0版(spring创建对象和对象的关联关系)
- 在messageprinter和messageservice类上添加@component,在applicationSpring类上@ComponentScan注解
- 初始化容器
- 获取对象
- 在setService添加注释autowired
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 打印
*/
@Component
public class MessagePrinter {
public MessagePrinter() {
super();
System.out.println("messageprinter");
}
/**
* 建立和messageservice的关联关系
*/
private MessageService service;
/**
* 设置service的值
* @param service
*/
@Autowired
public void setService(MessageService service) {
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class ApplicationSpring {
public static void main(String[] args) {
System.out.println("applicationSpring");
//初始化Spring容器
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationSpring.class);
//从容器中获取对象
//1.messagePrinter
MessagePrinter printer = context.getBean(MessagePrinter.class);
//2.messageservice
//MessageService service = context.getBean(MessageService.class);
System.out.println(printer);
//System.out.println(service);
//设置打印机对象的service属性
//printer.setService(service);
//打印消息
printer.printMessage();
}
}
3.Spring介绍
1.核心概念
- IOC
- inversion of cntrol
- 控制反转
- DI
- dependency injection
- 依赖注入
- AOP
- aspect oriented programming
- 面向切面编程
小项目获取对象使用的就是IOC,对象和对象关系的创建使用的就是DI,aop后面会将
2.Spring框架图
core container:核心容器
core:核心,IOC和di都是由core实现
beans:bean工厂(创建对象的工厂)中bean的装配(对象的创建工作)
context:上下文,IOC容器通过它来实现
spEL:spring表达式语言
data access/integration:数据访问集成
orm:支持数据集成框架的封装,比如:jpa、jpo、hibernate
oxm:xml的支持
jms: 生产者和消费者的消息功能的实现
transactions:实现事务管理
web:web模块
web:文件上传功能,servlet监听器,初始化IOC容器
serlet:实现springmvc
portlet:网页内容聚合功能
websocket:socket开发
aspects:提供aspectJ的支持,也是aop的重要功能
instrumentation:设备,检测器
messaging:消息处理
test:测试
4.使用xml实现spring基本应用
- 在resources文件夹下新建一个xml文件,new>xml configuration file>spring config
- 添加bean元素,建立依赖关系
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
bean元素:描述当前的对象需要由spring容器管理
id属性:标识对象,未来在应用程序中可以根据id获取对象
class:被管理对象的类全名
-->
<bean id="service" class="hello.MessageService"></bean>
<bean id="printer" class="hello.MessagePrinter">
<!--
建立与service的依赖联系
name对应MessagePrinter中的private MessageService service;
ref对应的是id="service"
-->
<property name="service" ref="service"></property>
</bean>
</beans>
- java程序创建同上
package hello;
/**
* 打印服务
*/
//
public class MessageService {
//鼠标右键或Alt+ins打开Generate,选择Override Methods...
public MessageService() {
super();
System.out.println("messageservice");
}
public String getMessage(){
return "Hello World";
}
}
package hello;
/**
* 打印
*/
public class MessagePrinter {
public MessagePrinter() {
super();
System.out.println("messageprinter");
}
/**
* 建立和messageservice的关联关系
*/
private MessageService service;
/**
* 设置service的值
*
* @param service
*/
public void setService(MessageService service) {
this.service = service;
}
public void printMessage() {
System.out.println(this.service.getMessage());
}
}
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationSpring {
public static void main(String[] args) {
System.out.println("applicationSpring");
//初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象
//1.messagePrinter
MessagePrinter printer = context.getBean(MessagePrinter.class);
//打印消息
printer.printMessage();
}
}
5.添加log4j日志系统
- 在pom.xml中加入配置
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- 添加配置文件
在官网上找Spring Framework,打开某个spring框架的官方文档,ctrl+f打开查找,找lod4j.properties,在resource中新建lod4j.properties,将文档中的内容复制过去
log4j.rootCategory = INFO,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%d {ABSOLUTE}%5p%t%c {2}:%L - %m%n
log4j.category.org.springframework.beans.factory = DEBUG
