Java EE企业级-第2章 Spring中的Bean

Bean的配置

如果把Spring看做一个大型工厂,则Spring容器中的Bean就是该工厂的产品。要想使用这个工厂生产和管理Bean,就需要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起

在实际开发中,最常使用的是XML文件格式的配置方式,这种配置方式是通过XML文件来注册并管理Bean之间的依赖关系。

在配置文件中,通常一个普通的Bean只需要定义id(或name)和class 两个属性即可,定义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">
            <bean id="bean1" class="com.itheima.Bean1" />
            <bean name="bean2" class="com.itheima.Bean2" />
    </beans>

Bean的实例化

在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。

同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。

实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化和实例工厂方式实例化

(其中最常用的是构造器实例化)。

构造器实例化

1.创建Web项目,导入相关Jar包;
2.创建名为Bean1的Java类;
3.创建Spring配置文件beans1.xml,并配置Bean1实体类Bean
4.创建测试类,测试程序

public class InstanceTest1 {
       public static void main(String[] args) {
     	String xmlPath = "com/itheima/instance/constructor/beans1.xml";
               ApplicationContext applicationContext = 
			    new ClassPathXmlApplicationContext(xmlPath);
	Bean1 bean = (Bean1) applicationContext.getBean("bean1");
                System.out.println(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-4.3.xsd">
           <bean id="bean1" class="com.itheima.instance.constructor.Bean1" />
    </beans>

Bean的作用域

Spring 4.3中为Bean的实例定义了7种作用域

singleton和prototype是最常用的两种作用域
在这里插入图片描述

singleton作用域

singleton是Spring容器默认的作用域,当Bean的作用域为singleton时,Spring容器就只会存在一个共享的Bean实例。singleton作用域对于无会话状态的Bean(如Dao 组件、Service组件)来说,是最理想的选择。

在Spring配置文件中,可以使用元素的scope属性,将Bean的作用域定义成singleton。

<bean id="scope" class="com.itheima.scope.Scope" scope="singleton"/>

prototype作用域

    对需要保持会话状态的Bean(如Struts 2的Action类)应该使用prototype作用域。在使用prototype作用域时,Spring容器会为每个对该Bean的请求都创建一个新的实例。
    
 在Spring配置文件中,同样使用<bean>元素的scope属性,将Bean的作用域定义成prototype 。
<bean id="scope" class="com.itheima.scope.Scope" scope=" prototype "/>

Bean的生命周期

Spring中Bean的生命周期的意义:可以利用Bean在其存活期间的特定时刻完成一些相关操作

singleton作用域:
Spring容器可以管理singleton作用域的Bean的生命周期,在此作用域下,Spring能够精确的知道该Bean何时被创建,何时初始化完成,以及何时被销毁。

prototype作用域:
prototype作用域的Bean,Spring只负责创建,当容器创建了Bean实例后,Bean的实例就交给客户端代码来管理,Spring容器将不再跟踪其生命周期。

Bean的装配方式

Bean的装配可以理解为依赖关系注入,Bean的装配方式即Bean依赖注入的方式

Spring容器支持多种形式的Bean的装配方式,
如基于XML的装配、
基于注解(Annotation)的装配
和自动装配(其中最常用的是基于注解的装配)

基于注解(Annotation)的装配

在这里插入图片描述

Dao层的类

在这里插入图片描述

package com.itheima.annotation;

public interface UserDao {

	public void save();
}

在这里插入图片描述

package com.itheima.annotation;
import org.springframework.stereotype.Repository;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
	public void save() {
		System.out.println("userdao...save...");
	}
}

Service层

在这里插入图片描述

package com.itheima.annotation;
public interface UserService {
	public void save();
}

在这里插入图片描述

package com.itheima.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource(name= "userDao")
	private UserDao userDao;
	@Override
	public void save() {
		//调用userDao的save()方法
				this.userDao.save();				System.out.println("userservice...save...");
					}
}

Controller层
在这里插入图片描述

package com.itheima.annotation;

import javax.annotation.Resource;


import org.springframework.stereotype.Controller;

@Controller("userController")

public class UserController {
	
	@Resource(name="userService")
	private UserService userService;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	
	public   void save() {
		this.userService.save();
		System.out.println("userController...save...");
	}
}

配置文件
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 	
    
    <!-- 使用context命名空间,通知Spring扫描指定包下所有的Bean类,进行注解解析 -->
    <context:component-scan base-package="com.itheima.annotation"/>
    
     <!--  使用context命名空间,在配置文件中开启相应的注解处理器 -->
    <context:annotation-config />  
    <bean id ="userDao" class="com.itheima.annotation.UserDaoImpl"/>
    <bean id="userService" class="com.itheima.annotation.UserServiceImpl"/>
    <bean id="userController" class="com.itheima.annotation.UserController"/>
  	
</beans>

运行效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值