ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring介绍

2021-11-11 20:30:11  阅读:119  来源: 互联网

标签:容器 对象 spring 介绍 UserInfo IOC public


IOC(控制反转)

IOC介绍

IOC是什么

IOC(Inversion of Control 即控制反转)将对象交给容器管理
考虑:谁控制谁、控制什么?为了是反转?那些方面反转?
谁控制了谁?是容器控制了对象
控制什么?主要控制了外部资源及生命周期
由容器帮我们查找并注入依赖的对象,对象只能被动的接收依赖对象,依赖对象的获取被反转了。
在这里插入图片描述
spring中提供了一种IOC容器,来控制对象的创建,无论是你创建对象,处理对象之间的依赖关系,对象的创建时间还是对象的创建数量,都是spring提供IOC容器上配置对象的信息就可以了。

IOC能做什么

由IOC容器帮对象找相应的依赖思想并注入,并不是由对象主动去找
资源集中管理,实现资源的可配置和易管理
降低了使用资源双方的依赖程度,松耦合

Spring容器管理对象

1、maven管理依赖

引入spring核心依赖

    <properties>
        <spring.propety>4.1.7.RELEASE</spring.propety>
    </properties>
    <dependencies>
        <!--spring核心依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.propety}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.propety}</version>
        </dependency>
	<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.propety}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.propety}</version>
        </dependency>
    </dependencies>

2、给定容器的配置文件

将要交个容器的对象配置在该配置文件中,配置文件名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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    </beans> 

3、IOC容器管理对象

将userInfo对象交给容器管理

    <!--使用bean标签来管理对象,id属性必填,用来表示对象,class属性给定对象的全限定名-->
    <bean id="userInfo" class="com.tulun.Spring.IOC.ObjectUse.UserInfo"/>

4、通过容器来获取对象

    public static void main(String[] args) {
        //获取IOC容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springcontext1.xml");
        //获取对象
        UserInfo userInfo = (UserInfo)context.getBean("userInfo");
        System.out.println(userInfo);
    }

执行结果:
在这里插入图片描述
通过spring容器获取到了对象。

总结:Spring IOC操作
1、增加一个spring配置文件
2、解析xml的配置文件
3、BeanFactory工厂类
4、在工厂类中使用反射创建bean对象

Spring中IOC容器介绍

在spring中,spring-core包模块中包含IOC技术,IOC容器主要是为了管理对象,以及对象之间的依赖关系。
在这里插入图片描述
容器实现主要是BeanFactory接口和ApplicationContext接口,其继承关系如上图所示:
**BeanFactory:**是Spring IOC的最底层设计,提供了先进的配置机制,使得任何类型的对象配置成为可能。
ApplicationContext:ApplicationContext接口是继承自BeanFactory,实现了许多接口的扩展

BeanFactory的使用和ApplicationContext类似

BeanFactory factory = new xmlBeanFactory(new ClassPathResource("context.xml"));
fatory.getBean("XXX");

BeanFactory和ApplicationContext区别在于:BeanFactory的实现是按需创建,即第一次获取Bean才创建这个Bean,而ApplicationContext会一次性创建所有Bean。
BeanFactory是ApplicationContext父接口,ApplicationContext也提供了一个额外的功能,比如提供支持国际化、事件通知等,通常总是使用ApplicationContext,很少使用BeanFactory。
ApplicationContext接口常见实现类

  • ClassPathXmlApplicationContext

读取的xml配置文件放置在类路径下,优先考虑使用ClassPathXmlApplicationContext

//获取IOC容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“springcontext1.xml”);

  • FileSystemXmlApplicationContext

读取的配置文件放置在文件系统路径下,则优先考虑使用FileSystemXmlApplicationContext

FileSystemXmlApplicationContext context1 = new FileSystemXmlApplicationContext(“c://applacation.xml”);

  • XMLWebApplicationContext

需要在web环境下读取配置信息XMLWebApplicationContext

Spring中Bean的实例化方式

spring中装配Bean的方式主要两种
基于xml配置形式
基于注解形式

1、基于XML配置方式装配bean

bean基于配置实例化方式有三种形式

  • 通过无参构造实例化
  • 通过静态功能方式实例化
  • 通过普通工厂实例化

无参构造方式实例化对象
通过类的全限定名找到类的位置,通过无参构造函数创建对象

<bean id="userInfo" class="com.tulun.Spring.IOC.ObjectUse.UserInfo"/>

:如果不指定构造函数,会生成默认的无参构造函数
如果指定有参构造,必须显性的指定一个无参构造函数,否则实例化对象就会抛出异常。
静态工厂方法实例化对象
首先,使用工厂的静态方法返回去一个对象

public class ObjectFactory {
    //提供静态的方法来获取UserInfo对象
    public static UserInfo getBean() {
        return new UserInfo();
    }
}

在配置文件中使用静态方法返回对象

    <!--
    通过静态工厂获取对象
    class属性指向工厂类全限定名
    factory-method属性是工厂类中获取对象的静态方法
    -->
    <bean id="userInfo1" class="com.tulun.Spring.IOC.IOCDemo.ObjectFactory" factory-method="getBean"/>

通过普通工厂方法实例化对象
通过工厂的非静态方法来得到一个对象

public class ObjectFactory {
    //提供普通方法来获取UserInfo对象
    public UserInfo getUserInfo() {
        return new UserInfo();
    }
}

在容器中使用工厂的非静态方法获取对象

    <!--
    通过普通工厂获取对象
    -->
    <!--创建工厂对象-->
    <bean id="factory" class="com.tulun.Spring.IOC.IOCDemo.ObjectFactory"/>
    <bean id="userInfo2" class="com.tulun.Spring.IOC.ObjectUse.UserInfo" factory-bean="factory" factory-method="getUserInfo"/>

2、使用注解方式装配Bean

通过Component注解标记类

@Component(value = "userInfo")
/**
 * 
 * 注解形式类似于配置中bean标签处理
 * 注解中value参数和bean标签中id是相同的作用
 * <bean id="userInfo" class="com.tulun.Spring.IOC.ObjectUse.UserInfo"/>
 */
public class UserInfo {
    private String name;
    private String sex;
    public UserInfo(){}
    } 

配置启动组件扫描注解

<?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: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">
       <!--开启注解扫描,指定的包路径或者类名:扫描类、方法、属性上是否有注解-->
    <context:component-scan base-package="com.tulun.Spring.IOC.ObjectUse"/>
    <!--扫描属性上的注解(不建议使用)-->
    <context:annotation-config></context:annotation-config>
</beans>

@Component注解是spring最早的标注类要交给容器管理的注解
还有以下注解:
@Component
@Controller 对Controller层类进行标注(主要接受处理URL)
@Service 对Service层的类进行标注(是进行业务处理)
@Repository 对DAO层实现类进行标注(和数据库打交道的类)
后三个都是Component注解衍生出来,功能是一样,可以互换,为了区分被注解标注的类在不同的业务层,使逻辑更清晰。

IOC中属性讲解:
Bean的作用范围@Scope
@Scope取值:
singleton:单例对象,默认是单例
prototype:多例对象

Spring中DI介绍

DI-Dependency Injection:依赖注入
bean对象中需要依赖一些其他组件或者对象,依赖关系由容器在运行时决定
两种方式处理依赖注入:
基于配置形式
基于注解形式

基于配置形式注入依赖

有参构造函数注入依赖

public class User {
    private String name;
    private UserInfo userInfo;
    public User(String name,UserInfo userInfo) {
        this.name = name;
        this.userInfo = userInfo;
    }

set方法注入依赖

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

bean配置:

    <bean id="user1" class="com.tulun.Spring.IOC.ObjectUse.User">
        <property name="name" value="hello"/>
    </bean>

其他类型数据介绍

public class User {
        private String name;
        private UserInfo userInfo;
        //list类型
        private List<String> lists;
        //map类型
        private Map<String,String> maps;
        //set类型
        private Set<String>  sets;
	public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
	public void setLists(List <String> lists) {
        this.lists = lists;
    }
    public void setMaps(Map <String, String> maps) {
        this.maps = maps;
    }
    public void setSets(Set <String> sets) {
        this.sets = sets;
    }
<property name="lists" >
            <!--list属性注入-->
            <list >
                <value>张三</value>
                <value>李四</value>
            </list>
        </property>
        <property name="maps">
            <!--map类型数据注入-->
            <map>
                <entry key="k1" value="k11"/>
                <entry key="k2" value="k22"/>
            </map>
        </property>
        <property name="sets">
            <!--set类型数据注入-->
            <set>
                <value>set1</value>
                <value>set2</value>
            </set>
        </property>

基于注解形式注入依赖

@Value() //注入普通类型属性
@Autowired 注入对象类型
@Resource 注入对象类型
都是在属性上添加

@Component
public class User {
@Value("tulun") //针对普通类型的数据
        private String name;
         @Autowired  //注入对象类型
        @Resource  //注入对象类型
        private UserInfo userInfo;

循环依赖的问题:
如果是使用构造函数的方式注入依赖,有可能会无法解决循环依赖的问题。
解决问题:
对于会产生循环依赖的问题的这些类可以通过setter方法注入,可以解决循环依赖。

标签:容器,对象,spring,介绍,UserInfo,IOC,public
来源: https://blog.csdn.net/m0_46853673/article/details/121274338

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有