最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中利用IOC實(shí)現(xiàn)注入的方式

 更新時(shí)間:2023年04月20日 10:27:04   作者:_BugMan  
Spring IOC(控制反轉(zhuǎn))實(shí)現(xiàn)依賴注入,將對(duì)象創(chuàng)建和依賴關(guān)系的管理交由Spring容器處理,通過注解或XML配置,實(shí)現(xiàn)對(duì)象之間的松耦合,提高代碼復(fù)用性和可維護(hù)性

1.環(huán)境搭建

maven依賴:

<dependencies>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
   </dependency>
 </dependencies>

在resources文件夾下創(chuàng)建xml文件:

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="xxx" class="XXX">
        ......
    </bean>
</beans>

項(xiàng)目結(jié)構(gòu):

2.注入方式

2.1.配置文件

2.1.1.Set注入

在xml中使用property標(biāo)簽,調(diào)用set方法進(jìn)行注入。

實(shí)體類:

使用set方法注入,實(shí)體類中必須存在set方法,否則會(huì)注入失敗。

package com.eryi.beans;
public class User {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

xml文件:

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.eryi.beans.User">
        <property name="username" value="admin"></property>
        <property name="password" value="123"></property>
    </bean>
</beans>

測(cè)試:

public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=applicationContext.getBean("user",User.class);
        System.out.println(user.toString());
    }

2.1.2.構(gòu)造器注入

在xml文件中使用constructor-arg標(biāo)簽,調(diào)用構(gòu)造器進(jìn)行注入。

實(shí)體類:

使用構(gòu)造器注入,實(shí)體類中必須存在構(gòu)造器,否則會(huì)注入失敗。

package com.eryi.beans;
public class User {
    private String username;
    private String password;
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

xml:

<bean id="user" class="com.eryi.beans.User">
    <constructor-arg value="admin" type="java.lang.String"></constructor-arg>
    <constructor-arg value="123" type="java.lang.String"></constructor-arg>
</bean>

測(cè)試:

public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=applicationContext.getBean("user",User.class);
        System.out.println(user.toString());
    }

2.2.注解注入

Spring支持通過注解的方式將注入實(shí)體注入IOC,然后通過注解去IOC中獲取實(shí)體。

注入IOC的注解有:

@Component、@Repository、@Service、@Controller

@Component是通用的,后面三個(gè)只是為了細(xì)化層次為各個(gè)分層上的實(shí)體推出的,功能上來說都具有將實(shí)體注入IOC的能力。

從IOC中獲取實(shí)體的注解有:

@Autowired

代碼示例:

開啟注解掃描:

需要首先在Spring的配置文件中開啟注解掃描,這樣Spring才回去配置的路徑下掃描帶有注解的Bean將其納入IOC。

public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=applicationContext.getBean("user",User.class);
        System.out.println(user.toString());
    }

dao層:

package com.eryi.beans;
import org.springframework.stereotype.Component;
@Component
public class UserDao {
    public String findAll(){
        return "find all users";
    }
}

service:

@Component
public class UserService {
    private UserDao userDao;
    @Autowired
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }
    public String findAll(){
        return  userDao.findAll();
    }
}

測(cè)試:

package com.eryi.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=applicationContext.getBean("userService",UserService.class);
        System.out.println(userService.findAll());
    }
}

3.獲取方式

有兩種獲取方式:

  • 通過id
  • 通過類型

通過id:

UserService userService=applicationContext.getBean("userService",UserService.class);

通過類型:

UserService userService=applicationContext.getBean(UserService.class);

4.Bean關(guān)系

在Spring框架中,配置文件除了可以描述類以外,還可以描述Bean的依賴關(guān)系。

繼承關(guān)系:

<bean id="parentBean" class="com.eryi.beans.User">
   <property name="username" value="father"></property>
   <property name="password" value="123"></property>
</bean>
<!--使用parent來指向父類-->
   <bean id="childBean" class="com.eryi.beans.User" parent="parentBean">
   <property name="username" value="son"></property>
</bean>

抽象:

<bean>標(biāo)簽中的“abstract”屬性用于指定當(dāng)前Bean是否為抽象Bean,其取值為“true”或“false”,默認(rèn)為“false”。當(dāng)一個(gè)Bean被指定為抽象Bean時(shí),它不能被直接實(shí)例化,而只能作為其他Bean的父類或者模板。這個(gè)抽象Bean只是一個(gè)模板,其中可以定義一些通用的屬性或方法,供其他Bean繼承或者引用。

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
User user_01=applicationContext.getBean("childBean",User.class);
System.out.println(user_01);
User user_02=applicationContext.getBean("parentBean",User.class);
System.out.println(user_02);

5.Bean生命周期

一個(gè)bean在完整生命周期中會(huì)觸發(fā)的方法順序:

  • 構(gòu)造器
  • 注入屬性(set、構(gòu)造、或者命名空間)
  • init-method方法
  • 生成實(shí)例對(duì)象
  • destroy-method(容器關(guān)閉觸發(fā)該方法)

注意:init-method、destroy-method需要配置才會(huì)生效。

配置:

<bean id="user" class="com.eryi.beans.User" init-method="init" destroy-method="destroy">
        <property name="username" value="admin"></property>
        <property name="password" value="123"></property>
</bean>

實(shí)體類:

public class User {
    private String username;
    private String password;
    public User() {
        System.out.println("construction......");
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        System.out.println("set......");
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    private void init(){
        System.out.println("init......");
    }
    private void destroy(){
        System.out.println("destroy......");
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

測(cè)試:

ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=context.getBean("user",User.class);
context.close();

6.Bean作用域

四種作用域:

  • singleton,默認(rèn)的,在IOC初始化時(shí)創(chuàng)建容器中的實(shí)例,全局訪問的是同一個(gè)實(shí)例。
  • prototype,IOC初始化時(shí)不生成實(shí)例,每次請(qǐng)求生成一個(gè)實(shí)例。
  • session,同一個(gè)Session共享一個(gè)Bean實(shí)例。不同Session使用不同的實(shí)例。
  • request 該屬性僅對(duì)HTTP請(qǐng)求產(chǎn)生作用,每次HTTP請(qǐng)求都會(huì)創(chuàng)建一個(gè)新的實(shí)例
  • global-session 所有的Session共享一個(gè)Bean實(shí)例。

到此這篇關(guān)于Spring中利用IOC實(shí)現(xiàn)注入的方式的文章就介紹到這了,更多相關(guān)Spring IOC注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot中MyBatis-Flex的集成和使用實(shí)現(xiàn)

    SpringBoot中MyBatis-Flex的集成和使用實(shí)現(xiàn)

    MyBatis-Flex是一個(gè)基于MyBatis的數(shù)據(jù)訪問框架,MyBatis-Flex能夠極大地提高我們的開發(fā)效率和開發(fā)體驗(yàn),本文主要介紹了SpringBoot中MyBatis-Flex的集成和使用實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • SpringMVC框架整合Junit進(jìn)行單元測(cè)試(案例詳解)

    SpringMVC框架整合Junit進(jìn)行單元測(cè)試(案例詳解)

    本文詳細(xì)介紹在SpringMVC任何使用Junit框架。首先介紹了如何引入依賴,接著介紹了編寫一個(gè)測(cè)試基類,并且對(duì)其中涉及的各個(gè)注解做了一個(gè)詳細(xì)說明,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Java中的方法、常量、變量、參數(shù)用例詳解

    Java中的方法、常量、變量、參數(shù)用例詳解

    在JVM的運(yùn)轉(zhuǎn)中,承載的是數(shù)據(jù),而數(shù)據(jù)的一種變現(xiàn)形式就是“量”,量分為:常量與變量,我們?cè)跀?shù)學(xué)和物理學(xué)中已經(jīng)接觸過變量的概念了,在Java中的變量就是在程序運(yùn)行過程中可以改變其值的量,這篇文章主要介紹了Java中的方法、常量、變量、參數(shù),需要的朋友可以參考下
    2024-01-01
  • Java遞歸遍歷文件目錄代碼實(shí)例

    Java遞歸遍歷文件目錄代碼實(shí)例

    這篇文章主要介紹了Java遞歸遍歷文件目錄代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Spring?JDBC?框架簡(jiǎn)介

    Spring?JDBC?框架簡(jiǎn)介

    Spring?JDBC?提供幾種方法和數(shù)據(jù)庫(kù)中相應(yīng)的不同的類與接口。我將給出使用JdbcTemplate類框架的經(jīng)典和最受歡迎的方法。本文給大家介紹Spring?JDBC?框架的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-12-12
  • Java使用注解和反射簡(jiǎn)化編程的方法示例

    Java使用注解和反射簡(jiǎn)化編程的方法示例

    這篇文章主要介紹了Java使用注解和反射簡(jiǎn)化編程的方法,結(jié)合實(shí)例形式分析了java使用注解和反射調(diào)用大量函數(shù)簡(jiǎn)化編程的相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • 淺談Java線程Thread之interrupt中斷解析

    淺談Java線程Thread之interrupt中斷解析

    這篇文章主要介紹了Java線程Thread之interrupt中斷解析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • 很簡(jiǎn)單的Java斷點(diǎn)續(xù)傳實(shí)現(xiàn)原理

    很簡(jiǎn)單的Java斷點(diǎn)續(xù)傳實(shí)現(xiàn)原理

    這篇文章主要以實(shí)例的方式為大家詳細(xì)介紹了簡(jiǎn)單的Java斷點(diǎn)續(xù)傳實(shí)現(xiàn)原理,感興趣的小伙伴們可以參考一下
    2016-07-07
  • SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼

    SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼

    這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • IDEA java出現(xiàn)無效的源發(fā)行版14解決方案

    IDEA java出現(xiàn)無效的源發(fā)行版14解決方案

    這篇文章主要介紹了IDEA java出現(xiàn)無效的源發(fā)行版14解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論

凌源市| 斗六市| 贵州省| 祁阳县| 肥城市| 临沂市| 十堰市| 恩平市| 元谋县| 江孜县| 武宁县| 鄄城县| 莱州市| 固镇县| 上高县| 彭泽县| 泰来县| 秦安县| 榆社县| 天水市| 锦屏县| 渝中区| 乌审旗| 中卫市| 夏邑县| 连江县| 英超| 璧山县| 临汾市| 开江县| 庄河市| 镇雄县| 阜平县| 高邮市| 紫金县| 灵台县| 容城县| 漳平市| 确山县| 布拖县| 化隆|