Spring根据XML配置文件注入对象类型属性

本文介绍如何在Spring中通过XML配置文件来创建对象并注入对象类型的属性,以降低组件间的耦合。讨论了在DAO、Service和Servlet中的应用,并提供了一个因忘记更新setter方法名称导致的错误案例及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里有dao、service和Servlet三个地方

通过配过文件xml生成对象,并注入对象类型的属性,降低耦合

dao文件代码:

package com.swift;

public class DaoUser {
    public void fun() {
        System.out.println("I'm  dao's fun()....................");
    }
}

service文件代码:(提供setter方法,xml文件可通过这种方法配置)

package com.swift;

public class ServiceUser {
    private DaoUser daoUser;
    
    public void setDaoUser(DaoUser daoUser) {
        this.daoUser = daoUser;
    }

    public String fun() {
        System.out.println("I am Service's fun()..............");
        return daoUser.fun();
    }
}

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">
<!-- IoC 控制反转 Spring根据XML配置文件注入对象类型属性 -->
<bean id="dao" class="com.swift.DaoUser"></bean>
<bean id="service" class="com.swift.ServiceUser">
<property name="daoUser" ref="dao"></property>
</bean>
</beans>

Servlet类文件可以绕开dao的文件,直接使用service即可

package com.swift;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@WebServlet("/duixiang")
public class ServletService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public ServletService() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        @SuppressWarnings("resource")
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ServiceUser service=(ServiceUser) context.getBean("service");
        response.getWriter().append(service.fun());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

中间因修改属性名字,而忽略修改setter方法名字导致出错,已改好,错误的类型记录了一下:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'daoUser' of bean class [com.swift.ServiceUser]: Bean property 'daoUser' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
提示的很好,要不自己很难发现。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值