springBean

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.测试
在这里插入代码片
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值