Spring bean注入
1.构造方法注入
1.bean类
public class User {
private String name;
private Integer age;
private Cat cat;
public User(String name,Integer age,Cat cat){
this.name=name;
this.age=age;
this.cat=cat;
} @Override
public String toString() {
return "User"+"name"+name+"age"+age+"cat"+cat.getName();
}
} 2.测试方法
public void demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User)applicationContext.getBean("user");
System.out.println(user);
} 3.xml文件配置
<!--构造方法 注入-->
<bean id="user" class="com.imooc.ioc.demo4.User">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="13"></constructor-arg>
<constructor-arg name="cat" ref="cat"></constructor-arg>
</bean>
<bean id="cat" class="com.imooc.ioc.demo4.Cat">
<property name="name" value="cat"></property>
</bean>
2.set方法注入
1.bean类
public class Person {
private String name;
private Integer age;
private Cat cat;
public void setCat(Cat cat) {
this.cat = cat;
}
public Cat getCat() {
return cat;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "name"+name+"age"+age+"cat"+cat.getName();
}
}
2.测试方法
public void demo2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person)applicationContext.getBean("person1");
System.out.println(person);
}
3.xml文件配置
<!--set方法注入-->
<bean id="person1" class="com.imooc.ioc.demo4.Person">
<property name="age" value="12"></property>
<property name="name" value="李四"></property>
<property name="cat" ref="cat"></property>
</bean>
3.复杂类型的注入
数组 List set Map Properties
package com.imooc.ioc.demo5; import java.util.*; /*
*
*
* 复杂类型注入*/
public class CollectionBean {
private String[] args; //数组类型
private List<String> list;//list集合
private Set<String> set ;//set集合
private Map<String,Integer> map;
private Properties properties; public List<String> getList() {
return list;
} public Map<String, Integer> getMap() {
return map;
} public Properties getProperties() {
return properties;
} public Set<String> getSet() {
return set;
} public String[] getArgs() {
return args;
} public void setList(List<String> list) {
this.list = list;
} public void setMap(Map<String, Integer> map) {
this.map = map;
} public void setProperties(Properties properties) {
this.properties = properties;
} public void setSet(Set<String> set) {
this.set = set;
} public void setArgs(String[] args) {
this.args = args;
} @Override
public String toString() {
return "arrs"+ Arrays.toString(args)+"list"+list+"map"+map+"properties"+properties+"set"+set;
}
} 2.测试方法
public void demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean=(CollectionBean)applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
3.配置文件
<bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean" >
<!--数组的属性注入-->
<property name="args">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!--list的属性注入-->
<property name="list">
<list>
<value>111</value>
<value>333</value>
<value>222</value>
</list>
</property>
<!--set集合的属性注入-->
<property name="set">
<set>
<value>ddd</value>
<value>eee</value>
<value>fff</value>
</set>
</property>
<!--map的属性注入-->
<property name="map">
<map>
<entry key="aaa" value="111"></entry>
<entry key="bbb" value="222"></entry>
</map>
</property>
<!--properties的属性注入-->
<property name="properties">
<props>
<prop key="username">zhangsan</prop>
</props>
</property>
</bean>
Spring bean注入的更多相关文章
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
- Spring bean注入方式
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html Spring bean提供了3中注入方式:属 ...
- spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)
这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...
- spring bean 注入
概念 http://developer.51cto.com/art/200610/33311.htm http://kb.cnblogs.com/page/45266/ ==https://www.c ...
- Spring Bean 注入 2 注解篇
1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...
- [spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...
- Spring JMX之一:使用JMX管理Spring Bean
spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...
- 在非spring组件中注入spring bean
1.在spring中配置如下<context:spring-configured/> <context:load-time-weaver aspectj-weaving=&q ...
随机推荐
- 基于log4net的日志组件扩展封装,实现自动记录交互日志 XYH.Log4Net.Extend(微服务监控)
背景: 随着公司的项目不断的完善,功能越来越复杂,服务也越来越多(微服务),公司迫切需要对整个系统的每一个程序的运行情况进行监控,并且能够实现对自动记录不同服务间的程序调用的交互日志,以及通一个服务或 ...
- Linux CentOS内核升级
1. 说明 正在使用的阿里云服务器报了几个内核漏铜,使用自带[一键修复]需要额外的支付费用,所以尝试采用升级系统内核的方式来修复漏洞. 1.1 服务器参数 操作系统:CentOS 7.4 64位 当前 ...
- Python-- easy_install 的安装
http://peak.telecommunity.com/dist/ez_setup.py 将这里面的复制出来打包成ez_setup.py 然后cmd到目录下,直接输入ez_setup.py 可能会 ...
- MySQL基础(四)(子查询与链接)
1.子查询简介 其中,所谓的“外层查询”并不是指“查找”,指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称SQL. : 2.由比较运算符引发的子查 ...
- vue学习指南:第九篇(详细) - Vue的 Slot-插槽
Slot v-slot 插槽元素 浏览器在解析时候首先把它当作标签来解析,只有遇到不认识的就不管了,直接跳过,当你发现是组件,在以组件形式解析. 使用插槽的好处? 比如一个网站 分布顶部都是一样的, ...
- springBoot添加日志管理
一. 近期自己的项目想要一个记录日志的功能,而springboot本身就内置了日志功能,然而想要输入想要的日志,并且输出到磁盘,然后按天归档,或者日志的切分什么的,自带的日志仅仅具有简单的功能,百度了 ...
- PHP7.1-soap扩展安装
1.下载php7.1.27源码包 cd /root & wget -O php7.1.27.tar.gz http://cn2.php.net/get/php-7.1.27.tar.gz/fr ...
- Skyshop.代码解析
MarmosetInput.cginc: Input结构定义: struct Input { #if defined(MARMO_PACKED_UV) || defined(MARMO_PACKED_ ...
- 201871010109-胡欢欢《面向对象程序设计(java)》第6-7周学习总结
实验六 继承定义与使用 实验时间 2019-9-29 第一部分:理论部分. 1.继承:已有类来构建新类的一种机制.档定义了一个新类继承另一个类时,这个新类就继承了这个类的方法和域,同时在新类中添加新的 ...
- NOIP 2003 神经网络
洛谷 P1038 神经网络 https://www.luogu.org/problemnew/show/P1038 JDOJ 1278: [NOIP2003]神经网络 T1 https://neooj ...