这里有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?
提示的很好,要不自己很难发现。

Spring根据XML配置文件注入对象类型属性的更多相关文章

  1. Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值

    方法一使用setter方法 package com.swift; public class Book { private String bookName; public void setBook(St ...

  2. commons-logging 和log4j包下载 Spring根据XML配置文件生成对象

    需要用到Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/1qXLHzAW 以及日志j ...

  3. Java框架spring 学习笔记(八):注入对象类型属性

    使用set方法注入对象属性 编写UserDao.java文件 package com.example.spring; public class UserDao { public void print( ...

  4. Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)

    Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...

  5. Spring_day01--注入对象类型属性(重点)_P名称空间注入_注入复杂类型属性_IOC和DI区别_Spring整合web项目原理

    注入对象类型属性(重点) Action要new一个service对象,Service中又要new一个Dao对象,现在把new的过程交给spring来操作 1 创建service类和dao类 (1)在s ...

  6. Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

    XML配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  7. Spring根据XML配置文件 p名称空间注入属性(property后出现,简便但只针对基本数据类型管用,自定义集合等引用类型无效)

    要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void ...

  8. spring注入对象类型的属性

    一.1.创建service类和Dao类 (1)在service中得到dao对象 2.具体实现过程 (1)在service里边把dao作为类型属性 (2)生成dao类型属性的set方法 public c ...

  9. Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法

    通过配置文件XML方法的配置 可以使用非常简练的Service类 UserService类代码如下: package com.swift; public class UserService { pri ...

随机推荐

  1. Unity2d 骨骼动画1:介绍

    http://bbs.9ria.com/thread-401613-1-1.html by Orlando Pereira and PedroPereira3 days ago2 Comments 在 ...

  2. C 语言实例 - 连接字符串

    C 语言实例 - 连接字符串 C 语言实例 C 语言实例 使用 strcat() 连接两个字符串. 实例 #include <stdio.h> int main() { ], s2[], ...

  3. iOS集成支付宝支付

    本文由本人原创发表于博客园,转载请注明出处 http://www.cnblogs.com/wangqw/p/5074907.html 一. 开发前准备 iOS 支付宝SDK下载地址:(内含iOS An ...

  4. Serervlet | 慕课课程实战 | 编写登录逻辑

    Users.java package com.po; public class Users { private String username; private String password; pu ...

  5. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

  6. 网络请求方法(SDK封装可以替换afn)

    //个人觉得 sdk开发时候 最好不要用第三方 最好可以用最原始的方法 替换 此处仅做sdk封装使用 留存+(void)GET:(NSString *)urlStr params:(NSDiction ...

  7. DRF教程3-类视图

    Rewriting our API using class-based views 使用类视图重写root views,只需要一点点重构. from django.http import Http40 ...

  8. [软件工程基础]2017.11.02 第六次 Scrum 会议

    具体事项 燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #10 搭建可用的开发测试环境:#9 阅读分析 PhyLab 后端代码与文档:#8 掌握 Laravel 框 ...

  9. Java微信公众平台开发(九)--微信自定义菜单的创建实现

    自定义菜单这个功能在我们普通的编辑模式下是可以直接在后台编辑的,但是一旦我们进入开发模式之后我们的自定义菜单就需要自己用代码实现,所以对于刚开始接触的人来说可能存在一定的疑惑,这里我说下平时我们在开发 ...

  10. WPF CanExecuteChanged

    继承ICommand ,RelayCommand命令 public class RelayCommand : ICommand { private readonly Action _execute; ...