1.id和name
在创建Bean组件时:
name:可重复,可以有特殊字符
id:不可重复,不可以有特殊字符
关于内存中调用的数据的处理:
public void testCreatPerson(){
//读取配置文件(创建容器)
ApplicationContext context=new ClassPathXmlApplicationContext(file);
//查找对象
Person p=(Person)context.getBean("p");
Person p3=(Person)context.getBean("p");
Person p1=new Person();
Person p2=p1;
System.out.println("p的名字:"+p.getName());
System.out.println("p3的名字:"+p3.getName());
p3.setName("222");
System.out.println("p的名字:"+p.getName());
}
控制台(Console) :
构造方法被调用
p的名字:的撒
p3的名字:的撒
p的名字:222
二、工厂的类型
常见的两种类型的工厂
ClasspathXmlApplicationContext(从类路径获取文件) 和FileSystemXmlApplicationContext(从系统磁盘获取文件)
ApplicationContext context=new FileSystemXmlApplicationContext("从系统磁盘获取文件")
ApplicationContext context=new ClassPathXmlApplicationContext("从类路径获取文件");
三、单例和多例
配置scope属性
1.默认情况下bean是单例的,scope=“singleton”
2.多例模式:scope=“prototype”
3.scope=“request”(一个request范围内,不常用)
3.scope=“session”(一个session范围内,不常用)
public void testCreatPerson(){
//读取配置文件(创建容器)
ApplicationContext context=new ClassPathXmlApplicationContext(file);
//查找对象
Person p=(Person)context.getBean("p");
Person p3=(Person)context.getBean("p");
//scope=singleton 时true
//scope="property" 时 false
System.out.println(p==p3);
}
四、什么时候创建
scope=“prototype”(多例模式) 时,在容器启动时不创建对象,当获取对象时才创建
scope=“singleton” (单例模式)时,在容器创建对象,而且只创建一个
是否延迟创建(在单例情况下)
只对单例模式有效
lazy-init=“true”: 延迟创建对象,启动时不创建,获取时在创建
lazy-init=false: 默认值,不延迟创建对象,容器启动时立即创建
<bean id="p" class="ar.bdqn.spring.pojo.Person" scope="prototype" lazy-init="true"></bean>
五、对象的初始化和销毁
<bean id="p" class="ar.bdqn.spring.pojo.Person" *init-method="init" destroy-method="destroy"* ></bean>
init-method:对象的初始化。
//读取配置文件(创建容器)
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(file);
destroy-method:对象的销毁
//读取配置文件(创建容器)
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(file);
//主动调用destroy方法执行销毁
context.destroy();
//或者调用close方法,出发销毁
context.close();
六控制反转 依赖注入
1.创建spring支持
引入jar包
aopalliance-1.0.jar
aspectjweaver-1.6.9.jar
commons-logging-1.2.jar
log4j-1.2.17.jar
spring-aop-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
- 2.创建实体类
package ar.bdqn.entity;
/**
* 实体类
* @author 安儿
*
*/
public class User {
private String username;
private Integer pwd;
private Integer sex;
private String address;
public void hello(){
System.out.println("你好我是spring:"+username);
}
public void show(){
System.out.println(username+"说"+address);
}
public User() {
super();
}
public User(String username, Integer pwd, Integer sex, String address) {
super();
this.username = username;
this.pwd = pwd;
this.sex = sex;
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getPwd() {
return pwd;
}
public void setPwd(Integer pwd) {
this.pwd = pwd;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 3.dao接口
package ar.bdqn.dao;
import ar.bdqn.entity.User;
public interface UserDao {
public User getUser();
public int addUser();
public int updateUser();
public int delUser();
}
- 4.dao接口实现类
在这里插入代码片
-
5.service接口
-
6.service接口实现类
在这里插入代码片
- 7.创建application.xnl文件
在这里插入代码片
- 8.测试
在这里插入代码片