spring(1):基于XML获取Bean对象以及各种依赖注入方式

这篇具有很好参考价值的文章主要介绍了spring(1):基于XML获取Bean对象以及各种依赖注入方式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 获取Bean

XML文件:
<bean id="helloworld" class="org.kkk.spring6.bean.HelloWorld"></bean>

1.1 根据id获取

@Test
public void testHelloWorld(){
    //加载XML文件
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
	//根据id获取Bean对象
    HelloWorld bean = context.getBean("helloworld");
    //调用该对象的方法
    bean.sayHello();
}

1.2 根据类型获取

@Test
public void testHelloWorld(){
    //加载XML文件
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
	//根据类型获取Bean对象
    HelloWorld bean = context.getBean(helloworld.class);
    //调用该对象的方法
    bean.sayHello();
}

1.3 根据id和类型获取

@Test
public void testHelloWorld(){
    //加载XML文件
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
	//根据类型获取Bean对象
    HelloWorld bean = context.getBean("helloworld", helloworld.class);
    //调用该对象的方法
    bean.sayHello();
}

注意:

  • 当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个。

    例如以下XML文件,当IOC容器中一共配置了两个,根据类型获取时会抛出异常。

<bean id="helloworldOne" class="org.kkk.spring6.bean.HelloWorld"></bean>
<bean id="helloworldTwo" class="org.kkk.spring6.bean.HelloWorld"></bean>
  • 根据类型来获取bean时,在满足bean唯一性的前提下,其实只是看:『对象 instanceof 指定的类型』的返回结果,只要返回的是true就可以认定为和类型匹配,能够获取到。

    因此,如果一个接口有多个实现类,这些实现类都配置了 bean,根据接口类型就不可以获取到 bean ,因为不满足唯一性。而如果这个接口只有一个实现类,那就可以获取到。

2. 依赖注入

个人理解:依赖注入就是为对象的属性赋值

2.1 setter注入

通过组件类的setXxx()方法给组件对象设置属性

①创建学生类Student

package org.kkk.spring6.bean;

public class Student {

    private Integer id;

    private String name;

    private Integer age;

    private String sex;

    public Student() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

}

②配置bean时为属性赋值

spring-di.xml

<bean id="student" class="org.kkk.spring6.bean.Student">
    <!-- property标签:通过组件类的setXxx()方法给组件对象设置属性 -->
    <!-- name属性:指定属性名(这个属性名是getXxx()、setXxx()方法定义的,和成员变量无关) -->
    <!-- value属性:指定属性值 -->
    <property name="id" value="1001"></property>
    <property name="name" value="张三"></property>
    <property name="age" value="23"></property>
    <property name="sex" value=""></property>
</bean>

③测试

@Test
public void testDIBySet(){
    ApplicationContext context= new ClassPathXmlApplicationContext("spring-di.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
}

2.2 构造器注入

通过组件类的有参构造方法给组件对象设置属性

①在Student类中添加有参构造

public Student(Integer id, String name, Integer age, String sex) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.sex = sex;
}

②配置bean

spring-di.xml

<bean id="student" class="org.kkk.spring6.bean.Student">
    <constructor-arg value="1002"></constructor-arg>
    <constructor-arg value="李四"></constructor-arg>
    <constructor-arg value="33"></constructor-arg>
    <constructor-arg value=""></constructor-arg>
</bean>

注意:

constructor-arg标签还有两个属性可以进一步描述构造器参数:

  • index属性:指定参数所在位置的索引(从0开始)
  • name属性:指定参数名

③测试

@Test
public void testDIByConstructor(){
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-di.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
}

2.3 特殊值处理

在为属性进行赋值时,可能会出现一些特殊情况要特殊处理。

①null值
<property name="name">
    <null />
</property>

注意:

<property name="name" value="null"></property>

以上写法,为name所赋的值是字符串null

②xml实体
<!-- 小于号在XML文档中用来定义标签的开始,不能随便使用 -->
<!-- 解决方案一:使用XML实体来代替 -->
<property name="expression" value="a &lt; b"/>
③CDATA节
<property name="expression">
    <!-- 解决方案二:使用CDATA节 -->
    <!-- CDATA中的C代表Character,是文本、字符的含义,CDATA就表示纯文本数据 -->
    <!-- XML解析器看到CDATA节就知道这里是纯文本,就不会当作XML标签或属性来解析 -->
    <!-- 所以CDATA节中写什么符号都随意 -->
    <value><![CDATA[a < b]]></value>
</property>

2.4 为对象类型属性赋值

当某一个类存在类型为另外一个类的属性时,就不能按照上述一般情况对这个属性赋值。例如,现在有学生类和班级类,为了表示学生和班级的关系,学生类中有一个班级类的属性。

代码表示如下:

班级类Clazz

package org.kkk.spring6.bean
    
public class Clazz {

    private Integer clazzId;

    private String clazzName;

    public Integer getClazzId() {
        return clazzId;
    }

    public void setClazzId(Integer clazzId) {
        this.clazzId = clazzId;
    }

    public String getClazzName() {
        return clazzName;
    }

    public void setClazzName(String clazzName) {
        this.clazzName = clazzName;
    }

    @Override
    public String toString() {
        return "Clazz{" +
                "clazzId=" + clazzId +
                ", clazzName='" + clazzName + '\'' +
                '}';
    }

    public Clazz() {
    }

    public Clazz(Integer clazzId, String clazzName) {
        this.clazzId = clazzId;
        this.clazzName = clazzName;
    }
}

Student类

在Student类中添加以下代码:

private Clazz clazz;

public Clazz getClazz() {
	return clazz;
}

public void setClazz(Clazz clazz) {
	this.clazz = clazz;
}

可以看到,学生类中有一个属性为clazz,其类型为班级类。我们对clazz进行属性赋值,有以下几种方法。

外部bean

配置Clazz类型的bean:

<bean id="clazz" class="org.kkk.spring6.bean.Clazz">
    <property name="clazzId" value="1001"></property>
    <property name="clazzName" value="c++开发培训班"></property>
</bean>

为Student中的clazz属性赋值:

<bean id="student" class="org.kkk.spring6.bean.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value=""></property>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazz"></property>
</bean>

如果错把ref属性写成了value属性,会抛出异常: Caused by: java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘org.kkk.spring6.bean.Clazz’ for property ‘clazz’: no matching editors or conversion strategy found

意思是不能把String类型转换成我们要的Clazz类型,说明我们使用value属性时,Spring只把这个属性看做一个普通的字符串,不会认为这是一个bean的id,更不会根据它去找到bean来赋值

内部bean

<bean id="student" class="org.kkk.spring6.bean.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value=""></property>
    <property name="clazz">
        <!-- 在一个bean中再声明一个bean就是内部bean -->
        <!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
        <bean id="clazzInner" class="org.kkk.spring6.bean.Clazz">
            <property name="clazzId" value="1001"></property>
            <property name="clazzName" value="c++开发培训班"></property>
        </bean>
    </property>
</bean>

级联属性赋值

<bean id="student" class="org.kkk.spring6.bean.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value=""></property>
    <property name="clazz" ref="clazzOne"></property>
    <property name="clazz.clazzId" value="1001"></property>
    <property name="clazz.clazzName" value="c++开发培训班"></property>
</bean>

2.5 为数组类型属性赋值

①修改Student类

在Student类中添加以下代码:

private String[] hobbies;

public String[] getHobbies() {
    return hobbies;
}

public void setHobbies(String[] hobbies) {
    this.hobbies = hobbies;
}

②配置bean

<bean id="student" class="org.kkk.spring.bean6.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value=""></property>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazz"></property>
    <property name="hobbies">
        <array>
            <value>吃饭</value>
            <value>睡觉</value>
            <value>写代码</value>
        </array>
    </property>
</bean>

2.6 为集合类型属性赋值

①为List集合类型属性赋值

在Clazz类中添加以下代码:

private List<Student> students;

public List<Student> getStudents() {
    return students;
}

public void setStudents(List<Student> students) {
    this.students = students;
}

配置bean:

<bean id="clazz" class="org.kkk.spring6.bean.Clazz">
    <property name="clazzId" value="1002"></property>
    <property name="clazzName" value="Java开发培训班"></property>
    <property name="students">
        <list>
            <ref bean="studentOne"></ref>
            <ref bean="studentTwo"></ref>
            <ref bean="studentThree"></ref>
        </list>
    </property>
</bean>

若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可

②为Map集合类型属性赋值

创建教师类Teacher:

package org.kkk.spring6.bean;
public class Teacher {

    private Integer teacherId;

    private String teacherName;

    public Integer getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Integer teacherId) {
        this.teacherId = teacherId;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public Teacher(Integer teacherId, String teacherName) {
        this.teacherId = teacherId;
        this.teacherName = teacherName;
    }

    public Teacher() {

    }
    
    @Override
    public String toString() {
        return "Teacher{" +
                "teacherId=" + teacherId +
                ", teacherName='" + teacherName + '\'' +
                '}';
    }
}

在Student类中添加以下代码:

private Map<String, Teacher> teacherMap;

public Map<String, Teacher> getTeacherMap() {
    return teacherMap;
}

public void setTeacherMap(Map<String, Teacher> teacherMap) {
    this.teacherMap = teacherMap;
}

配置bean:

<bean id="teacherOne" class="org.kkk.spring6.bean.Teacher">
    <property name="teacherId" value="1005"></property>
    <property name="teacherName" value="大宝"></property>
</bean>

<bean id="teacherTwo" class="org.kkk.spring6.bean.Teacher">
    <property name="teacherId" value="1006"></property>
    <property name="teacherName" value="二宝"></property>
</bean>

<bean id="studentFour" class="org.kkk.spring6.bean.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value=""></property>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazzOne"></property>
    <property name="hobbies">
        <array>
            <value>吃饭</value>
            <value>睡觉</value>
            <value>写代码</value>
        </array>
    </property>
    <property name="teacherMap">
        <map>
            <entry>
                <key>
                    <value>1005</value>
                </key>
                <ref bean="teacherOne"></ref>
            </entry>
            <entry>
                <key>
                    <value>1006</value>
                </key>
                <ref bean="teacherTwo"></ref>
            </entry>
        </map>
    </property>
</bean>
③引用集合类型的bean
<!--list集合类型的bean-->
<util:list id="students">
    <ref bean="studentOne"></ref>
    <ref bean="studentTwo"></ref>
    <ref bean="studentThree"></ref>
</util:list>
<!--map集合类型的bean-->
<util:map id="teacherMap">
    <entry>
        <key>
            <value>1005</value>
        </key>
        <ref bean="teacherOne"></ref>
    </entry>
    <entry>
        <key>
            <value>1006</value>
        </key>
        <ref bean="teacherTwo"></ref>
    </entry>
</util:map>
<bean id="clazzTwo" class="org.kkk.spring6.bean.Clazz">
    <property name="clazzId" value="1002"></property>
    <property name="clazzName" value="Java开发培训班"></property>
    <property name="students" ref="students"></property>
</bean>
<bean id="studentFour" class="org.kkk.spring6.bean.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value=""></property>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazzOne"></property>
    <property name="hobbies">
        <array>
            <value>吃饭</value>
            <value>睡觉</value>
            <value>写代码</value>
        </array>
    </property>
    <property name="teacherMap" ref="teacherMap"></property>
</bean>

使用util:list、util:map标签必须引入相应的命名空间

<?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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

2.7 p命名空间

引入p命名空间

<?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:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

引入p命名空间后,可以通过以下方式为bean的各个属性赋值

<bean id="student" class="org.kkk.spring6.bean.Student"
    p:id="1006" p:name="小明" p:clazz-ref="clazzOne" p:teacherMap-ref="teacherMap"></bean>

2.8 引入外部属性文件

①加入依赖

 <!-- MySQL驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.30</version>
</dependency>

<!-- 数据源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.15</version>
</dependency>

②创建外部属性文件

文件jdbc.properties保存数据库连接信息

jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver

③引入属性文件

引入context 名称空间

<?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">

</beans>
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

注意:在使用 context:property-placeholder 元素加载外包配置文件功能前,首先需要在 XML 配置的一级标签 中添加 context 相关的约束。

④配置bean

<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="url" value="${jdbc.url}"/>
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="username" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

⑤测试

@Test
public void testDataSource() throws SQLException {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-datasource.xml");
    DataSource dataSource = context.getBean(DataSource.class);
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
}

2.9 bean的作用域

①概念

在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围,各取值含义参加下表:

取值 含义 创建对象的时机
singleton(默认) 在IOC容器中,这个bean的对象始终为单实例 IOC容器初始化时
prototype 这个bean在IOC容器中有多个实例 获取bean时

如果是在WebApplicationContext环境下还会有另外几个作用域(但不常用):

取值 含义
request 在一个请求范围内有效
session 在一个会话范围内有效

②创建类User

package org.kkk.spring6.bean;
public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    public User() {
    }

    public User(Integer id, String username, String password, Integer age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

③配置bean

<!-- scope属性:取值singleton(默认值),bean在IOC容器中只有一个实例,IOC容器初始化时创建对象 -->
<!-- scope属性:取值prototype,bean在IOC容器中可以有多个实例,getBean()时创建对象 -->
<bean class="org.kkk.spring6.bean.User" scope="prototype"></bean>

④测试

@Test
public void testBeanScope(){
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-scope.xml");
    User user1 = context.getBean(User.class);
    User user2 = context.getBean(User.class);
    System.out.println(user1==user2);
}

如果将scope属性设置为singleton(单例)时,那么user1与user2的地址相同,两者实质上是同一个实例对象。如果scope属性为prototype(多例),那么user1与user2的地址不同。

2.10 基于xml自动装配

自动装配:

根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类型属性赋值

①场景模拟

创建类UserController

package org.kkk.spring6.autowire.controller
public class UserController {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void saveUser(){
        userService.saveUser();
    }

}

创建接口UserService

package org.kkk.spring6.autowire.service
public interface UserService {

    void saveUser();

}

创建类UserServiceImpl实现接口UserService

package org.kkk.spring6.autowire.service.impl
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void saveUser() {
        userDao.saveUser();
    }

}

创建接口UserDao

package org.kkk.spring6.autowire.dao
public interface UserDao {

    void saveUser();

}

创建类UserDaoImpl实现接口UserDao

package org.kkk.spring6.autowire.dao.impl
public class UserDaoImpl implements UserDao {

    @Override
    public void saveUser() {
        System.out.println("保存成功");
    }

}

②配置bean

使用bean标签的autowire属性设置自动装配效果

自动装配方式:byType

byType:根据类型匹配IOC容器中的某个兼容类型的bean,为属性自动赋值

若在IOC中,没有任何一个兼容类型的bean能够为属性赋值,则该属性不装配,即值为默认值null

若在IOC中,有多个兼容类型的bean能够为属性赋值,则抛出异常NoUniqueBeanDefinitionException

<bean id="userController" class="org.kkk.spring6.autowire.controller.UserController" autowire="byType"></bean>

<bean id="userService" class="org.kkk.spring6.autowire.service.impl.UserServiceImpl" autowire="byType"></bean>

<bean id="userDao" class="org.kkk.spring6.autowire.dao.impl.UserDaoImpl"></bean>

自动装配方式:byName

byName:将自动装配的属性的属性名,作为bean的id在IOC容器中匹配相对应的bean进行赋值

<bean id="userController" class="org.kkk.spring6.autowire.controller.UserController" autowire="byName"></bean>

<bean id="userService" class="org.kkk.spring6.autowire.service.impl.UserServiceImpl" autowire="byName"></bean>
<bean id="userServiceImpl" class="org.kkk.spring6.autowire.service.impl.UserServiceImpl" autowire="byName"></bean>

<bean id="userDao" class="org.kkk.spring6.autowire.dao.impl.UserDaoImpl"></bean>
<bean id="userDaoImpl" class="org.kkk.spring6.autowire.dao.impl.UserDaoImpl"></bean>

③测试文章来源地址https://www.toymoban.com/news/detail-823277.html

@Test
public void testAutoWireByXML(){
    ApplicationContext context = new ClassPathXmlApplicationContext("autowire-xml.xml");
    UserController userController = context.getBean(UserController.class);
    userController.saveUser();
}

到了这里,关于spring(1):基于XML获取Bean对象以及各种依赖注入方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • Spring——更快捷的存储 / 获取Bean对象

    本人是一个普通程序猿!分享一点自己的见解,如果有错误的地方欢迎各位大佬莅临指导,如果你也对编程感兴趣的话,互关一下,以后互相学习,共同进步。这篇文章能够帮助到你的话,劳请大家点赞转发支持一下! 上篇文章中,向Spring容器中添加对象,还要去配置文件里手动添

    2024年02月15日
    浏览(32)
  • Spring IOC基于XML和注解管理Bean(一)

    Spring IOC基于XML和注解管理Bean(二) IoC 是 Inversion of Control 的简写,译为“ 控制反转 ”,它不是一门技术,而是一种设计思想,是一个重要的面向对象编程法则,能够指导我们如何设计出 松耦合 、更优良的程序。 Spring 通过 IoC 容器来管理所有 Java 对象的实例化和初始化,控

    2024年02月08日
    浏览(64)
  • Spring IOC基于XML和注解管理Bean(二)

    Spring IOC基于XML和注解管理Bean(一) 2.9、实验八:p命名空间 引入p命名空间 引入p命名空间后,可以通过以下方式为bean的各个属性赋值 2.10、实验九:引入外部属性文件 ①加入依赖 ②创建外部属性文件 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    2024年02月09日
    浏览(34)
  • 【JavaEE】Spring中注解的方式去获取Bean对象

    【JavaEE】Spring的开发要点总结(3) 在前面的代码里,我们获取Bean对象也比较麻烦: 本文章就是为了更方便地去获取Bean对象~ 对象装配 也叫 对象注入 那么有没有对应的注解去实现这个功能呢? Spring提供的三种实现方法: 属性注入 构造方法注入 Setter注入 而这种非明文获取

    2024年02月15日
    浏览(30)
  • Spring6学习技术|IoC+基于xml管理bean

    尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解) 控制反转。是一种设计思想。 通过id,通过class,和双重方式。 普通属性:String, Interger (set和构造器:感觉还是set比较方便) 特殊属性:null,特殊的大于小于等(xml转义字符,cdata) 对象:外部

    2024年02月21日
    浏览(37)
  • spring boot applicationContext.getBeansOfType 无法获取所有bean对象

     代码如上所示,我想在某个service中注入所有AvatarScanCallback类型bean对象,但是发现无法注入全部bean, 最后检查发现是因为有些AvatarScanCallback的子对象中存在循环依赖问题导致此时只能获取一部分。  

    2024年02月17日
    浏览(33)
  • 【Spring进阶系列丨第四篇】学习Spring中的Bean管理(基于xml配置)

    在之前的学习中我们知道,容器是一个空间的概念,一般理解为可盛放物体的地方。在Spring容器通常理解为BeanFactory或者ApplicationContext。我们知道spring的IOC容器能够帮我们创建对象,对象交给spring管理之后我们就不用手动去new对象。 那么Spring是如何管理Bean的呢? 简而言之,

    2024年02月05日
    浏览(59)
  • Spring依赖注入之setter注入与构造器注入以及applicationContext.xml配置文件特殊值处理

    依赖注入之setter注入 在管理bean对象的组件的时候同时给他赋值,就是setter注入,通过setter注入,可以将某些依赖项标记为可选的,因为它们不是在构造对象时立即需要的。这种方式可以减少构造函数的参数数量,使得类的构造函数更加简洁。 注:既然是setter注入,则对象的

    2024年01月25日
    浏览(36)
  • 【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取

    Spring的开发要点总结 Spring的初步了解博客:【JavaEE】JavaEE进阶:框架的学习 - Spring的初步认识_s:103的博客-CSDN博客 就不带大家回顾了~ 从框架获取的对象称为获取【Bean对象】! Java是咖啡,Bean就是\\\"咖啡豆\\\",也就是“对象” Spring项目开发就是 开业 , 放咖啡豆到罐子里 , 后

    2024年02月16日
    浏览(31)
  • 一文详解Spring Bean循环依赖

    一、背景 有好几次线上发布老应用时,遭遇代码启动报错,具体错误如下: 眨眼一看,这不就是Spring Bean循环依赖报错吗?脑海立马闪过那些年为了进阿里面试时被死亡N连问的场景,那时我们都知道Spring已经支持bean循环依赖,为啥我们的Springboot应用启动时还报这个错误?带

    2024年02月15日
    浏览(23)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包