

Spring Boot与Spring Cloud

狂神认为所谓控制反转就是:获得依赖对象的方式反转了。

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为
一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。
注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.18version>
dependency>
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+ name );
}
}
<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 id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
bean>
beans>
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new
ClassPathXmlApplicationContext("beans.xml");
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
package com.yang.pojo;
/**
* @author 缘友一世
* @date 2022/7/25-21:54
*/
public class User {
private String name;
public User() {
System.out.println("User的无参构造!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public void show() {
System.out.println("name="+name);
}
}
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.yang.pojo.User">
<constructor-arg name="name" value="greatBoy"/>
bean>
beans>
@Test
public void test(){
ApplicationContext context = new
ClassPathXmlApplicationContext("beans.xml");
//在执行getBean的时候, user已经创建好了 , 通过无参构造
User user = (User) context.getBean("user");
//调用对象的方法 .
user.show();
}
<bean id="user" class="com.yang.pojo.User">
<constructor-arg index="0" value="java"/>
bean>
<bean id="user" class="com.yang.pojo.User">
<constructor-arg type="java.lang.String" value="java"/>
bean>
<bean id="user" class="com.yang.pojo.User">
<constructor-arg name="name" value="greatBoy"/>
bean>
package com.yang.pojo;
/**
* @author 缘友一世
* @date 2022/7/25-21:54
*/
public class User {
private String name;
public User(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public void show() {
System.out.println("name="+name);
}
}
<bean id="user" class="com.yang.pojo.User">
<constructor-arg name="name" value="greatBoy"/>
bean>
<bean id="userT" class="com.yang.pojo.UserT">
bean>
test.java
@Test
public void testT(){
ApplicationContext context = new
ClassPathXmlApplicationContext("beans.xml");
UserT user = (UserT) context.getBean("userT");
user.show();
}


<import resource="{path}/beans.xml"/>
package com.yang.pojo;
/**
* @author 缘友一世
* @date 2022/7/26-14:17
*/
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
package com.yang.pojo;
import java.util.*;
/**
* @author 缘友一世
* @date 2022/7/26-14:17
*/
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.yang.pojo.Address">
<property name="address" value="开封"/>
bean>
<bean id="student" class="com.yang.pojo.Student">
<property name="name" value="酸梅汤小王子"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>唐三value>
<value>萧炎value>
<value>罗峰value>
<value>狂神value>
array>
property>
<property name="hobbies">
<list>
<value>开挂value>
<value>修炼value>
<value>打怪兽value>
<value>讲课value>
list>
property>
<property name="card">
<map>
<entry key="身份证" value="666"/>
<entry key="银行卡" value="888"/>
map>
property>
<property name="games">
<set>
<value>LOLvalue>
<value>COCvalue>
<value>BOBvalue>
set>
property>
<property name="wife">
<null/>
property>
<property name="info">
<props>
<prop key="driver">666888prop>
<prop key="url">xxxprop>
<prop key="username">rootprop>
<prop key="password">1234567890prop>
props>
property>
bean>
beans>
import com.yang.pojo.Student;
import com.yang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 缘友一世
* @date 2022/7/26-14:28
*/
public class MyTest04 {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("beans04.xml");
Student student = (Student) Context.getBean("student");
System.out.println(student.toString());
/*
* Student{name='酸梅汤小王子',
* address=Address{address='开封'},
* books=[唐三, 萧炎, 罗峰, 狂神],
* hobbies=[开挂, 修炼, 打怪兽, 讲课],
* card={身份证=666, 银行卡=888},
* games=[LOL, COC, BOB], wife='null',
* info={password=1234567890, url=xxx, driver=666888, username=root}
* }
* */
}
}
package com.yang.pojo;
/**
* @author 缘友一世
* @date 2022/7/26-15:01
*/
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
benans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.yang.pojo.User" p:name="白小纯" p:age="18"/>
<bean id="user2" class="com.yang.pojo.User" c:name="黑小纯" c:age="18" scope="prototype"/>
beans>
@Test
public void test02(){
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,
bean就是由IoC容器初始化、装配及管理的对象 .

| 类型 | 说明 |
|---|---|
| singleton | 在Springloc容器中仅存在一个Bean实例,Bean以单例方式存在,默认值 |
| prototype | 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean0时,相当于执行newXxxBean() |
| request | 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境 |
| session | 同一个HTTPSession共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationCont |
几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。

<bean id="user" class="com.yang.pojo.User" p:name="白小纯" p:age="18" scope="singleton"/>
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("UserBeans.xml");
User user01 = context.getBean("user", User.class);
User user02 = context.getBean("user", User.class);
System.out.println(user01==user02);//true
}

<bean id="user2" class="com.yang.pojo.User" c:name="黑小纯" c:age="18" scope="prototype"/>
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("UserBeans.xml");
User user1 = context.getBean("user2", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user1==user2);//false
}