依赖注入、Bean的自动装配等

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了依赖注入、Bean的自动装配等脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

6、依赖注入

6.1、构造器注入

 之前写的

6.2、Set方式注入【重点】
  • 依赖注入:Set注入!

    • 依赖:bean对象的创建依赖于容器

    • 注入:bean对象中的所有属性,由容器来注入!

【环境搭建】

  1. 复杂类型

    public class Address {    PRivate String address;    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }}
  2. 真实测试对象

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; }
  1. beans.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">    <bean id="student" class="com.kuang.pojo.Student"><!--        第一种,普通值注入,value-->        <property name="name" value="荣哥"></property>    </bean></beans>
  2. 测试类

    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.getName());    }}
  3. 完善注入信息

        <bean id="address" class="com.kuang.pojo.Address">        <property name="address" value="南昌"></property>    </bean>    <bean id="student" class="com.kuang.pojo.Student"><!--        第一种,普通值注入,value-->        <property name="name" value="荣哥"></property><!--        第二种,Bean注入,ref-->        <property name="address" ref="address"></property><!--        数组-->        <property name="books" >            <array>                <value>红楼梦</value>                <value>西游记</value>                <value>水浒传</value>                <value>三国演义</value>            </array>        </property><!--            List-->        <property name="hobbys">            <list>                <value>听歌</value>                <value>敲代码</value>                <value>看电影</value>            </list>        </property><!--        Map-->        <property name="card">            <map>                <entry key="身份证" value="12121242"></entry>                <entry key="银行卡" value="124253434"></entry>            </map>        </property><!--        Set-->        <property name="games">            <set>                <value>LOL</value>                <value>COC</value>            </set>        </property><!--        null-->        <property name="wife">            <null/>        </property><!--        Properties-->        <property name="info">            <props>                <prop key="driver">20214160005</prop>                <prop key="url">20214160005</prop>                <prop key="username">root</prop>                <prop key="password">123456</prop>            </props>        </property>    </bean>

 

6.3、拓展方式注入

我们可以使用p命名空间和c命名空间注入

<?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:p="http://www.springframework.org/schema/p"       xmlns:c="http://www.springframework.org/schema/c"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd"><!--    p命名空间注入,可以直接注入属性的值:property-->    <bean id="user" class="com.kuang.pojo.User" p:name="荣哥" p:age="15"></bean><!--    c命名空间注入,可以通过构造器注入:constructor-arg-->    <bean id="user2" class="com.kuang.pojo.User" c:name="荣荣" c:age="18"></bean></beans>

测试:

    @Test    public void test2(){        ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");//        User user = (User) context.getBean("user");        User user = context.getBean("user2", User.class);        System.out.println(user);    }

 

注意点:p命名和c命名空间不能直接使用,需要导入约束

       xmlns:p="http://www.springframework.org/schema/p"       xmlns:c="http://www.springframework.org/schema/c"

 

6.4、bean的作用域

依赖注入、Bean的自动装配等

 

  1. 单例模式(Spring默认机制)

    <!--    c命名空间注入,可以通过构造器注入:constructor-arg-->    <bean id="user2" class="com.kuang.pojo.User" c:name="荣荣" c:age="18" scope="singleton"></bean>
  2. 原型模式:每次从容器中get的时候,都会产生一个新对象!

    <!--    c命名空间注入,可以通过构造器注入:constructor-arg-->    <bean id="user2" class="com.kuang.pojo.User" c:name="荣荣" c:age="18" scope="prototype"></bean>
  3. 其余的request、session、application这些只能在web开发中使用到!

7、Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式!

  • Spring会在上下文中自动寻找,并自动给bean装配属性!

 

在Spring中有三种装配的方式

  1. 在xml中显示的配置

  2. 在java中显示配置

  3. 隐式 的自动装配

7.1、测试
  1. 环境搭建:一个人有两个宠物!

7.2、ByName自动装配
    <bean id="dog" class="com.kuang.pojo.Dog"></bean>    <bean id="cat" class="com.kuang.pojo.Cat"></bean><!--    byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid        byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean,如果有两个Dog的就报错了-->    <bean id="people" class="com.kuang.pojo.People" autowire="byType">        <property name="name" value="荣哥">        </property>    </bean>

小结:

  • byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入属性的set方法值一致!

  • byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!

7.4、使用注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了!

要使用注解须知:

  1. 导入约束 : context约束

  2. 配置注解的支持 :context:annotation-config/

    <?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        https://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        https://www.springframework.org/schema/context/spring-context.xsd">    <context:annotation-config/></beans>

    @Autowired

    直接在属性上使用即可!也可以在set方式上使用!

    使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字

    科普:

    @Nullable   字段标记了这个注解,说明这个字段可以为null;
    public @interface Autowired {    boolean required() default true;}

    测试代码

    public class People {    //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空    @Autowired(required = false)    private Cat cat;    @Autowired    private Dog dog;    private String name;

     

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解 @Autowired 完成的时候,我们可以使用

@Qualifier(value = "xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入

public class People {    //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空    @Autowired(required = false)    @Qualifier(value = "cat1")    private Cat cat;        @Autowired    private Dog dog;    private String name;

@Resource注解

public class People {    @Resource(name = "cat778")    private Cat cat;        @Resource    private Dog dog;} 

 

小结:

@Autowired 和 @Resource 的区别:

  • 都是用来自动装配的,都可以放在属性字段上

  • @Autowired 通过byname的方式实现,而且必须要求这个对象存在!【常用】

  • @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】

  • 执行顺序不同:@Autowired 是通过byType的方式实现。@Resource 默认通过byName的方式实现。

  •  

 

 

 

 

 

 

脚本宝典总结

以上是脚本宝典为你收集整理的依赖注入、Bean的自动装配等全部内容,希望文章能够帮你解决依赖注入、Bean的自动装配等所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。