Spring是一个轻量级的控制反转和面向切面编程的框架。
SpringBoot是一个快速开发的脚手架,
学这个之前需要学习Spring和SpringMVC
基于SpringBoot开一快速地开发单个微服务。
约定大于配置
SpringCloud是基于SpringBoot实现的。
基本的使用:
创建一个实体类
package com.dongmu.pojo;
public class Hello {
String string;
@Override
public String toString() {
return "Hello{" +
"string='" + string + '\'' +
'}';
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
配置bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean class="com.dongmu.pojo.Hello" id="hello">
<!--普通类型的set注入-->
<property name="string" value="ksjdlaj"/>
</bean>
</beans>
使用的时候直接获取
这就是对象由容器创建,不需要修改代码。IOC。
IOC创建对象的方式
对象创建的时机:
在容器创建的时候会调用无参构造把对象创建好并放入容器中。如果没有无参构造就会报错。
如何使用有参构造?
<bean class="com.dongmu.pojo.Hello" id="hello">
<!-- <property name="string" value="ksjdlaj"/>-->
<!-- <constructor-arg index="0" value="sdada"/>-->
<constructor-arg name="string" value="ccc"/>
</bean>
<bean class="com.dongmu.pojo.Hello" id="hello">
<!-- <property name="string" value="ksjdlaj"/>-->
<!-- <constructor-arg index="0" value="sdada"/>-->
<!-- <constructor-arg name="string" value="ccc"/>-->
<constructor-arg type="java.lang.String" value="kksdmk"/>
<constructor-arg type="java.lang.String" value="00"/>
</bean>
使用的时候get到的是同一个对象吗?
是同一个对象,内存中只有一个实例。
别名<alias name="hello" alias="sd"/>,取的时候可以用别名直接取。
但是其实这个没啥用,在bean标签的内部可以直接使用name取别名,而且可以取多个
<bean class="com.dongmu.pojo.Hello" id="hello" name="hello2,hello3">
<!-- <property name="string" value="ksjdlaj"/>-->
<!-- <constructor-arg index="0" value="sdada"/>-->
<!-- <constructor-arg name="string" value="ccc"/>-->
<constructor-arg type="java.lang.String" value="kksdmk"/>
<constructor-arg type="java.lang.String" value="00"/>
</bean>


import可以把其他bean.xml文件导入到一个主的bean中。
bean注入的几种方式:
基本类型的注入:set注入(要求由set方法)
引用类型的注入:ref引用其他的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-3.2.xsd">
<bean class="com.dongmu.pojo.Hello" id="hello" name="hello2,hello3">
<!-- <property name="string" value="ksjdlaj"/>-->
<!-- <constructor-arg index="0" value="sdada"/>-->
<!-- <constructor-arg name="string" value="ccc"/>-->
<constructor-arg type="java.lang.String" value="kksdmk"/>
<constructor-arg type="java.lang.String" value="00"/>
</bean>
<bean id="com" class="com.dongmu.pojo.Complex">
<property name="a" value="1"/>
<property name="b" value="kkk"/>
<!-- <property name="c" value="{a,b,c,d}"/>-->
<property name="c">
<array>
<value>a</value>
<value>"b"</value>
<value>"c"</value>
<value>"d"</value>
</array>
</property>
<property name="d">
<map>
<entry key="1" value="one"/>
<entry key="2" value="two"/>
</map>
</property>
<property name="e" >
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</property>
<property name="f" ref="hello3"/>
</bean>
<alias name="hello" alias="sd"/>
</beans>
java类如下:
package com.dongmu.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Complex {
int a;
String b;
String[] c;
Map d;
List e;
Hello f;
@Override
public String toString() {
return "Complex{" +
"a=" + a +
", b='" + b + '\'' +
", c=" + Arrays.toString(c) +
", d=" + d +
", e=" + e +
", f=" + f +
'}';
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String[] getC() {
return c;
}
public void setC(String[] c) {
this.c = c;
}
public Map getD() {
return d;
}
public void setD(Map d) {
this.d = d;
}
public List getE() {
return e;
}
public void setE(List e) {
this.e = e;
}
public Hello getF() {
return f;
}
public void setF(Hello f) {
this.f = f;
}
public Complex() {
}
public Complex(int a, String b, String[] c, Map d, List e, Hello f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
}
p标签注入和c标签注入,直接在bean标签内部使用,p标签是set注入,c标签是构造器注入,了解即可。
版权声明:本文为qq_45401910原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。