自学内容网 自学内容网

【软件开发】Spring Bean的装配方式(头歌作业)

第1关:基于 XML 的 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" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    <!-- 请在此处编写代码 -->
    <!--********* Begin *********-->
    <!-- 使用设值注入方式装配Person实例 -->
     <bean id="person1" class="educoder.Person">
        <property name="name" value="张三" />
        <property name="sex" value="男" />
    </bean>

    <!-- 使用构造方法装配Person实例 -->
    <bean id="person2" class="educoder.Person">
        <constructor-arg index="0" value="李四" />
        <constructor-arg index="1" value="女" />
    </bean>

    <!--********* End *********-->
</beans>
package educoder;
public class Person {
    // 属性值 name
    private String name;
    // 属性值 age
    private String sex;
    // 请在此处编写代码
    /********* Begin *********/
    public Person() {
    }

    // 有参构造方法
    public Person(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    // get和set方法
    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    // 重写 toString 方法
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

第2关:基于注解的 Bean 装配

package com.educoder.springtest;

import org.springframework.stereotype.Repository;
// 请在此处编写代码
/********* Begin *********/
@Repository("studentDao")
/********* End *********/

public class StudentDaoImpl implements StudentDao{
    // 重写 showTables 方法
    @Override
    public void showTables() {
        System.out.println("执行Dao层的showTables()方法");
    }
}
package com.educoder.springtest;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
// 请在此处编写代码
/********* Begin *********/
@Service("studentService")
/********* End *********/

public class StudentServiceImpl implements StudentService {
    // 请在此处编写代码
    /********* Begin *********/
   @Resource(name="studentDao")
    /********* End *********/

    private StudentDao studentDao;
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public void setStudentDao(StudentDao studentDao){
        this.studentDao=studentDao;
    }
    // 重写 toTime 方法
    @Override
    public void toTime() {
        // 调用 studentDao 中的 showTables() 方法
        studentDao.showTables();
        System.out.println("执行Service层的toTime()方法");
    }
}
package com.educoder.springtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
// 请在此处编写代码
/********* Begin *********/
@Controller("studentAction")
/********* End *********/

public class StudentAction {
    // 请在此处编写代码
    /********* Begin *********/
   @Autowired
    /********* End *********/

    private StudentService studentService;
    public StudentService getStudentService() {
        return studentService;
    }
    public void setStudentService(StudentService studentService ){
        this.studentService=studentService;
    }


    public void show() {
        // 调用 personService 中的 toTime() 方法
        studentService.toTime();
        System.out.println("执行Action层的show()方法");
    }
}

第3关:自动装配 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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 请在此处编写代码 -->
    <!--********* Begin *********-->
   
    <bean id="studentDao" class="com.educoder.springtest.StudentDaoImpl" />
    <bean id="studentService" class="com.educoder.springtest.StudentServiceImpl" autowire="byName" />
    <bean id="studentAction" class="com.educoder.springtest.StudentAction" autowire="byName" />

    <!--********* End *********-->

</beans>


原文地址:https://blog.csdn.net/weixin_74154742/article/details/143931031

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!