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

Spring DI依賴注入實戰(zhàn)教程

 更新時間:2024年12月16日 11:27:50   作者:你讀書了嗎?  
這篇文章主要介紹了SpringDI依賴注入實戰(zhàn)教程,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

1 概述

  • 依賴
    • A對象中有著B對象的引用,需要使用B對象完成一些操作
    • A對象依賴B
  • 注入
    • 給A對象依賴的B對象賦值稱為注入

2 注入方式

1、set注入

2、構造注入

3、自動注入

4、p命名空間注入

3 可以注入的數(shù)據(jù)類型

基本類型

自定義對象

容器類型

? 數(shù)組

? 集合

? Map

? Properties

4 set注入

  • 創(chuàng)建對象的時候,Spring工廠會調用set方法給對象中的屬性賦值
  • 如果一個對象中的屬性沒有添加set方法,使用property賦值的時候偶會報錯
Error:(11, 13) java: 找不到符號
  符號:   方法 setId(int)
  位置: 類型為com.sw.entity.User的變量 user
import lombok.Data;
@Data
public class User {
    private Integer id;
    private String username;
    private String password;
    private String addr;
    private String info;
}
<!--  定義user  -->
<bean id="user" class="com.sw.entity.User">
    <property name="id" value="100111"></property>
    <property name="username" value="關羽"></property>
    <property name="info" value="賣雜糧的關羽"></property>
</bean>

5 構造器注入

通過對象的構造器注入屬性

<constructor-arg name="id" value="100110"></constructor-arg>
package com.sw.entity;
public class Hero {
    private Integer id;
    private String username;
    private String password;
    private String addr;
    private String info;
    public Hero() {
    }
    public Hero(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public Hero(Integer id, String username) {
        this.id = id;
        this.username = username;
    }
    public Hero(Integer id, String username, String password, String addr, String info) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.addr = addr;
        this.info = info;
    }
    @Override
    public String toString() {
        return "Hero{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", addr='" + addr + '\'' +
                ", info='" + info + '\'' +
                '}';
    }
}
    <!--  定義Hero  -->
    <bean id="hero" class="com.sw.entity.Hero">
        <constructor-arg name="id" value="100110"></constructor-arg>
        <constructor-arg name="username" value="劉備"></constructor-arg>
    </bean>
    <bean id="hero02" class="com.sw.entity.Hero">
        <constructor-arg name="username" value="趙云"></constructor-arg>
        <constructor-arg name="password" value="zhao"></constructor-arg>
    </bean>

6 自動注入

在bean標簽中聲明autowire的方式

Spring框架會在容器中查找符合參數(shù)的數(shù)據(jù)進行注入

  • byType
  • byName
package com.sw.service.impl;
import com.sw.dao.HeroDao;
import com.sw.entity.Hero;
import com.sw.service.HeroService;
public class HeroServiceImpl implements HeroService {
    private HeroDao heroDao;
    public void setHeroDao(HeroDao heroDao) {
        this.heroDao = heroDao;
    }
    @Override
    public Hero queryHeroById(Integer id) {
        return heroDao.selectHeroById(id);
    }
}
    <!--  heroService  -->
    <bean id="heroService01" class="com.sw.service.impl.HeroServiceImpl" autowire="byType"></bean>
    <!--  heroService  -->
    <bean id="heroService02" class="com.sw.service.impl.HeroServiceImpl" autowire="byName"></bean>
    @Test
    public void getHero01(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        HeroService heroService = ioc.getBean("heroService01",HeroService.class);
        Hero hero = heroService.queryHeroById(10010);
        System.out.println(hero);
    }
    @Test
    public void getHero02(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        HeroService heroService = ioc.getBean("heroService02",HeroService.class);
        Hero hero = heroService.queryHeroById(1001111);
        System.out.println(hero);
    }

自動注入還是通過set注入的方式注入

7 p命名空間注入

  • 使用p作為屬性的前綴,調用屬性執(zhí)行復制的操作
  • 實質上還是使用set方法賦值

xmlns:p=“http://www.springframework.org/schema/p”

    <!--  stu  -->
    <bean id="stu" class="com.sw.entity.Stu" p:id="100111" p:username="張三"></bean>
    @Test
    public void getStu(){
        ClassPathXmlApplicationContext ioc = 
        	new ClassPathXmlApplicationContext("applicationContext.xml");
        Stu stu = ioc.getBean(Stu.class);
        System.out.println(stu);
    }

8 數(shù)據(jù)源注入

  • 使用Spring工廠注入Druid數(shù)據(jù)源
  • 依賴
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

在dao中聲明數(shù)據(jù)源的引用

package com.sw.dao.impl;
import com.alibaba.druid.pool.DruidDataSource;
import com.sw.dao.HeroDao;
import com.sw.entity.Hero;
import java.sql.SQLException;
public class HeroDaoImpl implements HeroDao {
    private DruidDataSource dataSource;
    public DruidDataSource getDataSource() {
        return dataSource;
    }
    public void setDataSource(DruidDataSource dataSource) {
        this.dataSource = dataSource;
    }
    @Override
    public Hero selectHeroById(Integer id) {
        try {
            System.out.println(dataSource.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Hero hero = new Hero();
        hero.setId(id);
        return hero;
    }
}

配置

      <!--  引入外部配置文件  -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
  <!-- 配置數(shù)據(jù)源   -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--  heroDao  -->
    <bean id="heroDao" class="com.sw.dao.impl.HeroDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

測試

package com.sw;
import com.sw.dao.HeroDao;
import com.sw.entity.Hero;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
public class TestHeroDao {
    @Test
    public void getHeroDao() {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        HeroDao heroDao = ioc.getBean(HeroDao.class);
        Hero hero = heroDao.selectHeroById(222);
    }
}

4.9 容器類型數(shù)據(jù)注入

package com.sw.entity;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class Person {
    private Integer id;
    private String username;
    private String gender;
    private String info;
    private String[] hobby;
    private List<String> friend;
    private Map<String,String> phones;
}
<?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: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">
    <!--  person基本屬性注入  -->
    <bean id="person01" class="com.sw.entity.Person">
        <property name="id" value="100100"></property>
        <property name="username" value="宋江"></property>
        <property name="gender" value="male"></property>
        <property name="info" value="梁山頭頭"></property>
    </bean>
    <!--  注入數(shù)組  -->
    <bean id="person02" class="com.sw.entity.Person">
        <property name="hobby">
            <array>
                <value>籃球</value>
                <value>足球</value>
                <value>乒乓球</value>
            </array>
        </property>
    </bean>
    <bean id="person03" class="com.sw.entity.Person">
        <property name="friend">
            <list>
                <value>晁蓋</value>
                <value>扈三娘</value>
                <value>王婆</value>
                <value>武松</value>
            </list>
        </property>
    </bean>
    <!--  map參數(shù)  -->
    <bean id="person04" class="com.sw.entity.Person">
        <property name="phones">
            <map>
                <entry key="魯智深" value="10010"></entry>
                <entry key="林沖" value="10086"></entry>
                <entry key="李逵" value="10000"></entry>
                <entry key="李鬼" value="100000"></entry>
            </map>
        </property>
    </bean>
</beans>
package com.sw;
import com.sw.entity.Person;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPerson {
    @Test
    public void getPerson01(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person01 = ioc.getBean("person01", Person.class);
        System.out.println(person01);
    }
    @Test
    public void getPerson02(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person02 = ioc.getBean("person02", Person.class);
        System.out.println(person02);
    }
    @Test
    public void getPerson03(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person03 = ioc.getBean("person03", Person.class);
        System.out.println(person03);
    }
    @Test
    public void getPerson04(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person04 = ioc.getBean("person04", Person.class);
        System.out.println(person04);
    }
}

到此這篇關于SpringDI依賴注入的文章就介紹到這了,更多相關SpringDI依賴注入內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

湛江市| 西林县| 和硕县| 满洲里市| 九江市| 墨竹工卡县| 石嘴山市| 兴国县| 新绛县| 武义县| 江门市| 马山县| 甘泉县| 环江| 新乐市| 富阳市| 兖州市| 丰都县| 富民县| 巴塘县| 钦州市| 东方市| 观塘区| 会泽县| 郎溪县| 大名县| 耿马| 松滋市| 大悟县| 宁海县| 屏山县| 南昌市| 库伦旗| 丽江市| 招远市| 观塘区| 云安县| 铁岭县| 堆龙德庆县| 靖安县| 玛多县|