当前位置: 首页 > 编程日记 > 正文

【spring】自动装配

山顶洞人方法:aotowire

UserDao.java

代码实现:

public class UserDao {private DBUtil dbu;public DBUtil getDbu() {return dbu;}public void setDbu(DBUtil dbu) {this.dbu = dbu;}public void test() {System.out.println(dbu);}
}

DBUtil.java

代码实现:

算了,不实现了,DBUtil.java有就行了

配置文件:

代码实现:

    <bean id="dbutil" class="springboottest.ioc.speltest.DBUtil"></bean><bean id="userdao" class="springboottest.ioc.speltest.UserDao" autowire="byType">

注:

此时可能出现一个问题:

当要自动装配一个接口时,该接口有两个实现类,此时计算机就不知道选哪个,所以会报错

错码实现:

DBservice.java

public class DBService {private TwoSonTest ts;public TwoSonTest getTs() {return ts;}public void setTs(TwoSonTest ts) {this.ts = ts;}public void test() {System.out.println("=======>" + ts);}
}

配置文件:

    <bean id="firstsontest" class="springboottest.ioc.FirstSon" ></bean><bean id="secondSon" class="springboottest.ioc.SecondSon" ></bean><bean id="DBService" class="springboottest.ioc.spel.DBService" autowire="byType"></bean>

注:firstsontest.java/secondsontest.java是接口twosontest.java的实现类

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBService' defined in class path resource [config/applicationContext.xml]: Unsatisfied dependency expressed through bean property 'ts'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'springboottest.ioc.TwoSonTest' available: expected single matching bean but found 2: firstsontest,secondSon

解决方法:

使用byName的方法

代码实现:

    <bean id="ts" class="springboottest.ioc.FirstSon" ></bean><bean id="secondSon" class="springboottest.ioc.SecondSon" ></bean><bean id="DBService" class="springboottest.ioc.spel.DBService" autowire="byName">

注:

(1)使用byName的方法必须将DBService.java中的变量名和一个bean的名称一致

(2)使用byName的方法不会报错,但是如果将对象打印出来为null则说明没有找到名称一致的bean对象

扫描方法自动装配

DBUtil.java

代码实现:

package springboottest.ioc.auto;import org.springframework.stereotype.Repository;@Repository
public class DButil {private String constant;}

UserDao.java

代码实现:

package springboottest.ioc.auto;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;@Repository
public class UserDao {@Autowiredprivate DButil dbu;public void test() {System.out.println(dbu);}
}

配置文件

代码实现:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="springboottest.ioc.auto"></context:component-scan></beans>       

使用步骤:

1、在配置文件中添加如上述代码,在base-package的那一项添加文件所在的jar包名称

2、在需要扫描的文件添加注解

3、如果想要在类里添加自定义对象

在方法上或者在变量上添加@Autowired注解,如果是一个接口且该接口有多个实现类,则需要

相关文章:

扩展jquery实现客户端表格的分页、排序

下面链接中是我用jQuery的扩展来实现的表格分页和排序&#xff0c;使用这个扩展必须加上表头<thead>和<tbody>标签&#xff0c;因为我是 通过<tbody>来进行分页的&#xff0c;要是不加thead&#xff0c;那么表头也要作为分页计算时的一个行了。 下载最新代码…

Win7中如何删除word模板

Win7中如何删除word模板 计算机→本地磁盘c盘→用户→Administrator→AppData→Roaming→Microsoft→Templates转载于:https://blog.51cto.com/ilanni/555302

Spring 学习笔记

Spring 的 Ioc 容器所有的组件都是被动的&#xff08; Passive&#xff09;&#xff0c;所有的组件初始化和调用都由容器负责。组件处在一个容器当中&#xff0c;由容器负责管理。BeanFactory 根据配置文件确定容器中 bean 的实现&#xff0c;管理 bean 之间的依赖关系。通常&a…

【spring】初识aop(面向切面编程) 使用jdk动态代理

BankServiceIImple.java 代码实现&#xff1a; package com.zzxtit.aop;import java.math.BigDecimal;public interface BankServiceImple {public void transfer(String source, String target, BigDecimal money);public void withdraw(String account, BigDecimal money);…

sigaction

#include <signal.h> int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact); struct sigaction {   void (*sa_handler)(int);   void (*sa_sigaction)(int, siginfo_t *, void *);   sigset_t sa_mask;   int sa_flags;   void …

003本周总结报告

本周对java的循坏结构和条件语句以及switch分支进行了复习并通过九九乘法表和制作日历来更加熟练使用和理解循环&#xff0c;并用eclipse替代了记事本来编写程序&#xff0c;同时针对记事本编写java程序后台运行出现的GBK不可映射字符问题先后采用了 javac -encoding UTF-8 …

HDU-1203 I NEED A OFFER!-0、1背包及空间优化

I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5280 Accepted Submission(s): 1799Problem DescriptionSpeakless很早就想出国&#xff0c;现在他已经考完了所有需要的考试&#xff0c;准备了…

docker监控系统

第一&#xff1a;docker监控系统之命令行式监控 第二&#xff1a;docker监控系统之cadvisor 第三&#xff1a;docker监控系统之 第四&#xff1a;docker监控系统之 转载于:https://www.cnblogs.com/fengjunhua/p/8968210.html

Visual Studio 2005 Team System下载地址

注册一个msn就可以去微软下载了&#xff0c;关于替换序列号变成正版的方法我没有试&#xff0c;team suite我在用&#xff0c;但Team Foundation Server 我还没有安装好Microsoft Visual Studio 2005 简体中文正式版Visual Studio 2005 Team Suite 180 天试用版(可更换key变为正…

【spring】springAop开发

xml开发方式 springboot的使用 1、声明一个parent 代码实现&#xff1a; <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.0.RELEASE</version><relati…

使用 SqlHelperParameterCache 类管理参数

SqlHelperParameterCache类是位于 Microsoft.ApplicationBlocks.Data命名空间底下。它底下有三个方法&#xff0c;分别是&#xff1a; CacheParameterSet&#xff1a;用于将SqlParameters 数组存储到缓存中GetCachedParameterSet&#xff1a;用于检索读取缓存中SqlParameters数…

改善C#程序的建议6:在线程同步中使用信号量

所谓线程同步&#xff0c;就是多个线程之间在某个对象上执行等待&#xff08;也可理解为锁定该对象&#xff09;&#xff0c;直到该对象被解除锁定。C#中对象的类型分为引用类型和值类型。CLR在这两种类型上的等待是不一样的。我们可以简单的理解为在CLR中&#xff0c;值类型是…

Jerry眼中的SAP客户数据模型

本文Jerry将介绍八款SAP产品中的客户模型。希望您在阅读完本文之后&#xff0c;能对SAP客户模型设计的思路有一个最最粗浅的了解。 由于Jerry水平和精力所限&#xff0c;本文不会详细阐述这些产品里的客户模型设计细节&#xff0c;而是介绍了一种方法&#xff0c;如果您对这些模…

【spring】spring JDBC开发 、 将创建表生成sql语句的方法

将navicate中已存在表的创建转化成sql语句的方法 1、右击表&#xff0c;选择对象信息 2、点击DDL jar包引入 1、spring-starter-jdbc 代码实现&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-star…

PL/SQL ——分页编程

通过PL/SQL编程&#xff0c;编写分页存储过程。代码如下所示&#xff1a; 1 --PL/SQL开发编写分页代码 2 --创建包 3 create or replace package Page as 4 type test_cursor is ref cursor 5 end Page; 6 --创建存储过程 7 create or replace procedure Page( 8 (tablenam…

My view towards Machine Learning

Introduction:[to be continued]转载于:https://www.cnblogs.com/JVKing/articles/2290780.html

两个表的更新、表的复制

update 表1 set from 表1&#xff0c;表2 where 条件 表的复制数据 1.select * into newtable from table 2.insert into table select table2 select identity(int,1,1),* into newtable from .. 这条语句有时挺管用的转载于:https://www.cnblogs.com/leshang/archive/2…

Java数据结构简述

1、数组 概念&#xff1a;一个存储元素的线性集合。 数组声明和创建&#xff1a; dataType[] arrayRefVar new dataType[arraySize]; 二维数组&#xff08;多维数组&#xff09;声明和创建&#xff1a; dataType[][] arrayName new dataType[arraylenght1][arraylenght2]; PS…

CORS漏洞利用检测和利用方式

CORS全称Cross-Origin Resource Sharing, 跨域资源共享&#xff0c;是HTML5的一个新特性&#xff0c;已被所有浏览器支持&#xff0c;不同于古老的jsonp只能get请求。 检测方式&#xff1a; 1.curl访问网站   curl https://www.huasec.com -H "Origin: https://test.com…

【spring】具名参数

具名参数 配置xml文件 1、配置dataSource&#xff08;数据源&#xff09; 代码实现&#xff1a; <context:property-placeholderlocation"DB.properties"></context:property-placeholder><bean id"dataSource"class"com.alibaba.d…

利用委托和泛型实现树的常用操作

在日常开发中&#xff0c;经常遇到对树的操作&#xff0c;我们可以利用泛型和委托对这些树进行操作&#xff0c;这样就不需要每有一个树就要实现相应的功能了。 源码在http://files.cnblogs.com/haiconc/LangTest.zip 首先&#xff0c;使用类泛型声明&#xff1a; public class…

Scott的ASP.net MVC框架系列文章之四:处理表单数据(2)

前几周我发表了一系列文章介绍我们正在研究的ASP.NET MVC框架。ASP.NET MVC框架为你提供了一种新的开发Web应用程序的途径&#xff0c;这种途径可以让应用程序变得更加层次清晰&#xff0c;而且更加有利于对代码进行单元测试和支持TDD&#xff08;测试驱动开发&#xff09;开发…

eclipse工作空间配置导出

由于工作与学习的需求&#xff0c;需要使用不同的工作空间。而eclipse的新建工作空间其他以前的配置都没有继承过来&#xff0c;那么就得重新配置一遍。 经过学习其他前辈们的经验与自己的摸索总结一下3种方法&#xff1a; 方法一&#xff1a;使用eclipse的导出功能。 工作目录…

探讨UnsupportedOperationException的原因及解决方案

[原文链接]{https://blog.csdn.net/liu_005/article/details/74091805} 转载于:https://www.cnblogs.com/Wbin01/p/11222483.html

成长轨迹44 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2799、2976、2975、2742】...

一次ac的就不说啥了。。 2799:浮点数格式 View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <ctype.h> 4 #include <cmath> 5 6 char flo[10050][55]; 7 int poi[10050]; 8 9 int main()10 {11 int num;12 scanf("%d…

【转载】xmind的使用安装方法

原文链接&#xff1a;https://blog.csdn.net/qq_16093323/article/details/80967867

BCB Access violateion at Address 0000 0003. Read of address 0000 0003

来自网页&#xff1a;&#xff08;我的电脑做不到&#xff09; 运行一个程序&#xff0c;莫名出现一个对话框:access violation at address 0000.. read of address000试了几次问题依旧&#xff0c;网上搜了下解决办法&#xff1a;原文&#xff1a;baidu&#xff0b;google&…

与 Scott Guthrie 一道感受技术激情 1月13日于北京

可能很多朋友已经知道了这个消息&#xff0c;我觉得还是写一下&#xff0c;别让这个机会白白溜走。Scott Guthrie是谁&#xff0c;我就不介绍了&#xff0c;简单说&#xff1a;ASP.NET之父&#xff0c;Silverlight 的主要创始人&#xff0c;还管着太多微软的开发技术和工具&…

【web】将一个jar包更改成war包

可以看到&#xff0c;向tomcat中发布工程刚创建的工程不在可添加的范围内&#xff0c;所以可以看出该工程是一个jar包 1、在pom文件中添加一行代码 代码实现&#xff1a; <artifactId>jar.to.war</artifactId> 2、更新maven工程 此时发现main文件夹下出现了一个w…

牛腩44 整合登陆页 RequiredFieldValidator 和 ValidationSummary 以及 asp.net 自带的MD5 加密...

在我们后台登陆的时候,有 用户名,密码和验证码3个必选项,所以我们托3个验证控件过来 例如这里,如果没有填写用户名,当点提交的时候,显示 红色的 * 号,并且弹出一个 alert 效果如下 这个是怎么做到的呢? 用户名后面的 * 号和弹出的 alert 分别用到 RequiredFieldValida…