欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring框架對(duì)于Bean的管理詳解

 更新時(shí)間:2022年07月29日 08:39:46   作者:·~簡單就好  
在實(shí)際開發(fā)中,我們往往要用到Spring容器為我們提供的諸多資源,例如想要獲取到容器中的配置、獲取到容器中的Bean等等。本文為大家詳細(xì)講講工具類如何獲取到Spring容器中的Bean,需要的可以參考一下

什么是Bean管理

bean管理指的是如下的兩個(gè)操作。

1.創(chuàng)建對(duì)象

2.注入屬性

Bean管理操作的兩種方式

1.基于xml配置文件的方式實(shí)現(xiàn)

2.基于注解方式實(shí)現(xiàn)

基于注解的方式實(shí)現(xiàn)Bean管理和注入屬性(常用)

1.什么是注解

①:注解是代碼特殊標(biāo)記,格式:@注解名稱(屬性名稱=屬性值,屬性名稱=屬性值…)

②:使用注解,注解作用在類上面,方法上面,屬性上邊

③:使用注解的目的:簡化XML配置

2.Spring針對(duì)Bean管理中創(chuàng)建對(duì)象提供的注解

@Component 普通的類

@Controller 表現(xiàn)層

@Service 業(yè)務(wù)層

@Repository 持久層

上邊四個(gè)功能一樣,都可以用來創(chuàng)建bean實(shí)例

3.用注解的方式創(chuàng)建對(duì)象

①:編寫接口和實(shí)現(xiàn)類

package com.qcby.testanno;
public interface UserService {
    public void hello();
}

②:在需要管理的類上添加@Component注解

package com.qcby.testanno;
import org.springframework.stereotype.Component;
/* <bean id="us" class="UserServiceImpl"/> */
/**
 * 組件,作用:把當(dāng)前類使用IOC容器進(jìn)行管理,如果沒有指定名稱,默認(rèn)使用類名,首字母是小寫。
 * userServiceImpl。或者自己指定名稱
 **/
@Controller(value="us")
public class UserServiceImpl implements UserService {
    public void hello() {
        System.out.println("使用注解,方便吧!");
    }
}

③:編寫配置文件,重點(diǎn)是開啟注解掃描。

<?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">
    <!--開啟注解掃描 com.qcby所有的包中的所有的類-->
    <context:component-scan base-package="com.qcby"/>
</beans>

④:測試

package com.qcby.test;
import com.qcby.testanno.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
    @Test
    public void run1(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContextanno.xml");
        UserService us = (UserService) ac.getBean("us");
        us.hello();
    }
}

4.用注解的方實(shí)現(xiàn)屬性注入

@Value 用于注入普通類型(String,int,double等類型)

@Autowired 默認(rèn)按類型進(jìn)行自動(dòng)裝配(引用類型)

@Qualifier 不能單獨(dú)使用必須和@Autowired一起使用,強(qiáng)制使用名稱注入

@Resource Java提供的注解,也被支持。使用name屬性,按名稱注入

具體的代碼如下:用注解的方式創(chuàng)建對(duì)象和賦值

// 默認(rèn)當(dāng)前類名就是ID名稱,首字母小寫
@Component(value = "car")
// @Controller
// @Service(value = "c")
// @Repository(valu = "c")
public class Car {
    // 注解注入值,屬性set方法是可以省略不寫的。
    // 只有一個(gè)屬性,屬性的名稱是value,value是可以省略不寫的
    @Value("大奔2")
    private String cname;
    @Value(value = "400000")
    private Double money;
    // 也不用提供set方法
    // 按類型自動(dòng)裝配的注解,和id名稱沒有關(guān)系
    @Qualifier("person")
    @Autowired  //注意:(所引用的Car類里面必須要寫:類聲明@Component(value="person") )
    // 注入引用數(shù)據(jù)類型:不需要寫值,根據(jù)類型自動(dòng)匹配
    //@Resource(name="person")  //是jdk提供,按照名稱進(jìn)行注入,只能在引用數(shù)據(jù)類型當(dāng)中發(fā)使用
    private Person person;
    @Override
    public String toString() {
        return "Car{" +
                "cname='" + cname + '\'' +
                ", money=" + money +
                ", person=" + person +
                '}';
    }
}

注意聲明:@Component(value = “person”)

@Component(value = "person")
public class Person {
    @Value("張三")
    private String pname;
    @Override
    public String toString() {
        return "Person{" +
                "pname='" + pname + '\'' +
                '}';
    }
}

測試:用注解的方式創(chuàng)建對(duì)象和賦值

@Test
public void run1(){
    // 工廠
    ApplicationContext ac = new
            ClassPathXmlApplicationContext("applicationContext.xml");
    // 獲取對(duì)象
    Car car = (Car) ac.getBean("car");
    System.out.println(car);
}

到此這篇關(guān)于Spring框架對(duì)于Bean的管理詳解的文章就介紹到這了,更多相關(guān)Spring Bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論