Day20-spring
官网:https://spring.io/projects/spring-framework
- 
导入spring的包——-Spring Web MVC <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>6.0.11</version>
 </dependency>
- 
优点 spring是容器框架用于配置bean,并维护bean之间关系的框架 - 
bean是java中的任何一种对象,javabean/service/action/数据源/dao 
- 
ioc控制反转:inverse of control 
- 
aop面向切面 
- 
支持事务 
- 
di依赖注入:dependency injection 
 
- 
总结:Spring就是一个轻量级的控制反转(IOC)和面向切面(AOP)编程的框架
- 
组成 ![]() 
- 
扩展 - 
Spring Boot - 
一个快速开发的脚手架 
- 
基于SpringBoot可以快速的开发单个微服务 
- 
约定大于配置 
 
- 
- 
Spring Cloud - 
SpringCloud是基于SpringBoot实现的 
 
- 
 
- 
IOC

HelloSpring
- 
导包 
- 
创建pojo对象 package com.lsq.pojo;
 
 public class Hello {
 private String str;
 
 public String getStr() {
 return str;
 }
 
 public void setStr(String str) {
 this.str = str;
 }
 
 
- 
编写配置文件 
- 
测试 import com.lsq.pojo.Hello;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class MyTest {
 public static void main(String[] args) {
 //获取Spring的上下文对象
 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 //我们的对象现在都在Spring中管理了,我们要使用直接去里面取出来就可以
 Hello hello = (Hello)context.getBean("hello");
 System.out.println(hello.toString());
 }
 }
IOC创建对象的方式
- 
使用无参构造创建对象,默认! 
- 
假设我们要使用有参构造创建对象 - 
下标赋值 <bean id="hello" class="com.lsq.pojo.Hello">
 <property name="str" value="Spring"/>
 </bean>
- 
类型 <!--第二种:通过类型创建,不建议使用-->
 <bean id="user" class="com.lsq.pojo.User">
 <constructor-arg index="0" type="java.lang.String" value="七七"/>
 </bean>
- 
参数名 <!--第三种:通过参数名来设置-->
 <bean id="user" class="com.lsq.pojo.User">
 <constructor-arg name="name" value="罐罐"/>
 </bean>
 总结:在配置文件加载的时候,容器中管理的对象就已经初始化了 
- 
Spring配置
1. 别名
<!--如果配置了别名,也可以用别名获取到这个对象-->
<alias name="user" alias="user2"/>
2. Bean的配置
<!--
id:bean的唯一标识符,也就是相当于我们的对象名
class:bean对象所对应的全限定名:包名+类型
name:也是别名,而且name可以同时取多个别名
-->
3. import
这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这多个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的,使用的时候直接使用总的配置就可以了
依赖注入
构造器注入
Set方式注入【重点】
- 
依赖注入:Set注入 - 
依赖:bean对象的创建依赖于容器 
- 
注入:bean对象中的所有属性,由容器来注入 
 
- 
【环境搭建】
- 
复杂类型 package com.lsq.pojo;
 
 public class Address {
 private String address;
 
 public String getAddress() {
 return address;
 }
 
 public void setAddress(String address) {
 this.address = address;
 }
 }
- 
真实测试对象 public class Student {
 private String name;
 private Address address;
 private String[] books;
 private List<String> hobbys;
 private Map<String ,String> card;
 private Set<String> games;
 private String wife;
 private Properties info;
 }
- 
beans.xml 
- 
测试类 import com.lsq.pojo.Student;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class MyTest {
 public static void main(String[] args) {
 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 Student student = (Student)context.getBean("student");
 System.out.println(student.getAddress());
 }
 }
- 
完善注入信息 
 
                     
                    
                 
                    
                
