这里有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. [Xcode 实际操作]九、实用进阶-(6)在Swift文件中调用Object-C的类和方法

    目录:[Swift]Xcode实际操作 本文将演示在Swift文件中调用Object-C的类和方法. 在项目文件夹[DemoApp]上点击鼠标右键 ->[New File]创建一个Object- ...

  2. 编译keepalived 方法

    作者的环境: redhat 6.5 64 位版 在编译keepalived 前,需要提前给环境安装两个依赖包--zlib和openssl 编译 zlib 库 参考作者之前的博客 http://www. ...

  3. 分布式通信-tcp/ip 广播

    服务端 /** * 广播 */ public class MulticastServer { public static void main(String[] args) { try { //地址是2 ...

  4. 线段树-单点更新-HDU 1166 敌兵布阵

    #include <iostream> #include <cstdio> #include <string> #include <cstring> u ...

  5. 洛谷 P2231 [HNOI2002]跳蚤

    https://www.luogu.org/problemnew/show/P2231 题意相当于:有n个位置a[1..n],每个位置可以填[1,m]中任一个整数,问共有多少种填法满足gcd(a[1] ...

  6. SpringMVC-异常处理器

    1.    异常处理思路 系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生. 系统 ...

  7. JAVA常用知识总结(七)——Spring

    如果一个接口有2个不同的实现, 如何Autowire某一个指定的实现? 1.通过增加@Qualifier(实现类的名字): @Autowired @Qualifier("GirlStuden ...

  8. 17115 ooxx numbers 交表

    17115 ooxx numbers 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description a number ...

  9. oracle 直接连接到数据库 CMD窗口

    打开cmd 在命令行 中输入如下命令  sqlplus sys/zd****!@60.**.203/orcl as sysdba  就直接连上了

  10. CF1060D Social Circles

    思路: 贪心.既然每个人的左边是其他人的右边,每个人的右边是其他人的左边,那么使重叠的部分最多即可. 实现: #include <bits/stdc++.h> using namespac ...