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 ...
随机推荐
- [Xcode 实际操作]一、博主领进门-(12)代码重构
目录:[Swift]Xcode实际操作 本文将演示如何重构代码. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] [快速更改同名变量或常量] 在代码编辑区域,点击需 ...
- 企业级应用,如何实现服务化三(dubbo入门案例)
今天是六一儿童节,从千里之外的广州,回到了贵州老家,真好!好山好水好心情,好了接着写点东西.这是企业级应用,如何实现服务化系列的第三篇.在上一篇:企业级应用,如何实现服务化二(dubbo架构)中,认识 ...
- RPC入门
在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...
- [題解]luogu_P3205/BZOJ_1996 合唱隊
前言:基本上發題解的都是抄的題解所以 來源:題解 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个 ...
- 牛客小白月赛13 E(图、矩阵幂)
AC通道 如果建立第一天某点到某点有几条路的矩阵,做k次矩阵乘就是第k天某点到某点有几条路.统计即可. #include <bits/stdc++.h> using namespace s ...
- Influxdb 时序数据库 windows 安装
Influxdb 是一款比较火爆的时序数据库,本文介绍如何在 windows 平台下安装. 1.场景: windows 平台的 influxdb 似乎只支持单机非windows 服务的安装方式 适用于 ...
- getpass不起作用
#! /usr/bin/env python# -*- coding:utf-8 -*- # login 模块中登录时输入密码,想用getPass模块实现密码的不回显操作.#如下: import ge ...
- On the way to the park Gym - 101147I 几何
http://codeforces.com/gym/101147/problem/I I. On the way to the park time limit per test 5 seconds m ...
- disasters
1.list all the natural disasters to the best of your knowledge. 2.What are the possible causes for s ...
- Todolist总结
一.组件类里面的函数尽可能写成箭头函数的形式,方便绑定this 上面的箭头函数是好的,写面的不好,他需要在用的时候绑定this,或者在constructor绑定,如下: 如上用的时候绑定this是不好 ...