springIOC使用xml装配JavaBean对象

在一个maven工程下,在pom.xml中导入spring依赖和相关的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>MavenTest</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>SpringIOC</module>
        <module>sgsystem</module>
        <module>test</module>
    </modules>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <!--  依赖都是放入dependencies  -->
    <dependencies>
        <!-- 每一依赖 dependency标记-->
        <!--   Spring依赖  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

</project>

新建一个配置类,在类的上方使用@configuration注解标识这是spring容器;使用@ComponentScan注解让所有被修饰为@Component的类纳入容器管理

@Configuration
// basePackages 指定从哪个包开始扫描组件
@ComponentScan(basePackages = "com.project")
// basePackageClasses 指定从哪个类所在的包开始扫描
//@ComponentScan(basePackageClasses = ApplicationConfig.class)
public class ApplicationConfig {

}

新建一个学生Bean类,按照javaBean的规范去完成即可,这里为了方便,只展示了属性。注意别忘了给这个类上加上一个@Component注解,这样才会被纳入spring容器管理

//    写学生类包含属性(id、姓名、年龄、性别)
    private int sutId;
    private String name;
    private int age;
    private String gender;

以上完成之后,在resources目录下,书写装配javaBean文件,文件名是Application.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"
       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 https://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="db.properties"/>

 属性注入 是通过set方法进行注入 此时会用到property标签-->
<!--               name属性指代对象中的属性名称,value指代属性值

    <bean id="stu" class="com.project.bean.StudentBean">
        <property name="sutId" value="1"></property>11
        <property name="age" value="30"></property>
        <property name="gender" value="男"></property>
        <property name="name" value="张三"></property>
    </bean>




</beans>

构造器注入

<bean>

 通过构造方法进行注入,使用constructor-arg标签,-->
<!--            当只提供value属性值时,值的设置顺序要和构造方法一致->

 <constructor-arg value="四川成都成华区" index="4"></constructor-arg>
 <constructor-arg value="男" index="2"></constructor-arg>
<constructor-arg value="10" index="3"></constructor-arg>
 <constructor-arg value="1" index="0"></constructor-arg>
 <constructor-arg value="李四" index="1"></constructor-arg>



</bean>

接下来就可以测试了

    // 根据配置文件 创建容器对象
       ApplicationContext context =
                new ClassPathXmlApplicationContext("Application.xml");
        // 通过getBean方法获取bean对象
        // 通过bean标记的id值获取stu对象
    StudentBean stu = (StudentBean) context.getBean("stu");

总结:springIOC和IC

IOC:Inversion Of Control,即控制反转,是一种设计思想。

将主动创建对象的方式反转成被动接受。

实现代码的解耦。

IC:

Dependency injection,即依赖注入。

依赖注入是IOC的一种实现。

在Spring中交由容器来管理所有对象。

什么是控制反转呢?

控制反转--就是依赖倒置原则的一种代码设计的思路。

采用的方法就是所谓的依赖注入(DI) 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值