ICode9

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

Bean作用域、自动、手动、注解实现装配

2021-06-17 21:03:29  阅读:114  来源: 互联网

标签:name 作用域 void dog cat Bean 注解 public String


 bean的作用域

有六种作用域:

(1)singleton单例模式,也是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">
    <!--这种方式等于下边写 scope="singleton" -->
    <bean id="student" class="com.lxc.domain.Student" scope="singleton">
        <property name="name" value="Spring"/>
    </bean>
    <!-- <bean id="student" class="com.lxc.domain.Student" scope="singleton">
        <property name="name" value="Spring"/>
    </bean> -->
</beans>

测试下,下边调用两次 getBean( )

import com.lxc.domain.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        // spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans2.xml");
        Student student = (Student) ctx.getBean("student");
        Student student1 = ctx.getBean("student", Student.class);
        System.out.println(student == student1);
    }
}

 结果输出true,说明,两次的bean都是同一个对象。

 

(2)prototype原型模式

<?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.lxc.domain.Student" scope="prototype">
        <property name="name" value="Spring"/>
    </bean>
</beans>

测试调用:

(3)其余几个request、session、application这些只能在web开发中使用,在MVC时在记录。

 

Bean的自动装配

· 上一篇文章中,全都是手动装配的,而自动装配是Spring满足bean依赖的一种方式!
· Spring会在上下文中自动寻找,并自动给bean装配属性

Spring中三种装配的方式

(1)在xml中显示的手动配置。
(2)在java中显示的配置。
(3)隐式自动装配(很重要)

 

(1)先来看下手动配置

Person

package com.lxc.domain;

public class Person {
    private Cat cat;
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 Dog

package com.lxc.domain;
public class Dog {
    public void shout() {
        System.out.println("汪汪");
    }
}

Cat

package com.lxc.domain;

public class Cat {
    public void shout() {
        System.out.println("喵");
    }
}
<!-- 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="cat" class="com.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person">
        <property name="name" value="Spring"/>
        <property name="cat" ref="cat" />
        <property name="dog" ref="dog" />
    </bean>
</beans>

测试

import com.lxc.domain.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Person person = ctx.getBean("person", Person.class);
        person.getCat().shout();
        person.getDog().shout();
    }
}

输出结果

 

(2)在来看下自动配置 byName

         只需要改动beans.xml代码即可:

         添加 autowire="byName" 属性;

byName原理:它会自动在容器上下文中查找, 和自己对象set方法后边的值对应的<beans>标签上的id是否一致,一致的话,property就不需要引用cat和dog了,使用byName还需要保证bean的id必须唯一。

<?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="cat" class="com.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person" autowire="byName">
        <property name="name" value="lxc"/>
    </bean>
</beans>

 

(3)使用注解自动装配

jdk1.5支持注解

@Autowired:在属性或set方法上边使用,在属性上边使用注解,下边的set方法可以省略。

步骤:

· 导入约束 —— context

·配置注解支持

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

 

还是看上边的例子:

<?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/> <!--注解的支持-->

    <bean id="cat" class="com.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person">
        <property name="name" value="lxc" />
    </bean>

</beans>

Person

在Cat和Dog对象上加上 @Autowired注解即可

package com.lxc.domain;

import org.springframework.beans.factory.annotation.Autowired;

public class Person {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    // 可以去掉下边set方法,因为上边上属性使用了注解
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    // 同上,下边set方法可以省略
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

测试

 

(4)补充

@Nullable   字段标记了这个注解,说明该字段可以为null;

@Autowired(required  = false)
private String name;  // 加上@Autowired(required  = false)该字段表示可以为null。

 

 

标签:name,作用域,void,dog,cat,Bean,注解,public,String
来源: https://blog.csdn.net/qq_42778001/article/details/117997887

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

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

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

ICode9版权所有