02-Spring的IOC示例程序(通过id获取对象)
*******通过IOC容器创建id对象并为属性赋值********
整体结构:

①创建一个java工程
②导包
③创建log4j.properties日记配置文件
# Global logging configuration
log4j.rootLogger=INFO, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
④创建spring配置文件
<?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">
***********此处是手写代码************
</beans>
⑤创建javaBean对象
package cn.aynu.pojo;
public class Person {
private int id;
private String name;
private String phone;
private int age;
public Person() {
super();
}
public Person(int id, String name, String phone, int age) {
super();
this.id = id;
this.name = name;
this.phone = phone;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", phone=" + phone + ", age=" + age + "]";
}
}
⑥在application.xml中配置bean对象
<?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">
<!--id值(p1)不可重复,如果重复会报错,编译不通过 -->
<bean id="p1" class="cn.aynu.pojo.Person">
<property name="id" value="1001"/>
<property name="name" value="琥珀"/>
<property name="phone" value="18337280000"/>
<property name="age" value="18"/>
</bean> </beans>
⑦写测试类(从容器中获取bean对象)
public class PersonTest {
@Test
public void Test01() {
//获取spring容器对象(classpath:可略)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application.xml");
//根据id获取bean对象
Person person = (Person) applicationContext.getBean("p1");
System.out.println(person);
}
}
补充:①如果有多个对象同时获取P1,只有一次输出(单例模式)
②优先调用无参构造器中的内容

02-Spring的IOC示例程序(通过id获取对象)的更多相关文章
- 获取spring的IOC核心容器,并根据id获取对象
public class Client { /** * 获取spring的IOC核心容器,并根据id获取对象 * ApplicationContext的三个常用实现类 * classPathXmlAp ...
- 03-Spring的IOC示例程序(通过类型获取对象)
根据bean类型从IOC容器中获取bean的实例 ①test测试类 @Test public void Test02() { //获取spring容器对象 ApplicationContext app ...
- hibernate的获取session的两方法比较,和通过id获取对象的比较,一级缓存二级缓存
opensession与currentsession的联系与区别 在同一个线程中opensession的session是不一样的,而currentsession获取的session是一样的,这就保证了 ...
- spring的IOC(反转控制)
Spring概念 1.1.1 spring 是什么 Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制 ...
- Spring之 IOC&依赖注入
0x01.Spring 1什么是Spring Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的(解耦). 框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组 ...
- jQuery选择器ID、CLASS、标签获取对象值、属性、设置css样式
jQuery是继prototype之后又一个优秀的Javascrīpt框架.它是轻量级的js库(压缩后只有21k) , 它兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safa ...
- [JQuery] jQuery选择器ID、CLASS、标签获取对象值、属性、设置css样式
reference : http://www.suyunyou.com/aid1657.html jQuery是继prototype之后又一个优秀的Javascrīpt框架.它是轻量级的js库(压缩后 ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
- Spring中的IOC示例
Spring中的IOC示例 工程的大概内容是: 一个人在中国时用中国话问候大家,在国外时用英语问候大家. 其中, IHelloMessage是接口,用来定义输出问候信息 public interfac ...
随机推荐
- HTML文本域标签
textarea文本域 cols指定列数 rows行数(不够用会自动扩容)
- jenkins 集成环境搭建
http://www.cnblogs.com/jenniferhuang/p/3355252.html
- Python学习笔记9——异常处理
处理异常 如果执行到程序中某处抛出了异常,程序就会被终止并退出.你可能会问,那有没有什么办法可以不终止程序,让其照样运行下去呢?答案当然是肯定的,这也就是我们所说的异常处理,通常使用 try 和 ex ...
- manifold learning
MDS, multidimensional scaling, 线性降维方法, 目的就是使得降维之后的点两两之间的距离尽量不变(也就是和在原是空间中对应的两个点之间的距离要差不多).只是 MDS 是针对 ...
- path is not a working copy directory
svn: 'G:\chengXu\2017_Year\JFW\Easy7\WebRoot' is not a working copy directory 解决方法: (1)原因:eclipse把sr ...
- leetcode 869. Reordered Power of 2
function reorderedPowerOf2(N) { var a1 = N.toString().split('') a1.sort((a, b) => a.localeCompare ...
- Git 时光穿梭机
git log 提交日志 git reflog 命令日志 git status 查看状态 管理修改 git diff 工作区与暂存区 git diff master 工作区与版本库 git diff ...
- nginx的错误处理
以下是针对nginx发生错误的处理方案(将会持续更新) 遇到 nginx: [error] invalid PID number "" in "/var/run/ngin ...
- Apache Kafka(十)Partitions与Replication Factor 调整准则
Partitions与Replication Factor调整准则 Partition 数目与Replication Factor是在创建一个topic时非常重要的两个参数,这两个参数的取值会直接影响 ...
- js函数防抖和函数节流
参考链接:https://juejin.im/post/5b651dc15188251aa30c8669 参考链接:https://www.jb51.net/article/158818.htm 在我 ...