Spring-IOC创建对象方式

本文探讨了如何在Spring Boot中使用无参构造方法创建对象,并通过`beans.xml`配置文件进行实例化。接着介绍了有参构造方法的配置方式,包括下标、参数名和类型。还涵盖了Spring配置中的alias和bean的详细配置,以及多文件导入和重复资源合并。重点在于理解对象初始化与配置文件的关系。

通过无参构造方法来创建对象

@Data
public class Hello {
    private String name;
    public Hello(){
        System.out.println("hello无参构造方法");
    }
    public void show(){
        System.out.println("Hello,"+ name );
    }
}
 @Test
    public void test(){
        //解析beans.xml文件 , 生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean : 参数即为spring配置文件中bean的id .
        Hello hello = (Hello) context.getBean("hello");
        hello.show();
    }

结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!

 

通过有参构造方法来创建

 

@Data
@AllArgsConstructor

public class UserT {
    private String name;
    private int age;
    public void show(){
        System.out.println("name = " + name);
        System.out.println("age = " + age);
    }
}

beans.xml 有三种方式编写

第一种:根据index参数下标设置

<?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">
    
    
    <!-- 第一种根据index参数下标设置 -->
    <bean id="userT" class="com.yao.pojo.UserT">
        <!-- index指构造方法 , 下标从0开始 -->
        <constructor-arg index="0" value="kuangshen2"/>
        <constructor-arg index="1" value="18"/>
    </bean>

</beans>

第二种:根据参数名字设置

<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- name指参数名 -->
   <constructor-arg name="name" value="kuangshen2"/>
   <constructor-arg name="age" value="11"/>
</bean>

第三种:根据参数类型设置

    <!-- 第三种根据参数类型设置 -->
    <bean id="userT" class="com.yao.pojo.UserT">
        <constructor-arg type="java.lang.String" value="kuangshen2"/>
        <constructor-arg type="int" value="15"/>
    </bean>

Test

 @Test
    public void testT(){
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        UserT user= (UserT) context.getBean("userT");
        user.show();
    }

输出:

结论:

在配置文件加载的时候。其中管理的对象都已经初始化了!

 

 

Spring配置

Alias(没啥用,因为有Bean的配置)

<?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="userT" class="com.yao.pojo.UserT">
        <constructor-arg type="java.lang.String" value="kuangshen2"/>
        <constructor-arg type="int" value="15"/>
    </bean>
    <!--设置别名:在获取Bean的时候可以使用别名获取-->
    <alias name="userT" alias="T"/>

</beans>

 

    @Test
    public void testT(){
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        UserT user= (UserT) context.getBean("T");
        user.show();
    }

Bean的配置

  • id是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
  • 如果配置id,又配置了name,那么name是别名
  • name可以设置多个别名,可以用逗号,分号,空格隔开
  • 如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象
   <!--bean就是java对象,由Spring创建和管理-->

    <!--
       id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
       如果配置id,又配置了name,那么name是别名
       name可以设置多个别名,可以用逗号,分号,空格隔开
       如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;

    class是bean的全限定名=包名+类名
    -->

    <bean id="userT" name="u,uu uuu;uuuu" class="com.yao.pojo.UserT">
        <property name="name" value="kuangshen2"/>
        <property name="age" value="15"/>
    </bean>

 

import

团队的合作通过import来实现 

Test

    @Test
    public void testT(){
//        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml","beans2.xml");
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserT user= (UserT) context.getBean("uuuu");
        user.show();
    }

applicationContext.xml下import

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

    <import resource="beans.xml"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xml"/>
    <import resource="beans4.xml"/>

</beans>

重复的会合并

 

 

 

 

 

 

Spring-IOCSpring框架的核心部分之一,它是一种设计模式,全称为Inversion of Control(控制反转)。它通过将对象的创建、依赖关系的管理和对象的生命周期交给Spring容器来实现,从而降低了组件之间的耦合度,提高了代码的可重用性和可维护性。Spring-IOC的实现主要依靠Spring容器,Spring容器是Spring框架的核心,它负责创建、管理和装配Bean对象,其中Bean是Spring框架中最基本的组件。 Spring-IOC的实现主要有两种方式:BeanFactory和ApplicationContext。其中,BeanFactory是Spring-IOC的基本实现,而ApplicationContext是BeanFactory的子接口,提供了更多高级特性。ApplicationContext是Spring框架中最常用的IOC容器,它除了提供BeanFactory的所有功能外,还提供了更多的企业级特性,例如AOP、事务管理、国际化、事件传播等。 下面是一个简单的Spring-IOC的例子,假设我们有一个UserService接口和一个UserServiceImpl实现类,我们可以通过Spring-IOC容器来创建和管理UserServiceImpl对象: 1.定义UserService接口和UserServiceImpl实现类 ```java public interface UserService { void addUser(User user); } @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { // 添加用户的具体实现 } } ``` 2.在Spring配置文件中配置UserService实例 ```xml <bean id="userService" class="com.example.service.UserServiceImpl"/> ``` 3.在代码中获取UserService实例并使用 ```java ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean("userService", UserService.class); User user = new User(); userService.addUser(user); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值