首页 > 其他分享 >注入依赖对象

注入依赖对象

时间:2023-04-27 22:01:22浏览次数:25  
标签:personDao IPersonDao 依赖 name IPersonServer 对象 void public 注入


知识点:

【 


基本类型对象注入: 

<bean id="orderService" class="cn.itcast.service.OrderServiceBean"> 

 <constructor-arg index=“0” type=“java.lang.String” value=“xxx”/>//构造器注入 

 <property name=“name” value=“zhao/>//属性setter方法注入 

</bean> 

注入其他bean: 

方式一 

<bean id="orderDao" class="cn.itcast.service.OrderDaoBean"/> 

<bean id="orderService" class="cn.itcast.service.OrderServiceBean"> 

 <property name="orderDao" ref="orderDao"/> 

</bean> 

方式二(使用内部bean,但该bean不能被其他bean使用) 

<bean id="orderService" class="cn.itcast.service.OrderServiceBean"> 

 <property name="orderDao"> 

 <bean class="cn.itcast.service.OrderDaoBean"/> 

 </property> 

</bean> 



】 


环境搭建见上.... 


实现步骤: 

第一步:建立PersonDaoBean 和 IpersonDao接口 


IpersonDao.java 

public interface IPersonDao { 


 public abstract void add(); 


} 


PersonDaoBean.java 


public class PersonDaoBean implements IPersonDao { 

 /* (non-Javadoc) 

 * @see com.liyong.PersonDaoBean.Imp.PersonDao#add() 

 */ 

 public void add(){ 

 System.out.println("执行PersonDaoBean中的add()方法"); 

 } 

} 


第二步:编写PersonServer和IPersonServer 


IPersonServer.java 


public interface IPersonServer { 


 public abstract void save(); 


} 


PersonServer.java 


public class PersonServer implements IPersonServer { 

 /* 

 * 注意必须为这些属性提供setter方法 

 * 通过属性注入依赖对象 

 */ 

 private IPersonDao personDao; 

 private String name; 


 public PersonServer(){} 

 /* 

 * 通过构造器来注入依赖对象 

 */ 

 public PersonServer(IPersonDao personDao,String name) { 


 this.name = name; 

 this.personDao = personDao; 


 } 


 public String getName() { 

 return name; 

 } 


 public void setName(String name) { 

 this.name = name; 

 } 


 public IPersonDao getPersonDao() { 

 return personDao; 

 } 


 public void setPersonDao(IPersonDao personDao) { 

 this.personDao = personDao; 

 } 




 public void save() 

 { 

 personDao.add(); 

 System.out.println("name="+name); 

 System.out.println("save is OK !"); 

 } 

} 


第四步:编写beans.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-2.5.xsd"> 

<!--外部引用 

<bean id="personDaoBean" class="com.liyong.PersonDaoBean.Imp.PersonDaoBean"/> 

 <bean id="personServer" class="com.liyong.ServersBean.PersonServer" > 

 <property name="personDao" ref="personDaoBean"/> 

 </bean> 

 --> 

 <!--使用内部bean,但该bean不能被其他bean使用 

<bean id="personServer" class="com.liyong.ServersBean.PersonServer" > 

 <property name="personDao"> 

 <bean class="com.liyong.PersonDaoBean.Imp.PersonDaoBean"/> 

 </property> 

 </bean> 

 --> 

<!--使用构造器注入依赖对象--> 

<bean id="personDaoBean" class="com.liyong.PersonDaoBean.Imp.PersonDaoBean"/> 

 <bean id="personServer" class="com.liyong.ServersBean.PersonServer"> 

 <constructor-arg index="0" ref="personDaoBean"/> 

 <constructor-arg index="1" value="liyong"/> 

 </bean> 

</beans> 


第五步:编写单元测试 


public class JUnitTest { 


 @Test 

 public void TestSave() 

 { 

 //得到Spring容器实例 

 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 

 //ApplicationContext ctx2 = new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); 


 //这里面向接口 

 IPersonServer server =(IPersonServer)ctx.getBean("personServer"); 

 server.save(); 

 } 

} 


第六步:部署 


.....

标签:personDao,IPersonDao,依赖,name,IPersonServer,对象,void,public,注入
From: https://blog.51cto.com/u_16091571/6232153

相关文章

  • Langchain框架 prompt injection注入
    Langchain框架promptinjection注入PromptInjection是一种攻击技术,黑客或恶意攻击者操纵AI模型的输入值,以诱导模型返回非预期的结果Langchain框架LangChain是一个基于大语言模型进行应用开发的框架。所谓大语言模型(LargeLanguageModels,LLMs),是指基于海量语料训练、......
  • 探究“黑科技”:自动驾驶中的对象追踪技术丨曼孚科技
    自动驾驶在真正上路前,会经过上千公里的测试。许多自动驾驶公司为了更好地掌握车辆的安全性能,会在训练中增设障碍物,如果车辆能够成功规避障碍物,说明该自动驾驶车辆更安全。而对象追踪技术的出现,对解决这类问题起到了关键性作用。作为无人驾驶的必要技术之一,目标追踪通过计算机视......
  • Amazon S3 对象存储Java API操作记录(Minio与S3 SDK两种实现)
    缘起今年(2023年)2月的时候做了个适配AmazonS3对象存储接口的需求,由于4月份自学考试临近,一直在备考就拖着没总结记录下,开发联调过程中也出现过一些奇葩的问题,最近人刚从考试缓过来顺手记录一下。S3对象存储的基本概念S3是什么?AmazonS3(SimpleStorageService)对象存储出现......
  • .NET CORE 通过依赖注入获取所有的的实现
    先定义一个接口1///<summary>2///支付接口3///</summary>4publicinterfaceIPaymentService5{67///<summary>8///支付类型9///</summary>10publicstringPayType{get;......
  • Gson 对对象下的数组进行转换
    现有一个json数据{"result":[{"powerUnitId":1,"powerUnitName":"供电单位1"},{"powerUnitId":2,"p......
  • Java把实体转为map对象
    方式一importorg.springframework.cglib.beans.BeanMap;BeanMap.create(entityObj); 方式二importcom.alibaba.fastjson.JSONObject;//方式1、强转为JSONObjectJSONObjectxxx=(JSONObject)JSONObject.toJSON(xxxEntity);//方式2、转成json,在转为mapStringjs......
  • Spring17_配置文件依赖注入4
    一、Bean的依赖注入入门1.创建UserService,UserService内部再调用UserDao的save()方法 2.将UserServiceImpl的创建权交给Spring3.从Spring容器中获得UserService进行操作执行UserController中的main方法,检查控制台输出:二、Bean的依赖......
  • 记一次峰回路转的注入
    0X01背景自己之前写过一篇记录,当时是由于之前是一位校友刚做开发,叫我友情帮忙测试一波,由于是开发的新手,漏洞比较多,所以直接从注入开始讲起,但是到getshell的过程也算是一场峰回路转再跌跌撞撞的路程。0x02注入测试判断注入是否存在http://11X.XX.XXX.XX/test/sub_hack.php?id......
  • JDBC使用List和Map键值封装多表查询结果 java JDBC insert查询返回List对象
    JDBC使用List和Map键值封装多表查询结果https://www.cnblogs.com/zengyu1234/p/15451812.html查询结果的处理Java.sql.ResultSet接口是jdbcAPI中唯一用来封装查询结果记录行的组件。ResultSet接口唯一创建方式是通过执行SQL查询返回创建此对象 遍历结果集中数据truenext......
  • Day 27 27.2 JS进阶之window对象
    JS-Function对象之window对象window是客户端浏览器对象模型的基类,window对象是客户端JavaScript的全局对象。一个window对象实际上就是一个独立的窗口,对于框架页面来说,浏览器窗口每个框架都包含一个window对象。(1)全局作用域在客户端浏览器中,window对象是访问BOM......