Spring入门 #CSDN博文精选# #IT# #主流框架# #spring框架#

大家好,小C将继续与你们见面,带来精选的CSDN博文~

又到周一啦,上周的系统化学习专栏已经结束,我们总共一起学习了20篇文章,这周将开启全新专栏《放假不停学,全栈工程师养成记》

在这里,你将收获:

  • 将系统化学习理论运用于实践,系统学习IT技术
  • 学习内容涵盖数据库、软件测试、主流框架、领域驱动设计和第三方生态等,离全栈工程师更近一步
  • 精心整理的CSDN技术大咖博文,假期学习实现弯道超车

新年快乐!春节过后的学习脚步不能停下,小C来介绍主流框架!

本文来自CSDN博主@湮顾千古

 

1.什么是Spring

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。

   Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
  然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
   目的:解决企业应用开发的复杂性
   功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
   范围:任何Java应用

   它是一个容器框架,用来装javabean(java对象),中间层框架(万能胶)可以起一个连接作用,比如说把Struts和hibernate粘合在一起运用。简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

2. 什么是控制反转(或依赖注入) 

   控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
   IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)”  ,即由容器动态的将某种依赖关系注入到组件之中 ,案例:实现Spring的IoC

第一步:需要添加springIDE插件,配置相关依赖(插件如何安装点击打开链接

pom.xml  (1.spring-context   2.spring-orm  3.spring-web  4.spring-aspects)

 

 

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>zking</groupId>
  <artifactId>s1</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>s1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    	<dependency>
      		<groupId>junit</groupId>
      		<artifactId>junit</artifactId>
      		<version>3.8.1</version>
      		<scope>test</scope>
    	</dependency>
   
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-context</artifactId>
    		<version>5.0.1.RELEASE</version>
	</dependency>
   
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-web</artifactId>
    		<version>5.0.1.RELEASE</version>
	</dependency>
	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-orm</artifactId>
    		<version>5.0.1.RELEASE</version>
	</dependency>
 
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-aspects</artifactId>
    		<version>5.0.1.RELEASE</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>s1</finalName>
  </build>
</project>

 

第二步:插件Spring的xml文件(右键-->new-->other-->spring-->Spring Bean Configuration File)

注:创建spring的XML文件时,需要添加beans/aop/tx/context标签支持(勾上即可)

ApplicationContext.xml

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	
</beans>

 

第三步:创建一个helloworld类

 
  1.  

    package p1;
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    public class HelloWorld {
    	private String name;
     
    	public HelloWorld() {
    		super();
    		System.out.println("new HelloWorld()");
    	}
    	
    	public HelloWorld(String name) {
    		super();
    		this.name = name;
    	}
     
    	public void init() {
    		System.out.println("init.......");
    	}
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}	
    }

     

3. 如何在spring当中定义和配置一个JavaBean

使用无参构造方法+set方法创建一个JavaBean

   1 id:在容器中查找Bean(对象)的id(唯一、且不能以/开头)
   2 class:bean(对象)的完整类名
   3 name:在容器中查找Bean(对象)的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
   4 scope:(singleton|prototype)默认是singleton
     4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
     4.2 prototype(原型模式/多例模式):一个bean(对象)定义对应多个对象实例
   4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
   5 parent:指定一个父bean(必须要有继承关系才行)
   6 init-method:指定bean对象()的初始化方法

   7 使用有参数构造方法创建javaBean(java对象):constructor-arg

第四步:在xml中创建bean(看不懂属性的,在第三点中有介绍

  1.  

<bean id="helloworld" class="p1.HelloWorld" scope="prototype" name="a b c" init-method="init">
		<property name="name">
			<value>zs</value>
		</property>
</bean>
	
	<bean id="helloworld2" class="p1.HelloWorld">
		<constructor-arg index="0">
			<value>zzz</value>
		</constructor-arg>
	</bean>

 

第五步:写一个测试的类即可

 
public static void main(String[] args) {
		//以前的写法
		HelloWorld helloWorld=new HelloWorld();
		helloWorld.setName("张三");
		System.out.println("hello"+helloWorld.getName());
		//-------------------------------------------------------------
		//Spring
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
		HelloWorld a = (HelloWorld)applicationContext.getBean("a");
		System.out.println("你好: "+a.getName());
		
		HelloWorld b = (HelloWorld)applicationContext.getBean("b");
		System.out.println("你好: "+b.getName());
		
		HelloWorld c = (HelloWorld)applicationContext.getBean("c");
		System.out.println("你好: "+c.getName());
		
		HelloWorld d = (HelloWorld)applicationContext.getBean("helloworld2");
		System.out.println("--------------------------------");
		System.out.println("你好: "+d.getName());
	}
  1.  

4. 简单属性的配置:

   8+1+3:8大基本数据类型+String+3个sql
                       java.util.Date      java.sql.Date    java.sql.Time    java.sql.Timestamp
   通过<value>标签赋值即可

5. 复杂属性的配置

  5.1 JavaBean    ref bean=""
  5.2 List或数组
  5.3 Map
  5.4 Properties

创建一个学生类(Student),定义这几个属性

 

 

private HelloWold helloworld;
 
private String []arr;
private List list;
private Map map;
private Properties properties;

 

在xml配置进行配置

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="helloworlds" class="p1.HelloWold">
<property name="name">
<value>张三</value>
</property>
 
</bean>
<bean id="ss" class="p1.Student">
<property name="helloworld">
<ref bean="helloworlds"><!-- ref引用另一个对象 -->
</property>
 
<property name="arr">
<list>
<value>aa</value>
<value>bb</value>
<value>cc</value>
<value>dd</value>
</list>
</property>
<property name="list">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</property>
<property name="map">
<map>
<entry>
<key>
<value>zs</value>
</key>
<value>张三</value>
</entry>
<entry>
<key>
<value>ls</value>
</key>
<value>李四</value>
</entry>
<entry>
<key>
<value>ww</value>
</key>
<value>王五</value>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="a2">222</prop>
</props>
</property>
 
</bean>

 

6. 针对项目,配置文件路径的2种写法

ApplicationContext 

String path = "applicationContext.xml";(独自开发)

String path = "classpath:applicationContext-*.xml";//src(分模块开发  多人开发)

 

关注高校俱乐部,更多精彩内容等着你!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值