亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2022年07月29日 08:39:46   作者:·~簡(jiǎn)單就好  
在實(shí)際開(kāi)發(fā)中,我們往往要用到Spring容器為我們提供的諸多資源,例如想要獲取到容器中的配置、獲取到容器中的Bean等等。本文為大家詳細(xì)講講工具類(lèi)如何獲取到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)記,格式:@注解名稱(chēng)(屬性名稱(chēng)=屬性值,屬性名稱(chēng)=屬性值…)

②:使用注解,注解作用在類(lèi)上面,方法上面,屬性上邊

③:使用注解的目的:簡(jiǎn)化XML配置

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

@Component 普通的類(lèi)

@Controller 表現(xiàn)層

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

@Repository 持久層

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

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

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

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

②:在需要管理的類(lèi)上添加@Component注解

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

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

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

④:測(cè)試

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 用于注入普通類(lèi)型(String,int,double等類(lèi)型)

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

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

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

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

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

測(cè)試:用注解的方式創(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)論