Spring-Ioc的介绍

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


Spring中Ioc获取对象的介绍

一、Ioc

Ioc是什么?

IOC(Inversion of Control 即控制反转)将对象交给容器管理。
传统程序中定义对象:
1.创建person类
2.创建person属性
3.人为通过new的方式创建person对象
4.通过set方法给对象赋值

Ioc中定义对象
1.创建person类
2.创建person属性
3.将person属性值主动注入到person类中(控制反转,通过反射机制)
4.由Ioc容器统一进行管理Bean(person)对象

Ioc能做什么?

1.Spring IoC容器的本质目的是管理Bean,对于Bean而言在容器中存在其生命周期(Bean的初始化到销毁的过程)。
2.由IOC容器帮对象找相应的依赖思想并注入,并不是由对象主动去找资源集中管理,实现资源的可配置和易管理(高内聚,低耦合)。

二、Spring容器管理对象

1.依赖注入

1.先在pom.xml文件导入jar包
在properties标签内加入

 <!--  Spring统一版本号  -->
    <springVersion>5.1.1.RELEASE</springVersion>
<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--    Spring表达式    -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${springVersion}</version>
    </dependency>
    <dependency>
      <!--    Spring核心    -->
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${springVersion}</version>
    </dependency>
    <dependency>
      <!--    Spring实体    -->
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${springVersion}</version>
    </dependency>
    <dependency>
      <!--    Spring容器    -->
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springVersion}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${springVersion}</version>
    </dependency>

    <!-- Lombok工具 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
    </dependency>

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.xsd">
</beans>

3.Ioc容器管理对象(setter注入方式)

 <!--使用bean标签来管理对象,id属性不是必须的,class属性给定对象的全限定名-->
    <bean id="person" class="com.bean.Person">
        <property name="Pname" value="张三"/>
        <property name="pid" value="1001"/>
    </bean>

4.通过容器获取对象

   @Test
    public void t1(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person =(Person) ac.getBean("person");
        System.out.println(person);
    }

结果

Person(Pname=张三, Pid=1001)

##2. 注入方式

setter注入

请看上面的例子

构造器注入


"id":不是必须的属性,若没有指定id,Spring框架采用"全限定名#{number}"格式生成一个id编号
		            
					如com.bean.Source#{0}
<!--使用bean标签来管理对象,id属性不是必须的,class属性给定对象的全限定名-->
    <!-- index是有参构造的属性个数0代表第一个以此类型 value代表属性值-->
    <bean id="person" class="com.bean.Person">
      <constructor-arg index="0" value="李四"/>
        <constructor-arg index="1" value="1002"/>
    </bean>

结果

Person(Pname=李四, Pid=1002)

接口注入

1.使用的资源并非来自自身系统,而是来自于外界。例如在Tomcat下配置数据库连接资源然后通过JNDI的形式来获取它。
2.接口注入模式因为具备侵入性,它要求组件必须与特定的接口相关联,因此并不被看好,实际使用有限。

2.隐式Bean的发现机制和自动装配原则

(1)组件扫描:通过定义资源的方式,让Spring Ioc容器扫描对应的包从而把Bean装配进来。
a、使用@Component装配Bean

b、创建一个配置类,该类使用@ComponentScan注解告诉Ioc容器在什么地方扫描Bean @ComponentScan默认扫描当前包的路径(bean和配置类在同一个包才能扫描到)

c、在使用时要用AnnotationConfigApplicationContext类去创建Ioc容器
如:ApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfig.class);
在创建Ioc容器时,要将定义的配置类的信息导入

d.调用getBean()获取 如:bean Role role = ac.getBean(Person.class);

(2)自动装配:通过注解的定义来实现
代码实现:

在这里插入图片描述


@ComponentScan(basePackages = {"com.bean"})
public class BeanConfig {
}
@Data
@Component(value = "person")
public class Person {
    @Value("李四")
    private String Pname;
    @Value("1003")
    private Integer Pid;
}
  @Test
    public void t1(){
        //创建一个可以识别注解的Ioc容器,扫描Person类
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfig.class);
        //获取Person对象
        Person p = (Person) ac.getBean("person");
        System.out.println(p);
    }

结果

Person(Pname=李四, Pid=1003)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值