这里有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. 删除node_modul模块

    npm安装rimraf ,npm版本号要是低于5.x.x 具体不记得了,不然就安装不了这个工具 npm install rimraf -g 然后: rimraf node_modules 在这里学到的 ...

  2. UGUI技术之LayoutGroup布局实现详解

    继续学习unity4.6,unity的LayoutGroup分为三种, Horizontal Layout Group(水平布局)Vertical Layout Group(垂直布局)Grid Lay ...

  3. Go语言之父谈Go:大道至简

    http://www.csdn.net/article/2012-07-05/2807113-less-is-exponentially-more 摘要:导读:这篇文章是Google首席工程师.Go语 ...

  4. JDK1.7 和 jetty配置教程

    系统是windows 7 64位版本,32位版本同理,xp系统的自己google设置环境变量 打开设置环境变量窗口,右键计算机->我的电脑,选择属性 点击高级系统设置 选择环境变量 红线为需要设 ...

  5. PJzhang:尽快发现并下架那些侵犯公司权利的假冒APP

    猫宁!!! 参考链接:https://www.freebuf.com/articles/paper/203358.html http://www.cac.gov.cn/2019-01/25/c_112 ...

  6. laravel-admin 配置富文本编辑器流程

    laravel-admin默认去除富文本编辑器的,官方也给出了配置方法. 我配置的是wangEditor,本来配置完后就能愉快得使用了,可万万没想到还是有坑的.默认是用base64上传的,也就是数据库 ...

  7. 最大xor,and,or

    http://210.33.19.103/contest/998 and,or部分 并不用01trie,题目&题解:https://blog.csdn.net/dreaming__ldx/ar ...

  8. JAVA常用知识总结(十四)——Servlet

    Servlet属于线程安全的吗? Servlet不是线程安全的! 谈谈转发和重定向的区别 请求转发: request.getRequestDispatcher("/king_l2lu.jsp ...

  9. MODBUS移植的参考文章

    https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32 http://www.360doc.com/content/14/0906/09 ...

  10. python_12(并发编程)

    第1章 进程 1.1 队列Queue 1.2 Queue方法 1.2.1 q.get([block [,timeout]]) 1.2.2 q.get_nowait() 1.2.3 q.put(item ...