[SSM]Spring对IoC的实现

这篇具有很好参考价值的文章主要介绍了[SSM]Spring对IoC的实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

四、Spring对IoC的实现

4.1IoC控制反转

4.2依赖注入

4.2.1set注入

4.2.2构造注入

4.3set注入专题

4.3.1注入外部Bean

4.3.2注入内部Bean

4.3.3注入简单类型

4.3.4级联属性赋值

4.3.5注入数组

4.3.6注入List、Set、Map集合

4.3.7注入Properties

4.3.8注入null和空字符串

4.3.9注入的值中含有特殊符号

4.4p命名空间注入

4.5c命名空间注入

4.6util命名空间

4.7基于XML的自动装配

4.7.1根据名称自动装配

4.7.2根据类型自动装配

4.8Spring引入外部属性配置文件


四、Spring对IoC的实现

4.1IoC控制反转

  • 控制反转是一种思想。

  • 控制反转是为了降低程序耦合度,提高程序扩展力,达到OCP原则、DIP原则。

  • 控制反转,反转的是什么?

    • 将对象的创建权交出去,交给第三方容器负责。

    • 将对象和对象之间的关系维护权交出去,交给第三方容器负责。

  • 控制反转这种思想如何实现?

    • DI(Dependency Injection):依赖注入

4.2依赖注入

  • 依赖注入实现了控制反转的思想。

  • Spring通过依赖注入的方式来完成Bean的管理。

  • Bean的管理说的是:Bean对象的创建,以及Bean对象中属性的赋值(或者叫做Bean对象之间关系的维护)。

  • 依赖注入:

    • 依赖指的是对象和对象之间的关联关系。

    • 注入指的是一种数据传递行为,通过注入行为来让对象和对象产生关系。

  • 依赖注入常见的实现方式包括两种:

    • 第一种:set注入。

    • 第二种:构造注入。

4.2.1set注入
  • set注入,基于set方法实现的,底层会通过反射机制调用属性对应的set方法然后给属性赋值。这种机制要求属性必须对外提供set方法。

UserDao

package com.hhb.dao;
​
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
​
public class UserDao {
    private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
    public void insert() {
        //使用log4j2日志框架
        logger.info("数据库正在保存用户信息");
    }
}

UserService

package com.hhb.service;
​
import com.hhb.dao.UserDao;
import com.hhb.dao.VipDao;
​
public class UserService {
    private UserDao userDao;
    //set注入的话,必须提供一个set方法
    //Spring容器会调用这个set方法来给userDao属性赋值。
    //set方法必须以set单词开头
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void saveUser() {
        //保存用户信息到数据库
        userDao.insert();
        vipDao.insert();
    }
}

spring.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">
​
    <!--配置dao-->
    <bean id="userDaoBean" class="com.hhb.dao.UserDao"/>
​
    <!--配置service-->
    <bean id="userServiceBean" class="com.hhb.service.UserService">
        <!--想让spring调用对应的set方法,需要配置property标签-->
        <!--name属性是set方法的方法名,去掉set,然后把剩下的单词首字母变小写-->
        <!--ref翻译为引用,全拼为references。ref后面要指定的是要注入bean的id-->
        <property name="userDao" ref="userDaoBean"/>
    </bean>
</beans>

测试

@Test
    public void testSetDI(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        UserService userServiceBean = applicationContext.getBean("userServiceBean", UserService.class);
        userServiceBean.saveUser();
    }
  • 实现原理:

    • 通过property标签获取到属性名:userDao

    • 通过属性名推断出set方法名:setUserDao

    • 通过反射机制调用setUserDao()方法给属性赋值

    • property标签的name是属性名

    • property标签的ref是要注入的bean对象的id。(通过ref属性来完成bean的装配,这是bean最简单的一种装配方式。装配指的是:创建系统组件之间关联的动作)

  • set注入的核心实现原理:通过反射机制调用set方法来给属性赋值,让两个对象之间产生关系。

4.2.2构造注入
  • 核心原理:通过调用构造方法来给属性赋值。

UserDao

package com.hhb.dao;
​
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
​
public class UserDao {
    private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
    public void insert() {
        //使用log4j2日志框架
        logger.info("数据库正在保存用户信息");
    }
}

VipDao

package com.hhb.dao;
​
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
​
public class VipDao {
    private static final Logger logger=LoggerFactory.getLogger(VipDao.class);
​
    public void insert(){
        logger.info("正在保存VIP信息");
    }
}

CustomerService

package com.hhb.service;
​
import com.hhb.dao.UserDao;
import com.hhb.dao.VipDao;
​
public class CustomerService {
    private UserDao userDao;
    private VipDao vipDao;
​
    public CustomerService(UserDao userDao, VipDao vipDao) {
        this.userDao = userDao;
        this.vipDao = vipDao;
    }
​
    public void save() {
        userDao.insert();
        vipDao.insert();
    }
}
 

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="userDaoBean" class="com.hhb.dao.UserDao"/>
    <bean id="vipDaoBean" class="com.hhb.dao.VipDao"/>
​
    <bean id="csBean3" class="com.hhb.service.CustomerService">
        <!--不指定下标,也不指定参数名,让spring自己做类型匹配-->
        <!--这种方式实际上是根据类型进行注入的,spring会自动根据类型来判断把ref注入到哪个参数-->
        <constructor-arg ref="userDaoBean"/>
        <constructor-arg ref="vipDaoBean"/>
    </bean>
​
    <bean id="csBean2" class="com.hhb.service.CustomerService">
        <!--根据构造方法参数的名字进行注入-->
        <constructor-arg name="vipDao" ref="vipDaoBean"/>
        <constructor-arg name="userDao" ref="userDaoBean"/>
    </bean>
​
    <bean id="csBean" class="com.hhb.service.CustomerService">
        <!--构造注入-->
        <!--
            index属性指定参数下标,第一个参数是0,第二个参数是2...
            ref属性用来指定注入的bean的id
        -->
        <constructor-arg index="0" ref="userDaoBean"/>
        <constructor-arg index="1" ref="vipDaoBean"/>
    </bean>
</beans>

测试

@Test
    public void testConstructDI() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        CustomerService csBean = applicationContext.getBean("csBean", CustomerService.class);
        csBean.save();
​
        CustomerService csBean2 = applicationContext.getBean("csBean2", CustomerService.class);
        csBean2.save();
​
        CustomerService csBean3 = applicationContext.getBean("csBean3", CustomerService.class);
        csBean3.save();
    }
  • 通过测试得知,通过构造方法注入的时候:

    • 可以通过下标

    • 可以通过参数名

    • 也可以不指定下标和参数名,可以类型自动推断

4.3set注入专题

4.3.1注入外部Bean

set-di.xml

<bean id="orderDaoBean" class="com.hhb.dao.OrderDao"/>
​
    <bean id="orderServiceBean" class="com.hhb.service.OrderService">
        <property name="orderDao" ref="orderDaoBean"/>
    </bean>
  • 外部Bean的特点:bean定义到外面,在property标签中使用ref属性进行注入,通常这种方式是常用的。

4.3.2注入内部Bean

set-di.xml

 <bean id="orderServiceBean2" class="com.hhb.service.OrderService">
     <property name="orderDao">
         <bean class="com.hhb.dao.OrderDao"/>
     </property>
 </bean>
  • 这种方式作为了解。

4.3.3注入简单类型

第一步:定义User类,提供setter方法

package com.hhb.bean;
​
public class User {
    private String username; // String是简单类型
    private String password;
    private int age; // int是简单类型
​
    public void setUsername(String username) {
        this.username = username;
    }
​
    public void setPassword(String password) {
        this.password = password;
    }
​
    public void setAge(int age) {
        this.age = age;
    }
​
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

第二步:set-di.xml

<bean id="userBean" class="com.hhb.bean.User">
    <!--重点:如果给简单类型赋值,需要使用value-->
    <property name="username" value="张三"/>
    <property name="password" value="123"/>
    <property name="age" value="19"/>
</bean>

第三步:测试

    @Test
    public void testSimpleTypeSet() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        User user = applicationContext.getBean("userBean", User.class);
        System.out.println(user);
    }
  • 简单类型包括:

    • 基本数据类型

    • 基本数据类型对应的包装类

    • String或其他的CharSequence子类

    • Number子类

    • Date子类

    • Enum子类

    • URL

    • URI

    • Temporal子类

    • Locale

    • Class

    • 以上简单值类型对应的数组类型

SimpleValueType

package com.powernode.spring6.beans;
import java.net.URI;
import java.net.URL;
import java.time.LocalDate;
import java.util.Date;
import java.util.Locale;
public class A {
 private byte b;
 private short s;
 private int i;
 private long l;
 private float f;
 private double d;
 private boolean flag;
 private char c;
 private Byte b1;
 private Short s1;
 private Integer i1;
 private Long l1;
 private Float f1;
 private Double d1;
 private Boolean flag1;
 private Character c1;
 private String str;
 private Date date;
 private Season season;
 private URI uri;
 private URL url;
 private LocalDate localDate;
 private Locale locale;
 private Class clazz;
 
 // ⽣成setter⽅法
 // ⽣成toString⽅法
}
enum Season {
 SPRING, SUMMER, AUTUMN, WINTER
}
  • 如果把Date当做简单类型的话,日期字符串格式不能随便写。必须按照这种格式写:Wed Oct 19 16:28:13 CST 2022

经典案例:给数据源的属性注入值

MyDataSource

package com.hhb.jdbc;
​
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
​
public class MyDataSource implements DataSource {
    private String driver;
    private String url;
    private String username;
    private String password;
​
    public void setDriver(String driver) {
        this.driver = driver;
    }
​
    public void setUrl(String url) {
        this.url = url;
    }
​
    public void setUsername(String username) {
        this.username = username;
    }
​
    public void setPassword(String password) {
        this.password = password;
    }
​
    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
​
    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }
​
    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }
​
    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }
​
    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
​
    }
​
    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
​
    }
​
    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }
​
    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
​
    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }
​
    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}

set-di.xml

<bean id="mds" class="com.hhb.jdbc.MyDataSource">
        <property name="username" value="root"/>
        <property name="password" value="123123"/>
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
    </bean>
4.3.4级联属性赋值

Student

package com.hhb.bean;
​
public class Student {
    private String name;
​
    //学生属于哪个班级
    private Clazz clazz;
​
    public void setName(String name) {
        this.name = name;
    }
​
    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }
​
    //使用级联属性赋值,需要get方法
    public Clazz getClazz() {
        return clazz;
    }
​
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", clazz=" + clazz +
                '}';
    }
}

Clazz

package com.hhb.bean;
​
public class Clazz {
    private String name;
​
    public void setName(String name) {
        this.name = name;
    }
​
    @Override
    public String toString() {
        return "Clazz{" +
                "name='" + name + '\'' +
                '}';
    }
}

cascade.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">
​
    <!--使用级联属性赋值需要注意:
        配置的顺序不能颠倒,必须以如下顺序
        clazz属性必须提供getter方法
    -->
    <bean id="studentBean" class="com.hhb.bean.Student">
        <!--简单类型使用value-->
        <property name="name" value="张三"/>
        <!--这不是简单类型,使用ref-->
        <property name="clazz" ref="clazzBean"/>
        <!--级联属性赋值-->
        <property name="clazz.name" value="大二"/>
    </bean>
​
    <bean id="clazzBean" class="com.hhb.bean.Clazz"/>
</beans>
4.3.5注入数组

Array1

package com.hhb.bean;
​
import java.util.Arrays;
​
public class Array1 {
    private String[] hobbies;
​
    private Array2[] array2;
​
    public void setArray2(Array2[] array2) {
        this.array2 = array2;
    }
​
    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }
​
    @Override
    public String toString() {
        return "Array1{" +
                "hobbies=" + Arrays.toString(hobbies) +
                ", array2=" + Arrays.toString(array2) +
                '}';
    }
}

Array2

package com.hhb.bean;
​
public class Array2 {
    private String name;
​
    public void setName(String name) {
        this.name = name;
    }
​
    @Override
    public String toString() {
        return "Array2{" +
                "name='" + name + '\'' +
                '}';
    }
}

spring-array.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="w1" class="com.hhb.bean.Array2">
        <property name="name" value="小红"/>
    </bean>
    <bean id="w2" class="com.hhb.bean.Array2">
        <property name="name" value="小花"/>
    </bean>
    <bean id="w3" class="com.hhb.bean.Array2">
        <property name="name" value="小丽"/>
    </bean>
        
    <bean id="array1" class="com.hhb.bean.Array1">
        <!--简单类型-->
        <property name="hobbies">
            <array>
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </array>
        </property>
        <!--非简单类型-->
        <property name="array2">
            <array>
                <ref bean="w1"/>
                <ref bean="w2"/>
                <ref bean="w3"/>
            </array>
        </property>
    </bean>
</beans>
4.3.6注入List、Set、Map集合

Person

package com.hhb.bean;
​
import java.util.List;
import java.util.Map;
import java.util.Set;
​
public class Person {
    private List<String> names;
    private Set<String> phones;
    private Map<Integer, String> adrrs;
​
    public void setNames(List<String> names) {
        this.names = names;
    }
​
    public void setPhones(Set<String> phones) {
        this.phones = phones;
    }
​
    public void setAdrrs(Map<Integer, String> adrrs) {
        this.adrrs = adrrs;
    }
​
    @Override
    public String toString() {
        return "Person{" +
                "names=" + names +
                ", phones=" + phones +
                ", adrrs=" + adrrs +
                '}';
    }
}
​

spring-collection.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="personBean" class="com.hhb.bean.Person">
        <property name="names">
            <!--list集合有序可重复-->
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
                <value>张三</value>
            </list>
        </property>
        <property name="phones">
            <!--set集合无须不可重复-->
            <set>
                <value>110</value>
                <value>120</value>
                <value>110</value>
            </set>
        </property>
        <property name="adrrs">
            <!--注入Map集合-->
            <map>
                <!--如果key和value不是简单类型,使用:<entry key-ref="" value-ref=""/>-->
                <!--简单类型-->
                <entry key="1" value="北京"/>
                <entry key="2" value="南京"/>
            </map>
        </property>
    </bean>
</beans>
4.3.7注入Properties

Property

package com.hhb.bean;
​
import java.util.Properties;
​
public class Property {
    private Properties properties;
​
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
​
    @Override
    public String toString() {
        return "Property{" +
                "properties=" + properties +
                '}';
    }
}
  • Properties本质上也是一个Map集合。

  • Properties的父类Hashtable实现了Map接口。

  • Properties的key和value只能是String类型。

spring-properties.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="propertiesBean" class="com.hhb.bean.Property">
        <property name="properties">
            <props>
                <prop key="driver">com.mysql.cj.jdbc.Driver</prop>
                <prop key="url">jdbc:mysql://localhost:3306/spring6</prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>
</beans>
4.3.8注入null和空字符串
<bean id="catBean" class="com.hhb.bean.Cat">
    <!--不给属性注入,默认的属性值就是null-->
    <!--<property name="name" value="tom"/>-->
    <!--这种方式是手动注入null-->
    <property name="name">
        <null/>
    </property>
    <!--注入空字符串第一种方式-->
    <!--property name="name" value=""/>-->
    <!--注入空字符串第二种方式-->
    <!--<property name="name">
        <value/>
    </property>-->
    <property name="age" value="3"/>
</bean>
4.3.9注入的值中含有特殊符号
  • XML中有5个特殊字符,分别是:<、>、'、"、&

  • 以上5个特殊符号在XML中会被特殊对待,会被当做XML语法的一部分进行解析,如果这些特殊符号直接出现在注入的字符串当中,会报错。

    特殊字符 转义字符
    > >
    < <
    ' '
    " "
    & &
<bean id="mathBean" class="com.hhb.bean.Math">
    <!--使用实体符号代替特殊符号-->
    <!--<property name="result" value="2&lt;3"/>-->
    <property name="result">
        <!--使用<![CDATA[]],只能在value标签中使用-->
        <value><![CDATA[2<3]]></value>
    </property>
</bean>

4.4p命名空间注入

  • 目的:简化配置

Dog

package com.hhb.bean;
​
import java.util.Date;
​
public class Dog {
    // 简单类型
    private String name;
    private int age;
    // 非简单类型
    private Date birth;
​
    // p命名空间注入底层还是set注入,只不过p命名空间注入可以让spring配置变的更加简单。
    public void setName(String name) {
        this.name = name;
    }
​
    public void setAge(int age) {
        this.age = age;
    }
​
    public void setBirth(Date birth) {
        this.birth = birth;
    }
​
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                '}';
    }
}

spring-p.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--在spring配置文件头部添加p命名空间。xmlns:p="http://www.springframework.org/schema/p"-->
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
​
    <!--使用 p:属性名="属性值"-->
    <bean id="dogBean" class="com.hhb.bean.Dog" p:name="tom" p:age="3" p:birth-ref="birthBean"/>
​
    <bean id="birthBean" class="java.util.Date"/>
</beans>

4.5c命名空间注入

  • c命名空间是简化构造方法注入的。

People

package com.hhb.bean;
​
public class People {
    private String name;
    private String age;
    private Boolean flag;
​
    //c命名空间是简化构造注入的
    //c命名空间注入办法是基于构造方法的
    public People(String name, String age, Boolean flag) {
        this.name = name;
        this.age = age;
        this.flag = flag;
    }
​
    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", flag=" + flag +
                '}';
    }
}

spring-c.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--在spring的配置文件头部添加:xmlns:c="http://www.springframework.org/schema/c"-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
​
    <!--下标方式-->
    <!--<bean id="peopleBean" class="com.hhb.bean.People" c:_0="张三" c:_1="25" c:_2="true"/>-->
    <!--参数名方式-->
    <bean id="peopleBean" class="com.hhb.bean.People" c:name="张三" c:age="25" c:flag="true"/>
</beans>

4.6util命名空间

  • 使用util命名空间可以让配置复用。

MyDataSource1

package com.hhb.jdbc;
​
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
​
public class MyDataSource1 implements DataSource {
    private Properties properties;
​
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
​
    @Override
    public String toString() {
        return "MyDataSource1{" +
                "properties=" + properties +
                '}';
    }
​
    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }
​
    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }
​
    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }
​
    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
​
    }
​
    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
​
    }
​
    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }
​
    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
​
    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }
​
    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}

spring-util.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--引入命名空间
    在spring的配置文件头部添加:
    xmlns:util="http://www.springframework.org/schema/util"
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
​
    <util:properties id="ds">
        <prop key="driver">com.mysql.cj.jdbc.driver</prop>
        <prop key="url">jdbc:mysql://localhost:3306/spring</prop>
        <prop key="username">root</prop>
        <prop key="password">123</prop>
    </util:properties>
​
    <!--数据源1-->
    <bean id="ds1" class="com.hhb.jdbc.MyDataSource1">
        <property name="properties" ref="ds"/>
    </bean>
​
    <!--数据源2-->
    <bean id="ds2" class="com.hhb.jdbc.MyDataSource2">
        <property name="properties" ref="ds"/>
    </bean>
​
</beans>

4.7基于XML的自动装配

  • 都是基于set方法的。

4.7.1根据名称自动装配
<!--根据名字自动装配-->
    <bean id="orderServiceBean" class="com.hhb.service.OrderService" autowire="byName"/>
​
    <!--id一般也叫做bean的名称-->
    <!--bean的id不能随意写,set方法名去掉set,剩下的单词首字母小写-->
    <bean id="orderDao" class="com.hhb.dao.OrderDao"/>
4.7.2根据类型自动装配
<!--根据类型进行自动装配-->
<!--根据类型进行自动装配的时候,在有效的配置文件中,某种类型的使用只能有一个-->
<bean class="com.hhb.dao.VipDao"/>
<bean class="com.hhb.dao.UserDao"/>
<bean id="cs" class="com.hhb.service.CustomerService" autowire="byType"/>

4.8Spring引入外部属性配置文件

MyDataSource

package com.hhb.jdbc;
​
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
​
public class MyDataSource implements DataSource {
    private String driver;
    private String url;
    private String username;
    private String password;
​
    public void setDriver(String driver) {
        this.driver = driver;
    }
​
    public void setUrl(String url) {
        this.url = url;
    }
​
    public void setUsername(String username) {
        this.username = username;
    }
​
    public void setPassword(String password) {
        this.password = password;
    }
​
    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
​
    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }
​
    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }
​
    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }
​
    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
​
    }
​
    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
​
    }
​
    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }
​
    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
​
    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }
​
    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}

jdbc.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring6
jdbc.username=root
jdbc.password=123

spring-properties.xml文章来源地址https://www.toymoban.com/news/detail-575598.html

<?xml version="1.0" encoding="UTF-8"?>
<!--引入context命名空间-->
<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">
​
    <!--使用context:property-placeholder的location属性来指定属性配置文件的路径。
        location默认从类的根路径下开始加载资源-->
    <context:property-placeholder location="jdbc.properties"/>
​
    <!--配置数据源-->
    <bean id="ds" class="com.hhb.jdbc.MyDataSource">
        <property name="driver" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>

到了这里,关于[SSM]Spring对IoC的实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • spring框架,以及和spring框架相关的Java面试题和spring ioc的注入方式

    目录 一.spring来源,以及介绍 1.spring诞生的背景 2.spring框架 介绍 3.spring框架在使用中的优点以及不足 3.1优点  3.2不足 3.3总结 4.为什么要使用spring  二.将spring框架部署在IDEA中  1.替换pom.xml  2.构建spring所需要的xml文件 三.spring的三种注入方式 0.定义需要的类,方法 1.方法注入

    2024年02月12日
    浏览(37)
  • 【Java面试】Spring中的IOC和AOP

    IOC:控制反转也叫依赖注入。利用了工厂模式 将对象交给容器管理,你只需要在spring配置文件总配置相应的bean,以及设置相关的属性,让spring容器来生成类的实例对象以及管理对象。在spring容器启动的时候,spring会把你在配置文件中配置的bean都初始化好,然后在你需要调用的

    2024年02月10日
    浏览(36)
  • 【Java EE】Spring核心思想(一)——IOC

    通过前⾯的学习, 我们知道了Spring是⼀个开源框架, 他让我们的开发更加简单. 他⽀持⼴泛的应⽤场 景, 有着活跃⽽庞⼤的社区, 这也是Spring能够⻓久不衰的原因. 但是这个概念相对来说, 还是⽐较抽象. 我们⽤⼀句更具体的话来概括Spring, 那就是: Spring 是包含了众多⼯具⽅法的

    2024年04月23日
    浏览(39)
  • Spring IOC底层原理实现过程

    Spring IOC(Inversion of Control,控制反转)是 Spring 框架的核心,它的实现原理是基于反射和配置文件实现的。 在 Spring IOC 中,控制权被反转了。传统的应用程序开发中,对象的创建、管理、销毁等全部由应用程序自己来控制,这个过程称为主动管理(Active Management)。而在 Spr

    2024年02月03日
    浏览(32)
  • IOC使用Spring实现附实例详解

    目录 一、相关导读 1. Maven系列专栏文章 2. Mybatis系列专栏文章 3. Spring系列专栏文章 二、前言 Spring简介 Spring体系结构 三、Spring实现IOC 1. 创建Maven工程,引入对应依赖 2. 创建实体类,Dao接口及实现类 3. 编写xml配置文件 4. 测试从Spring容器获取对象 5. 测试结果 四、Spring容器类型

    2023年04月24日
    浏览(31)
  • 【Java学习】 Spring的基础理解 IOC、AOP以及事务

        官网: https://spring.io/projects/spring-framework#overview     官方下载工具: https://repo.spring.io/release/org/springframework/spring/     github下载: https://github.com/spring-projects/spring-framework     maven依赖: 1.spring全家桶的结构构图:              最下边的是测试单元   其中spring封装

    2024年02月09日
    浏览(29)
  • spring6-实现简易版IOC容器

    我们都知道,Spring框架的IOC是基于Java反射机制实现的,下面我们先回顾一下java反射。 1、回顾Java反射 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调

    2024年02月08日
    浏览(30)
  • Spring IOC容器:让Java对象的管理和配置更简单

    在Java开发中,我们经常需要创建和使用各种Java对象,例如实体类,服务类,控制器类等。这些对象之间通常存在着一定的依赖关系,例如一个服务类可能需要调用另一个服务类或一个数据访问类的方法。为了创建和使用这些对象,我们通常需要做以下几件事: 在代码中通过

    2024年02月11日
    浏览(29)
  • javaee spring 用注解的方式实现ioc

    spring核心依赖 spring配置文件

    2024年02月10日
    浏览(35)
  • Spring Framework的核心:IoC容器的实现(1)

    个人名片: 🐼 作者简介:一名大二在校生 🐻‍❄️ 个人主页:落798. 🐼 个人WeChat:落798. 🕊️ 系列专栏: 零基础学java ----- 重识c语言 ---- 计算机网络 — 【Spring技术内幕】 🐓 每日一句: 看淡一点在努力,你吃的苦会铺成你要的路! Spring 容器是 Spring 框架的核心。 容

    2024年02月15日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包