一、Spring5框架总结学习(从入门到进阶)-IOC容器

Spring5

———————————————————————————————————————————————————————————

1、如何创建一个Spring项目(idea版)

1、新建一个java工程,并在项目下创建一个lib文件夹,用来存放Spring框架要导入的jar包

在这里插入图片描述

2、创建一个User类

在这里插入图片描述

3、创建Spring配置文件,在配置文件中配置类

在这里插入图片描述

<?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">

	<!--    配置User对象创建-->
	<bean id="user" class="com.Spring5.User"></bean>
    
</beans>

4、进行测试代码编写

		//加载Spring配置文件
        ApplicationContext context =new ClassPathXmlApplicationContext("bean1.xml");

        //获取配置创建的对象
        //"user"为配置文件中User类,id的值
        User user=context.getBean("user", User.class);

        user.add();

2、 IOC容器

1、XML解析+工厂模式+反射

IOC思想基于IOC容器完成的,IOC底层就是对象工厂。

Spring提供IOC容器实现2种方式

  • BeanFactory:是Spring内部接口,不提供开发人员使用。加载配置文件时,不会创建对象,只有这个对象被使用时才会创建。
  • ApplicationContext:BeanFactory的子接口,提供更强大的功能,一般由开发人员使用。加载配置文件时,就会把配置文件内的对象全部创建。

2、bean管理

1、总述

2.1、 主要包括2种操作

  • Spring创建对象
  • Spring注入属性

2.2、2种实现方式

  • 基于配置文件方式
  • 基于注解方式
2、基于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.xsd">

	<!--    配置User对象创建-->
	<bean id="user" class="com.Spring5.User"></bean>
    
</beans>
  • id 代表这个类的唯一标识
  • class 类的全路径
  • 创建对象时,默认执行无参构造函数。
3、基于XML注入属性

第一种方式:使用set方法注入

	public class User {
    private String name;
    public void add()
    {
        System.out.println("add......");
    }

    public void setName(String name) {
        this.name = name;
    }
}
<bean id="user" class="com.Spring5.User">
    <!--name:属性名字
        value:想要设置的属性名字
    -->
    <property name="name" value="AA"></property>
</bean>

第二种方式:使用含参构造函数

	private String name;
    private String passwd;

    public User(String name, String passwd) {
        this.name = name;
        this.passwd = passwd;
    }
<bean id="user" class="com.Spring5.User">

		<!--name:属性名字
            value:想要设置的属性名字
        -->
    
    <constructor-arg name="name" value="AA"></constructor-arg>
    <constructor-arg name="passwd" value="1234"></constructor-arg>
    
</bean>
4、基于XML注入属性,属性值为空或特殊符号

空值、name=null

<property name="name">
    <null></null>
 </property>

特殊符号 name= <<南京>>

	<property name="name">
    <value><![CDATA[<<南京>>]]></value>
    </property>
5、基于XML注入属性,外部bean

原方法:
在这里插入图片描述
基于配置文件方法

public class UserService {
    private UserDao userDao;
    
    public void add()
    {
        userDao.update();
    }
     public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
 <bean id="userSevice" class="com.service.UserService">
 
 		<!--  ref属性:创建userDao标签的id值   -->
        <property name="userDao" ref="userDao1"></property>
    </bean>
    <bean id="userDao1" class="com.Dao.UserDaoDemo"></bean>

在这里插入图片描述

6、基于XML注入属性,内部bean和级联赋值

一对多关系

员工类

public class Emp {
    private String name;
    private String sex;
    private Dept dept;

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}

部门类

public class Dept {
    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }
}

配置文件

<!--  内部bean--->
 <bean id="emp" class="com.company.Emp">

        <property name="name" value="AA"></property>
        <property name="sex" value=""></property>
        
        <property name="dept">
            <bean id="dept1" class="com.company.Dept">
                <property name="dname" value="人力资源部"></property>
            </bean>
        </property>
    </bean>
7、基于XML注入属性,级联赋值
 <bean id="emp" class="com.company.Emp">

        <property name="name" value="AA"></property>
        <property name="sex" value=""></property>
        
        <property name="dept" ref="dept1"> </property>
 <bean id="dept1" class="com.company.Dept">
 
         <property name="dname" value="人力资源部"></property>
 </bean>
  </bean>
8、基于XML注入属性,集合属性(数组,list,map)
public class Student {
    private String[] course;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> set;

    public void setCourse(String[] course) {
        this.course = course;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }
}
<!--    集合属性注入-->
    <bean name="stu" class="com.jihe.Student">

<!--        数组-->
        <property name="course">
            <list>
                <value>数据库</value>
                <value>c++</value>
                <value>java</value>
                <value>pthyon</value>
            </list>
        </property>

<!--        list-->
        <property name="list">
            <list>
                <value>数据库</value>
                <value>c++</value>
                <value>java</value>
                <value>pthyon</value>
            </list>
        </property>

<!--        map-->
        <property name="map">
            <map >
                <entry key="1" value="A"></entry>
                <entry key="2" value="B"></entry>
                <entry key="3" value="C"></entry>
            </map>
        </property>
        
<!--       set-->
        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
        </property>

    </bean>
9、基于XML注入属性,集合属性(属性为对象,抽取共有部分)
<!--    集合属性注入,对象注入属性-->
    <bean name="stu" class="com.jihe.Student">

<!--        数组-->
        <property name="course">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>

    </bean>

 <bean id="course1" class="com.Course"> 
  <property name="cname" value="C++"></property>
</bean>
<bean id="course2" class="com.Course"> 
  <property name="cname" value="java"></property>
</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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!--使用util标签,提取-->
    <util:list id="booklist">
        <value>java</value>
        <value>C++</value>

<!--        对象属性-->
<!--        <ref bean=""></ref>-->
<!--        <ref bean=""></ref>-->
    </util:list>

    <bean id="" class="">
        <property name="list" ref="booklist"></property>
    </bean>

</beans>
10、bean的作用域/生命周期

1、创建的对象,默认情况下是单例对象
2、如何设置单实例还是多实例

<!---设置成单实例-->
<bean id="" class="" scope="singleton"></bean>

<!---设置成多实例-->
<bean id="" class="" scope="prototype"></bean>

3、两种区别

  1. 设置scope=“singleton”,加载spring文件时就会创建但实例对象
  2. 设置scope=“prototype”,不是加载spring配置文件的时候创建对象,在调用getBean方法时候创建多实例对象

4、生命周期

  1. 通过构造器创建bean实例
  2. 为bean的属性设置值和对其它bean引用
  3. 调用bean的初始化方法
  4. bean可以使用了
  5. 当容器关闭时,调用bean的销毁方法(需要进行配置销毁的方法 )
<!--- 需要手动配置销毁方法--->
<bean id="" class="" destroy-method="">
        <property name="list" ref="booklist"></property>
    </bean>
11、自动装配

1、定义:根据指定装配规则(属性名称或者属性类型),spring自动将匹配的属性值进行注入。

2、实现自动匹配
bean标签属性autowire 配置自动装配
autowire属性常用两个值
byName 根据属性名注入 属性名称和id=“属性名称”相一致
byType 根据属性类型注入 类型不能重复

 <bean id="emp" class="com.ccb.Emp" autowire="byName">
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.ccb.Dept">
12、引入外部属性文件

1、写配置文件.properties

prop.url=jdbc:mysql://localhost:3306/数据库名

2、添加命称空间 context

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

3、在spring配置文件中使用标签引入外部属性文件

<!--
  location 后面需要配置文件的路径
--->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    <bean id="" class="">
    <!--    value=配置文件等号左边的部分--->
        <property name="url" value="${prop.url}"></property>
    </bean>
</beans>
13、基于注解方式,实现对象的创建

1、引入一个依赖
spring-aop-4.1.6.RELEASE.jar
2、开启组件扫描
引入命称空间 context

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--    如果扫描多个包,
        1、多个包之间用逗号隔开,
        2、用包的上一级目录
         -->
    <context:component-scan base-package="ccb">

    </context:component-scan>

指定扫描的类

 可用注解类型
@Component(value = "userService")  //==<bean id="userService" class=".."/>
@Repository
@Service
@Controller
package ccb;

import org.springframework.stereotype.Component;
//在注解里面value值可以省略不写,默认为类名,但是是类名第一个字母小写
@Component(value = "userService")  //==<bean id="userService" class=".."/>
public class UserService {
    public  String add()
    {
        return "";
    }
}

进行测试代码编写

		//加载Spring配置文件
        ApplicationContext context =new ClassPathXmlApplicationContext("bean1.xml");

        //获取配置创建的对
        UserService user=context.getBean("userService", UserService.class);

        user.add();
14、基于注解方式,实现对象的创建,细节配置
<!--
        事例1
        use-default-filters="false" 表示使用自己配置的filters
        context:include-filter 设置扫描哪些内容,只扫描注解带
        org.springframework.stereotype.Controller的类
-->
    <context:component-scan base-package="ccb" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--
        事例2
        use-default-filters="false" 表示使用自己配置的filters
        context:exclude-filter 设置不扫描哪些内容,只扫描注解带
        Controller的类
-->
    <context:component-scan base-package="ccb" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
15、基于注解方式,属性注入

1、使用注解类型

    @Autowired  //根据属性类型自动装配
    @Qualifier  //根据属性名称进行注入
    @Resource   //可以根据属性名称,也可以根据属性类型
    @Value   //注入普通属性

2、@Autowired //根据属性类型自动装配

@Component 
public class UserService {
    @Autowired  //根据属性类型自动装配
    private UserDao user;
 //   @Qualifier  //根据属性名称进行注入
 //   @Resource   //可以根据属性名称,也可以根据属性类型
//    @Value   //注入普通属性
    public  String add()
    {
        return "";
    }
}
@Repository
public class UserDaoDemo implements UserDao {
    @Override
    public void update() {

    }
}

3、 @Qualifier //根据属性名称进行注入

要和@Autowired 一块使用

@Component 
public class UserService {

    //    value值为 UserDao进行注解时所用到的value值
    @Autowired
    @Qualifier(value = "类名")  //根据属性名称进行注入
    private UserDao user;
 //   @Resource   //可以根据属性名称,也可以根据属性类型
//    @Value   //注入普通属性
    public  String add()
    {
        return "";
    }
}

在这里插入图片描述
在这里插入图片描述

4、@Resource //可以根据属性名称,也可以根据属性类型

  @Resource(name = "名称")   //可以根据属性名称,也可以根据属性类型
    private UserDao user;

不带name=“”,就是按照类型装配

5、// @Value //注入普通属性


    @Value(value = "A")   //注入普通属性
    private String name;
16、完全注解方式实现

1、创建配置类,代替xml

@Configuration
@ComponentScan(basePackages = {"ccb"})
//==<context:component-scan base-package="ccb">

public class SpringConfig {
}

2、实现,获取配置类

 ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        //获取配置创建的对象
        //"user"为配置文件中User类,id的值
        User user=context.getBean("user", User.class);

        user.add();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值