Spring根据XML配置文件注入对象类型属性
这里有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配置文件注入对象类型属性的更多相关文章
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
方法一使用setter方法 package com.swift; public class Book { private String bookName; public void setBook(St ...
- commons-logging 和log4j包下载 Spring根据XML配置文件生成对象
需要用到Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/1qXLHzAW 以及日志j ...
- Java框架spring 学习笔记(八):注入对象类型属性
使用set方法注入对象属性 编写UserDao.java文件 package com.example.spring; public class UserDao { public void print( ...
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...
- Spring_day01--注入对象类型属性(重点)_P名称空间注入_注入复杂类型属性_IOC和DI区别_Spring整合web项目原理
注入对象类型属性(重点) Action要new一个service对象,Service中又要new一个Dao对象,现在把new的过程交给spring来操作 1 创建service类和dao类 (1)在s ...
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
XML配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- Spring根据XML配置文件 p名称空间注入属性(property后出现,简便但只针对基本数据类型管用,自定义集合等引用类型无效)
要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void ...
- spring注入对象类型的属性
一.1.创建service类和Dao类 (1)在service中得到dao对象 2.具体实现过程 (1)在service里边把dao作为类型属性 (2)生成dao类型属性的set方法 public c ...
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
通过配置文件XML方法的配置 可以使用非常简练的Service类 UserService类代码如下: package com.swift; public class UserService { pri ...
随机推荐
- 一文解析总结Java虚拟机内存区域模型
最近抽空看了一点<深入理解Java虚拟机>,本篇文章主要来总结一下Java虚拟机内存的各个区域,以及这些区域的作用.服务对象以及其中可能产生的问题,作为大家的面试宝典. 首先我们来看一下J ...
- hyperledger fabric 1.0.5 分布式部署 (三)
本篇博客主要是向读者介绍 fabric 在部署时的一些细节,还有作者自己学习过程中的心得. 初始化相关密钥的程序,实际上是一个shell脚本,并且结构特别简单 generateArtifacts.sh ...
- Python 实现简单的登录注册界面
Python 实现简单的登录注册界面 注意:编写代码之前需要导入很重要的包 import tkinter as tk import pickle from tkinter import message ...
- day04 基本类型包装类
- C. Chessboard( Educational Codeforces Round 41 (Rated for Div. 2))
//暴力 #include <iostream> #include <algorithm> #include <string> using namespace st ...
- 有趣的JS存储 连等问题
五个月不见了,你是不是和我一样又帅了,今天我们先来看一道经典的关于JS存储的题目,来一场紧张又刺激的脑内吃鸡大战吧: var a = {n:1}; a.x = a = {n:2}; console.l ...
- mongodb vs redis(Tokyo Tyrant转)
* MongoDB vs Redis vs Tokyo Tyrant(原文链接:http://www.cnblogs.com/riceball/archive/2010/03/05/MongoDB_V ...
- python使用C扩展
CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/C API.每 ...
- 洛谷P3603 || bzoj 4763 雪辉 && bzoj4812: [Ynoi2017]由乃打扑克
https://www.luogu.org/problemnew/show/P3603 https://www.lydsy.com/JudgeOnline/problem.php?id=4763 就是 ...
- aix 推荐使用重启
重启os AIX 主机 推荐 shutdown –Fr 在客户一次停机维护中,发现了这个问题. 环境是ORACLE 10G RAC for AIX6,使用了HACMP管理共享磁盘. 在停机维护时间段内 ...